Changing SCNNode position without changing orientation

I use SceneKit and have an application with several SCNNodes, and when the user selects one, he will follow their finger (using UILongPressGestureRecognizer). In the state started and changed, I use the following line of code to change the position of the node:

_currentlyMoving?.position = SCNVector3Make(worldPoint.x, worldPoint.y, worldPoint.z) 

On this line _currentlyMoving is SCNNode, and worldPoint is the position of my finger.

This moves the node to the correct position; however, apparently, it also changes the rotation of the node to (0,0,0) (node ​​is always at the initial rotation.)

Is there a way to reposition the SCNNode without affecting the rotation?

Also, I tried the following, but it didn't work either (the same thing happened):

 _currentlyMoving?.transform.m41 = worldPoint.x _currentlyMoving?.transform.m42 = worldPoint.y _currentlyMoving?.transform.m43 = worldPoint.z 

The only thing I do with node is the following three lines to stop it when moving:

 _currentlyMoving?.physicsBody?.velocity = SCNVector3Make(0.0, 0.0, 0.0) _currentlyMoving?.physicsBody?.velocityFactor = SCNVector3Make(0.0, 0.0, 0.0) _currentlyMoving?.physicsBody?.angularVelocity = SCNVector4Make(0.0, 0.0, 0.0, 1.0) 
+5
source share
1 answer

So, after testing many things for several days, I found out that the rotation is never set to node; it is installed only on the presentation node. Because of this, whenever I set a position, the node transformation was automatically recalculated based on my new position and current rotation (which, in his opinion, was [0,0,0]), effectively resetting the node rotation.

So, to solve this problem, you need to change the rotation of the node to the rotation of presentationNode.

This is what I am currently doing in my state panGestureRecognizer .Began (after setting the touch node to currentlyMoving :

 if let currentlyMoving = _currentlyMoving { if let presentationNode = currentlyMoving.presentationNode() { currentlyMoving.rotation = presentationNode.rotation } currentlyMoving.position = newLocation } 

It just sets the rotation of the node to rotate the view before moving it so that the transformation is properly configured depending on what the user can see.

The problem I ran into was that some of the models I downloaded still rotated strangely, but all the models that I created worked just fine. I had to create my own versions of other models, after which everything works fine. Thus, I still do not quite understand what was wrong with the first models, which caused them to shift slightly when I set their rotations to rotate the view (I have no experience with 3D modeling).

+5
source

Source: https://habr.com/ru/post/1203900/


All Articles