How can I calculate a multi-axis SCNVector4 rotation for a SCNNode?

自闭症网瘾萝莉.ら 提交于 2019-12-01 06:47:21

问题


The SCNNode take a rotation using a SCNVector4, which has an angle (w) and a magnitude how that angle applies to each axis (x, y, z). For example, to rotate 45 degrees around the x-axis I'd create a SCNVector4 like this:

SCNVector4Make(1.0f, 0, 0, DEG2RAD(45))

What I'd like to do is rotate it across all three axis, for example: 45 degrees on the x-axis, 15 degrees on the y-axis and -135 degress across the z-axis. Does anyone know the math to calculate the final SCNVector4?


回答1:


You'll need to generate an SCNVector4 for each of the rotations, and then multiply them. Note that the order of operations matters!

http://www.cprogramming.com/tutorial/3d/rotationMatrices.html has a pretty good writeup of the math. Any OpenGL reference that deals with rotation matrices is worth a look too.




回答2:


Instead of rotation property, use eulerAngles and specify angle for each axis




回答3:


If you're not animating the rotation, it might be cleaner to just set the transform matrix directly, like:

node.transform = CATransform3DRotate(CATransform3DRotate(CATransform3DRotate(node.transform, xAngle, 1, 0, 0), yAngle, 0, 1, 0), zAngle, 0, 0, 1);



回答4:


Do you ask for rotation matrix or how to simply rotate in general? If the second is correct then for example:

[node runAction:[SCNAction rotateByX:0 y:M_PI z:0 duration:0]];


来源:https://stackoverflow.com/questions/15776637/how-can-i-calculate-a-multi-axis-scnvector4-rotation-for-a-scnnode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!