I'm working on a top-down space game built using Swift and SceneKit with the following setup:
SCNNode representing a spaceship
- Rotation is constrained to the y axis; values range from
-M_PI_2
toM_PI + M_PI_2
- Movement is constrained to the x and z axes.
Game controller thumbstick input
- Values range from
-1.0
to1.0
on the x and y axes.
When the game controller's thumbstick changes position, the spaceship should rotate using the physics body to match the thumbstick's radian.
The target radian of the thumbstick can be calculated with the following:
let targetRadian = M_PI_2 + atan2(-y, -x)
The current radian of the node can be obtained with the following:
let currentRadian = node.presentationNode.rotation.w * node.presentationNode.rotation.y
NSTimeInterval deltaTime
provides the time in seconds since the last rotation calculation.
How can the node be rotated using angularVelocity
, applyTorque
, or another physics method to reach the targetRadian
?