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
either the coordinates system of
nodeA
's parentnodeB1.transform = [nodeB1.parentNode convertTransform:nodeA.transform fromNode:nodeA.parentNode];
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:
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}.
Convert that vector to scene space with
convertPosition:toNode:
and the scene'srootNode
. This gets you the flight direction in terms of the scene, taking the node's orientation into account.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
How to Understand What Is Causing a Crash Involving '_Nstouchbarfinderobservation'
Prevent Redirect Response with Alamofire in Swift
Curl with Alamofire - Swift - Multipart/Form-Data
Swift Firebase Custom Object with Document Id
Swiftui Sheet Not Updating Variable
Calculate Time Difference in Swift 4
How to Draw Line Node Keep Same Size in Camera as Measure App in Iphone
Swift Alternative to Respondstoselector:
Realitykit - How to Edit or Add a Lighting
How to Use a Value Type Object as a Reference Type
Using Custom Cifilter on Calayer Shows No Change to Calayer
Swiftui Scroll/List Scrolling Events
Non Exhaustive List When Handling Errors Inside a Class Function in Swift
Anyobject Try Cast to Equatable
Create JSON Object from Class in Swift