Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rotate a 3d vector with a 4x4 matrix transform or euler angles?

I have a 3d vector I'm applying as a physics force:

let force = SCNVector3(x: 0, y: 0, z: -5)  
node.physicsBody?.applyForce(force, asImpulse: true)

I need to rotate the force based on the mobile device's position which is available to me as a 4x4 matrix transform or euler angles.

var transform :matrix_float4x4 - The position and orientation of the camera in world coordinate space.

var eulerAngles :vector_float3 - The orientation of the camera, expressed as roll, pitch, and yaw values.

I think this is more of a fundamental 3d graphics question, but the application of this is a Swift based iOS app using SceneKit and ARKit.

There are some utilities available to me in the SceneKit and simd libraries. Unfortunately my naive attempts to do things like simd_mul(force, currentFrame.camera.transform) are failing me.

like image 542
gmcerveny Avatar asked Dec 24 '22 16:12

gmcerveny


1 Answers

@orangenkopf provided a great answer that helped me come up with this:

let force = simd_make_float4(0, 0, -5, 0)
let rotatedForce = simd_mul(currentFrame.camera.transform, force)
let vectorForce = SCNVector3(x:rotatedForce.x, y:rotatedForce.y, z:rotatedForce.z)
node.physicsBody?.applyForce(vectorForce, asImpulse: true)
like image 183
gmcerveny Avatar answered May 20 '23 04:05

gmcerveny