Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get node position after physics calculation scenekit

enter image description here

Working on a scenekit application, I have 2 boxes that can fire shots to each other for points. This is the calculation of the position where shots need to be made for the first box :

shot1 = SCNNode()
shot1.geometry = SCNSphere(radius: 0.5)
shot1.physicsBody = SCNPhysicsBody.dynamic()
shot1.position = SCNVector3Make
(boxNode1.position.x, 
 boxNode1.position.y + 1, 
 boxNode1.position.z - 1)
scnScene.rootNode.addChildNode(shot1)

And for the second box :

shot2 = SCNNode()
shot2.geometry = SCNSphere(radius: 0.5)
shot2.physicsBody = SCNPhysicsBody.dynamic()
shot2.position = SCNVector3Make
(boxNode2.position.x, 
 boxNode2.position.y + 1, 
 boxNode2.position.z + 1)
scnScene.rootNode.addChileNode(shot2)

Every shot is made just in good position and is thrown to the other box with applying force on its physicsBody as impulse.

For touch implementation I used touchesBegan to search for touches, then if the touch have the hitResult, I fire shots as I described above.

The problem happens where : If the shot for example shot1 hit boxNode2, the boxNode2 is moved backward as I expect it and this time if I fire a shot with boxNode2, the starting position of shot2 IS NOT in NEW location of boxNode2 as I expect it, and is starting to fire right where it was at the beginning!

So how can I fire shots from new position of the boxNodes?

like image 341
FarhaD Avatar asked Aug 29 '17 16:08

FarhaD


1 Answers

Just use presentation:

let realPosition = yourNode.presentation.worldPosition
like image 189
Vasilii Muravev Avatar answered Dec 06 '22 13:12

Vasilii Muravev