As to your first question, I donât have the answer offhand, but I do think that Iâve seen similar threads in the pastâyou might perhaps find an answer by searching the forums (if you havenât done so already).
As to your second question, there are actually a few ways to do that, I do believe! Let me outline two:
- You can set a NodePathâs position relative to a given NodePathâincluding itself. And by setting a NodePathâs position relative to itself, you set its position within its own coordinate space, with the y-axis being the âforwardâ axis.
For example:
# Set a NodePath's position to be a position (3, 0, 1) relative to
# another NodePath
self.myNodePath.setPos(self.someOtherNodePath, 3, 0, 1)
# Set a NodePath's position to be 1 unit in its "forward" direction.
self.myNodePath.setY(self.myNodePath, 1)
# Set a NodePath's position to be 2 units in its "forward" direction
# and 1 unit in its "left" direction
self.myNodePath.setPos(self.myNodePath, -1, 2, 0)
However! Do note that the NodePathâs coordinate space includes things like its scaling factor (including any applied by nodes above it in the scene graph), which can affect the results of such operations!
- You can extract a NodePathâs âforwardâ, ârightâ, and âupâ vectors, and use those.
For example:
# First, get the NodePath's orientation as a quaternion.
# (If you're not familiar with quaterions, don't worry--
# for this process you don't have to be.)
quat = self.myNodePath.getQuat()
# Then extract the relevant vector(s) from that quaternion
forward = quat.getForward()
#right = quat.getRight()
#up = quat.getUp()
# Get the NodePath's current position
currentPos = self.myNodePath.getPos()
# And finally, set the NodePath's position to be 2 units
# in the "forward" direction
self.myNodePath.setPos(currentPos + forward * 2)
Note of course that, in both of these approaches, the result takes into account the full orientation of the NodePath. As a result, if your NodePath is tilted up, for example, the âforwardâ direction will likely be tilted up. If this is undesirable, you can perhaps use multiple parented nodes to separate out things like âhorizontal turningâ from âlooking up and downâ, allowing you to get a horizontal âforwardâ vector from the former.