Scenekit Physics Add Velocity in Local Space

SceneKit physics add velocity in local space

As it turns out, I had to get the proper position from the rootNode of the scene to perform a proper rotation based on the current orientation of the SCNNode.

xAxis = self.convertPosition(SCNVector3Make(1.0, 0.0, 0.0), toNode: self.parentNode!)
yAxis = self.convertPosition(SCNVector3Make(0.0, 1.0, 0.0), toNode: self.parentNode!)
zAxis = self.convertPosition(SCNVector3Make(0.0, 0.0, 1.0), toNode: self.parentNode!)
if isXRotatingPositive {
self.physicsBody?.applyTorque(
SCNVector4(
x: sin(kPlayerShipRotationSpeed/2.0) * (xAxis.x - self.position.x),
y: sin(kPlayerShipRotationSpeed/2.0) * (xAxis.y - self.position.y),
z: sin(kPlayerShipRotationSpeed/2.0) * (xAxis.z - self.position.z),
w: cos(kPlayerShipRotationSpeed/2.0) * kPlayerShipRotationMagnitude),
impulse: true)
}

Then I just used the standard quaternion rotation formula to get the rotation based on the new axes from the current position.
I hope this helps someone else (and that more information on SceneKit is forthcoming)
If any SceneKit experts want to comment on this or offer suggestions, they are much appreciated. :)

SceneKit - Modifying velocity for the direction a node is facing

I guess you use iOS11, so I think you can use SCNNode.convertVector for your purpose.

let velocityInLocalSpace = SCNVector3(0, 0, -0.15)
let velocityInWorldSpace = t.presentation.convertVector(velocityInLocalSpace, to: nil)
t.physicsBody?.velocity = velocityInWorldSpace

SceneKit Physics simulation does not match actual Node location

As per usual, the issue was RTFM. I set the physics body to be the wrong type:

self.enemyDrone?.physicsBody = SCNPhysicsBody(type: .dynamic, shape: dronePhysicsShape)

Needed to be changed to

self.enemyDrone?.physicsBody = SCNPhysicsBody(type: .kinematic, shape: dronePhysicsShape)

Scenekit Physics - Prevent Rotation on Collision

Found the answer on another thread: set angularVelocityFactor to 0.
allowRotation in SceneKit?
Apologies for the duplicate question. I searched, but only found the other thread as a link from my own question's page.

Transfer global to local node position in SceneKit

you have to express the new transform in the coordinate system of nodeB1's parent, and from

  1. either the coordinates system of nodeA's parent

    nodeB1.transform = [nodeB1.parentNode convertTransform:nodeA.transform fromNode:nodeA.parentNode];

  2. or the world coordinates system

    nodeB1.transform = [nodeB1.parentNode convertTransform:nodeA.worldTransform fromNode:nil];

of course you can replace nodeB1.parentNode by nodeB, and in this case nodeA's parent is the root node

scenekit node rotation and movement in 3D Space

If you want to combine gyroscope and accelerometer data to get an orientation, you'd probably be better off letting CoreMotion do that for you. The methods listed under Managing Device Motion Updates in the docs get you CMDeviceMotion objects; from one of those you can get a CMAttitude that represents an orientation, and from that you can get a rotation matrix or quaternion that you can apply to a node in your scene. (Possibly after further transforming it so that a neutral orientation fits into your scene the way you want.)

After you've oriented the node the way you want, getting a flight direction from that should be pretty simple:

  1. Choose a flight direction in terms of the local coordinate space of the node and make a vector that points in that direction; e.g. a camera looks in the -z direction of its node, so if you want the camera node to look along its flight path you'll use a vector like {0, 0, -1}.

  2. Convert that vector to scene space with convertPosition:toNode: and the scene's rootNode. This gets you the flight direction in terms of the scene, taking the node's orientation into account.

  3. Use the converted vector to move the node, either by assigning it as the velocity for the node's physics body or by using it to come up with a new position and moving the node there with an action or animation.



Related Topics



Leave a reply



Submit