iOS OpenGL ES 2.0旋转1点笛卡尔X Radians关于Origin

时间:2014-02-16 18:35:49

标签: ios objective-c opengl-es opengl-es-2.0

我需要旋转以笛卡尔XYZ坐标表示的关于Z轴的单个点。以下2次尝试无法正常工作 - 我相信第一次尝试更正确..

我尝试使用此网站上的数学轮换点数:http://farside.ph.utexas.edu/teaching/336k/newton/node153.html

// 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:

之后提取点来尝试此功能
// 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轴旋转的球体和一个在特定球面坐标(代表纬度/经度位置)处指定的广告牌精灵,并且需要能够使点与地球一起旋转。

我做错了什么?当我知道要旋转的弧度数时,如何计算新的X和Y坐标(Z是常数)以围绕Z轴旋转XYZ点?谢谢!

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

// 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)));

}

问题是我的图像在它应该处于的纬度/经度周围跳舞 - 它几乎是图8。

2 个答案:

答案 0 :(得分:1)

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

float x, y;
float currentAngle;
float radius = sqrt(x*x + y*y);
x = radius*cos(currentAngle);
y = radius*sin(currentAngle);

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

答案 1 :(得分:0)

看起来您正在将每个帧添加到您的角度。您可以计算“delta angle”,只是从前一帧旋转的角度,或者使用现在的角度,但是将旋转应用于初始方向,而不是最后一帧的结果。