首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS OpenGL es2.0旋转1点笛卡尔X射线关于起源

iOS OpenGL es2.0旋转1点笛卡尔X射线关于起源
EN

Stack Overflow用户
提问于 2014-02-16 18:35:48
回答 2查看 309关注 0票数 0

我需要旋转一个点,用笛卡尔XYZ坐标表示,关于Z轴。以下两次尝试都没有正常进行--我相信第一次是正确的。

我试着用这个网站上的数学来旋转点数:http://farside.ph.utexas.edu/teaching/336k/newton/node153.html

代码语言:javascript
复制
// Rotate the XYZ coordinate for the pin image
if ( [satName isEqualToString:@"pin"] ) {
    double x = xyz.x;
    double y = xyz.y;
    double radians = self.timeSinceOpenGlStarted;
    x = x * cos(radians) + y * sin(radians);
    y = -x * sin(radians) + y * cos(radians);
    xyz.x = x;
    xyz.z = y;
}

我还尝试了这个函数,方法是在GLKMatrix4Rotate之后提取点:

代码语言:javascript
复制
// This function rotates XYZ a certain of radians about the origin and gives back XYZ
- (GLKVector4)rotateXYZCoordinates:(XYZ*)coords {

    // Get the current modelview matrix
    GLKMatrix4 currMat = self.effect.transform.modelviewMatrix;

    // Print the coords before
    NSLog(@"Before: %f %f %f",coords->x,coords->y,coords->z);
    NSLog(@"Rotation Before: %f %f %f",currMat.m00,currMat.m10,currMat.m20);

    // Construct the rows in the new matrix
    float d = sqrt( pow(currMat.m00,2) + pow(currMat.m10,2) + pow(currMat.m20,2) );
    GLKVector4 columnToInsert0 = GLKVector4Make(d, 0, 0, coords->x);
    GLKVector4 columnToInsert1 = GLKVector4Make(0, d, 0, coords->y);
    GLKVector4 columnToInsert2 = GLKVector4Make(0, 0, d, coords->z);
    GLKVector4 columnToInsert3 = GLKVector4Make(0, 0, 0, 1);

    // Build the new Matrix
    GLKMatrix4 noTranslationInfo = GLKMatrix4SetRow(currMat, 0, columnToInsert0);
    noTranslationInfo = GLKMatrix4SetRow(noTranslationInfo, 1, columnToInsert1);
    noTranslationInfo = GLKMatrix4SetRow(noTranslationInfo, 2, columnToInsert2);
    noTranslationInfo = GLKMatrix4SetRow(noTranslationInfo, 3, columnToInsert3);

    // Throw the world translation coordinates in the matrix
    noTranslationInfo.m30 = ( noTranslationInfo.m30 );
    noTranslationInfo.m31 = ( noTranslationInfo.m31 );
    noTranslationInfo.m32 = ( noTranslationInfo.m32 );

    // Now rotate the matrix so many angles
    noTranslationInfo = GLKMatrix4Rotate(noTranslationInfo, self.timeSinceOpenGlStarted, 0, 0, 1);

    // Latch the output
    coords->x = noTranslationInfo.m30;
    coords->y = noTranslationInfo.m31;
    coords->z = noTranslationInfo.m32;

    // Print the coords After
    NSLog(@"AFter: %f %f %f",coords->x,coords->y,coords->z);
    NSLog(@"Rotation After: %f %f %f",noTranslationInfo.m00,noTranslationInfo.m10,noTranslationInfo.m20);

}

我有一个沿Z轴旋转的球体和一个在特定的球面坐标(代表lat/lon位置)指定的标牌雪碧,并且需要让点随地球旋转的能力。

我做错了什么?当我知道要旋转的半径数时,如何计算一个新的X和Y坐标(Z是常数)来使XYZ点绕Z轴旋转?谢谢!

更新:现在我已经尝试过了:

代码语言:javascript
复制
// Rotate the XYZ coordinate for the pin image
/* http://www.blitzbasic.com/Community/posts.php?topic=70536
 ;rotate offset around Z axis
 newx# = x# * Cos#(zr#) - y# * Sin#(zr#)
 newy# = x# * Sin#(zr#) + y# * Cos#(zr#)
 x# = newx#
 y# = newy#

 ;rotate offset around X axis
 newy# = y# * Cos#(xr#) - z# * Sin#(xr#)
 newz# = y# * Sin#(xr#) + z# * Cos#(xr#)
 y# = newy#
 z# = newz#


 ;rotate offset around Y axis
 newx# = z# * Sin#(-yr#) + x# * Cos#(-yr#)
 newz# = z# * Cos#(-yr#) - x# * Sin#(-yr#)
 x# = newx#
 z# = newz#
 */
if ( [satName isEqualToString:@"pin"] && self.shouldAnimate == YES ) {

    //NSLog(@"ONE %f %f %f %f",xyz.x,xyz.y,xyz.z,sqrt(pow(xyz.x, 2)+pow(xyz.y,2)+pow(xyz.z,2)));

    double x = xyz.x;
    double y = xyz.y;
    double z = xyz.z;

    NSLog(@"%f",self.timeSinceOpenGlStarted); // Values like: 32521.473728

    double zr = self.timeSinceOpenGlStarted;
    double yr = 0.0f;
    double xr = 0.0f;

    // Rotations must be in this order: Z then X then Y

    // Rotate around Z
    x = x * cos(zr) - y * sin(zr);
    y = x * sin(zr) + y * cos(zr);

    // Rotate around X
    y = y * cos(xr) - z * sin(xr);
    z = y * sin(xr) + z * cos(xr);

    // Rotate around Y
    x = z * sin(-yr) + x * cos(-yr);
    z = z * cos(-yr) + x * sin(-yr);

    // Get the coordinates back
    xyz.x = x;
    xyz.y = y;
    xyz.z = z;

    //NSLog(@"TWO %f %f %f %f",xyz.x,xyz.y,xyz.z,sqrt(pow(xyz.x, 2)+pow(xyz.y,2)+pow(xyz.z,2)));

}

问题是,我的图像在它应该在的lat/lon周围跳舞--它几乎达到了图8。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-17 08:32:40

我要么不明白你想要实现什么,要么你的这些方法有点奇怪。如果您需要围绕Z轴(在XY平面上)围绕中心(0,0,0)旋转一个点,那么您应该使用这样的方法:

代码语言:javascript
复制
float x, y;
float currentAngle;
float radius = sqrt(x*x + y*y);
x = radius*cos(currentAngle);
y = radius*sin(currentAngle);

为了使它更容易,您可以简单地使用半径(在您的情况下应该是常数)和角度的弧度。在这种情况下,只需要最后两行代码片段。

票数 1
EN

Stack Overflow用户

发布于 2014-02-17 08:30:05

看起来你是在把每一帧加到你的角度上。你可以计算一个“三角角”--从前一帧开始旋转的角度,或者像现在一样使用这个角度,但是把旋转应用到初始方向,而不是最后一帧的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21815634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档