To better understand NodePaths
, let's go ahead and use the print statement to take a closer look at what's going on.
__init__
method in our world class, add this line:print(self.track)
chp02_04.py
" and run it. Look at the command prompt window to see the output of the print
statement. NodePath
, we'll get a result like this. print
statement to look like this:chp02_05.py
" and run it. ModelRoot
is the node that the self.track NodePath
points to.The purpose of this exercise was to better understand the difference between a NodePath and a node. When we printed self. track:
print(self.track)
We printed out the NodePath
that the call to the loader
returned to us. We know it was a NodePath
because the output was similar to a file path and showed the location of the NodePath
in the Scene Graph. Next we used a call to the NodePath
method node():
print(self.track.node())
This printed out the node itself, a ModelRoot
named Track.egg
.
Locked within NodePaths
are the most common tools we'll use to manipulate the virtual world inside Panda3D. It is through these tools that we will make our game dance and twirl. They are the tools that change the position, rotation, and scale of the node the NodePath
handles. Each of these tools is made up of several methods, one to control changing all three axes at once, and three more methods to control each axis individually.
First we have the methods that control the position within the 3D world:
myNodePath.setPos([x],[y],[z]) myNodePath.setX([x]) myNodePath.setY([y]) myNodePath.setZ([z])
Panda3D uses a z-up coordinate system. That means that in the world of Panda3D, the x and y axis control horizontal positioning, and the z axis controls height. This is contrary to many modeling packages and can take some getting used to. Try to keep it in mind as we continue through the tutorials in this book.
Next are the methods that control the rotation of the node:
myNodePath.setHpr([h],[p],[r]) myNodePath.setH([h]) myNodePath.setP([p]) myNodePath.setR([r])
hpr stands for heading, pitch, and roll. If you imagine the node as a jet, facing down the positive y axis, heading will turn left and right, pitch will climb and dive, and roll will spin the jet and make you throw up. To put it in technical terms, heading rotates the model around its z axis, pitch rotates it around its x axis, and roll rotates it around its y axis.
Lastly we have the scalar methods:
myNodePath.setScale([sx],[sy],[sz]) myNodePath.setSx([sx]) myNodePath.setSy([sy]) myNodePath.setSz([sz])
The scale methods work relative to the original scale of the node, so setting a scale to 2 will double the size along that axis and setting it to 0.5 will halve the scale along that axis. We can also call setScale
with only a single argument:
myNodePath.setScale([scale])
This will change the scale of the node uniformly along all axis.