Rotation Question (I have nothing better to name this)

Hello,
I’m very new to panda3d, and trying to make a simple first person camera, and i need help with moving the camera local to its rotation. for example, since i’m not the best at explaning stuff, if you looked left, and tryed to go forword, you would go in the direction that you’re facing. I hope that makes sence, my brain doesn’t want to work today. Anyways, I’ve thoght about using a dummy node that would stay at the center of the map, and rotate when the player looks to the left or right. The problom with that though, is that when the node rotates and the camera is somewhere other than the canter of the map, it would move the player all weird, because it would be rotating from the center of the world. how could i make it so that insted of rotating with a pivot of the center of the world, it would rotate around its own center? Aain, i’m sorry if this is hard to understand, i’m not good at explaning stuff, and my brain is just not working today. Thank you in advanced.

I do believe that I understand what you’re asking, don’t worry! :slight_smile:

I see two ways of doing this.

Before I describe them, let me mention a feature that both use: when you set a NodePath’s position (or orientation), as well as in certain other methods, you can specify a NodePath that you want the result to be relative to.

For example, if you want to set a NodePath’s position to be (5, 5, 5) relative to “render”, you can do so like this:

self.myNodePath.setPos(render, 5, 5, 5)

Now, on to the two approaches to solving your problem:

First, you can get the “forward vector” of the camera’s NodePath, and simply move along that. Since this vector always points in the direction that the NodePath is facing, moving the NodePath along it naturally moves that NodePath in the direction that the NodePath is facing.

To get the “forward vector”, we first get the NodePath’s orientation-quaternion–a piece of data that defines the direction in which the NodePath is facing, I believe. We can then simply request the “forward vector” from this quaternion.

Something like this:

quaternion = self.myNodePath.getQuat(render)
forwardVector = quaternion.getForward()
self.myNodePath.setPos(render, forwardVector)

Note that I’m getting the quaternion and setting the NodePath’s position relative to “render”–the root of the scene-graph. If your NodePath is parented directly beneath “render”, then this is superfluous; conversely, if you want to place your NodePath relative to something else, then you would want to replace “render” with that something else as appropriate, I believe.

The second option is to place the NodePath on the y-axis, relative to itself.

Something like this:

self.myNodePath.setY(self.myNodePath, 1)

The second option above is arguably simpler than the first. However, there may be cases in which the first option above proves easier to work with–for example, if there’s a scale applied to your NodePath, then it will affect the second option above, but not the first, if I’m not much mistaken.

I leave it to you to determine which better suits your specific project!

1 Like

I wrote an example before.

1 Like