gpt4 book ai didi

objective-c - 从子弹物理四元数转换为角度以与 CATransform3DMakeRotation 一起使用

转载 作者:行者123 更新时间:2023-12-03 17:58:54 25 4
gpt4 key购买 nike

我使用 CATransformLayer 构建了一个 6 面 3D 骰子,将其用作容器层,将 6 个变换后的图层固定为可以在空间中旋转的 3D 对象。 I got the idea from here 。它运行良好,我可以在顶层调用 CATransform3DMakeRotation 旋转“骰子”,骰子旋转并且运行良好。

下一步是将“子弹物理”插入到程序中。我已经可以工作了,但是我被卡住了,因为我不知道如何将物理世界中骰子的“子弹物理‘四元数’方向”转换为可以传递给容器 CALayer 方法的旋转角度。

这就是我正在做的事情:

1) 调用“子弹物理”来获取骰子的方向:

// QUATERNION: Get the orientation
btQuaternion quatO = TObject->getOrientation();

2)将四元数转换为欧拉(大量时间谷歌搜索答案找到了下面复制的方法来执行此操作) qW = quatO.getW(); qX = quatO.getX(); qY = quatO.getY(); qZ = quatO.getZ(); [自四元数到欧拉];

3) 将新的欧拉角应用到我的 CALayer (sCtrl)

[sCtrl rotaX:eX rotaY:eY rotaZ:eZ];

注1:这是用于旋转我的日历的方法

- (CATransform3D) rotaX:(CGFloat) anguloX  rotaY:(CGFloat) anguloY rotaZ:(CGFloat) anguloZ
{
CATransform3D rX;
CATransform3D rY;
CATransform3D rZ;
CATransform3D ret;

// Apply all the rotations in order (x, y, z)
rX = CATransform3DMakeRotation([self gradosARadianes:(anguloX)], 1.0f, 0.0f, 0.0f);
ret = CATransform3DConcat(baseTransform, rX);
rY = CATransform3DMakeRotation([self gradosARadianes:(anguloY)], 0.0f, 1.0f, 0.0f);
ret = CATransform3DConcat(rX, rY);
rZ = CATransform3DMakeRotation([self gradosARadianes:(anguloZ)], 0.0f, 0.0f, 1.0f);
ret = CATransform3DConcat(rY, rZ);


// Store finished result for next time start from there...
baseTransform = ret;

return ret;
}

注2:这是我发现的将四元数转换为欧拉的方法。

-(void) quaternionToEuler
{
float matrix[3][3];
float cx,sx;
float cy,sy,yr;
float cz,sz;
matrix[0][0] = 1.0f - 2.0f * (qY * qY + qZ * qZ);
matrix[0][1] = (2.0f * qX * qY) - (2.0f * qW * qZ);
matrix[1][0] = 2.0f * (qX * qY + qW * qZ);
matrix[1][1] = 1.0f - (2.0f * qX * qX) - (2.0f * qZ * qZ);
matrix[2][0] = 2.0f * (qX * qZ - qW * qY);
matrix[2][1] = 2.0f * (qY * qZ + qW * qX);
matrix[2][2] = 1.0f - 2.0f * (qX * qX - qY * qY);


sy = -matrix[2][0];
cy = sqrt(1 - (sy * sy));
yr = (float)atan2(sy,cy);
eY = (yr * 180.0f) / (float)M_PI;

if (sy != 1.0f && sy != -1.0f)
{
cx = matrix[2][2] / cy;
sx = matrix[2][1] / cy;
eX = ((float)atan2(sx,cx) * 180.0f) / (float)M_PI; // RAD TO DEG

cz = matrix[0][0] / cy;
sz = matrix[1][0] / cy;
eZ = ((float)atan2(sz,cz) * 180.0f) / (float)M_PI; // RAD TO DEG
}
else
{
cx = matrix[1][1];
sx = -matrix[1][2];
eX = ((float)atan2(sx,cx) * 180.0f) / (float)M_PI; // RAD TO DEG

cz = 1.0f;
sz = 0.0f;
eZ = ((float)atan2(sz,cz) * 180.0f) / (float)M_PI; // RAD TO DEG
}
}

总之,我的目标:使用 CALayer(不是 OpenGL)在我的程序中表示“子弹物理”盒子(骰子)。为此,我使用 CATransformLayer 作为 6 个转换层到 3D 对象的容器。

--我现在得到的结果不好,图形骰子旋转但显然不正确......所以我在这里做了完全错误的事情......

问题是:鉴于我可以获得子弹物理世界中盒子的“方向或旋转”,我如何将返回的四元数转换为可用于 CATransform3DMakeRotation 的东西(角度)。

提前非常感谢,

路易斯

最佳答案

这是一个糟糕的方法。您从一个矩阵开始,将其转换为四元数,将其转换为欧拉角,然后将其转换为矩阵。

调用 getCenterOfMassTransform().getBasis() 来获取 btMatrix3x3,而不是 getOrientation()。将其打包到 CATransform3D 的左上角,并用零填充其余部分(m44 除外,它应该是 1)。

关于objective-c - 从子弹物理四元数转换为角度以与 CATransform3DMakeRotation 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9557966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com