Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS OpenGL ES 2.0围绕原点旋转1点笛卡尔X弧度_Ios_Objective C_Opengl Es_Opengl Es 2.0 - Fatal编程技术网

iOS OpenGL ES 2.0围绕原点旋转1点笛卡尔X弧度

iOS OpenGL ES 2.0围绕原点旋转1点笛卡尔X弧度,ios,objective-c,opengl-es,opengl-es-2.0,Ios,Objective C,Opengl Es,Opengl Es 2.0,我需要绕Z轴旋转一个以笛卡尔XYZ坐标表示的点。以下两次尝试都不正常-我认为第一次更正确 我尝试使用此网站上的数学旋转点: 我还通过提取GLKMatrix4Rotate之后的点来尝试此函数: // This function rotates XYZ a certain of radians about the origin and gives back XYZ - (GLKVector4)rotateXYZCoordinates:(XYZ*)coords { // Get the cu

我需要绕Z轴旋转一个以笛卡尔XYZ坐标表示的点。以下两次尝试都不正常-我认为第一次更正确

我尝试使用此网站上的数学旋转点:

我还通过提取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所示。

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

我不明白你想要实现什么,或者你的这些方法有点奇怪。如果需要绕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的结果是什么?它给了我一个非恒定的旋转加速度-我旋转的弧度是一个恒定的速率。谢谢您的回答。这把它修好了。
float x, y;
float currentAngle;
float radius = sqrt(x*x + y*y);
x = radius*cos(currentAngle);
y = radius*sin(currentAngle);