Dilraj S Devgun
Dilraj S Devgun

Reputation: 3

SceneKit - Modifying velocity for the direction a node is facing

I'm currently working on an ARKit game using SceneKit and I am having trouble controlling and moving the nodes. The layout for the game is quite simple, I have a node representing a tank that has a dynamic physics body. I can successfully add the node to the map, however I want to control the node using a joystick.

I am trying to modify the tank's velocity property so that when the joystick is pushed up the tank moves forward in the direction that it is facing. To do this I first tried to simply just set the velocity property as such:

t.physicsBody?.velocity = SCNVector3Make(0, 0, -0.15)

This caused the tank to move with the desired speed however it was not in the correct direction. As I understand the -z direction is the forward facing direction. Apple's documentation for local vs. world space is very vague and after looking around online I found this stack overflow article SceneKit physics add velocity in local space. I have tried converting to the parent's node which still doesn't have the desired effect. I have also tried using the presentation node's physics body which has updated values but both ways are causing jittery, non-uniform, and unpredictable movement.

I would use forces, however I want the SCNode to have a very immediate and cartoonish movement without acceleration. Any help understanding what is going on and on SceneKit's way of handling movement would be appreciated.

Upvotes: 0

Views: 1281

Answers (1)

magicien
magicien

Reputation: 141

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

Upvotes: 1

Related Questions