Simple FPS controller help

Hey all,

I’m trying to get back into Panda3D after rectifying my previous mistake of not knowing python very well. Anyway, I was taking some queues from some first person scripts people had built and I started taking a look at how this worked and I’m hoping there is a simple way to accomplish a “strafe.” I’m using a mouse look + WASD for movement.

direction = base.camera.getNetTransform().getMat().getRow3(1)
direction.setZ(0)
direction.normalize()
if(self.keyMap["forward"] != 0):
    self.position += direction * .3

the value for position in the above snippit is just base.camera.getPos(). Going back and forward is easy enough, but I’m not sure how to add if statements for left/right. Any help would be much appreciated. Let me know if I need to add any additional code; but I figure most people have done some sort of FPS controller before.

edit to add:

Basically I can add any x,y values for the strafing left/right, but I’m not sure how to get a parallel vector to the direction the camera is facing and move along that.

Okay, I’ve done some more digging and have a much more specific question.

First of all, I realized that I’m not doing parallel movement (duh) I’m looking for perpendicular movement. Once I realized my vocabulary mistake googling an answer became much easier.

So, what I’m trying to do is figure out how to just declare a static vector to use as a cross vector. I’m pretty sure I should be able to just do something like:

direction1 = Vec3(0,0,1)
direction2 = direction.cross(direction1)
self.position += direction2 * .3

I know this is hand holding, but I’m still very new to this…how do I declare that vector. The manual led me to believe it’s as simple as I put up there but I’m getting a Vec3 is not defined error.

Or maybe I’m doing it totally wrong. Help?

edit: My looking around tells me that I should be able to do whatever = Vec3(x,y,z)

I am now wondering why that specifically is failing.

You should be able to use the cross product, exactly as you describe. Not sure immediately why that doesn’t work for you.

But Panda provides an easier interface that doesn’t require you to know about cross products or matrix rows or anything. You can just ask Panda for a “relative vector”.

Replace:

direction = base.camera.getNetTransform().getMat().getRow3(1)

with:

direction = render.getRelativeVector(base.camera, (0, 1, 0))

to get the forward vector (0, 1, 0) of the camera relative to render, and use:

direction = render.getRelativeVector(base.camera, (1, 0, 0))

to get the right vector (1, 0, 0) of the camera relative to render. I suggest render because that’s the root of the scene graph, which is what you are computing with getNetTransform(), but what you really want to use is the parent of whatever your self.position node is (maybe you’re applying self.position back to base.camera, in which case the parent is indeed render, unless you’ve moved it).

In general, it helps to have a solid understanding of different coordinate spaces and be able to visualize moving between them. In Panda, every node defines its own coordinate space.

Note that you can also do this whole operation more directly on the camera itself:

base.camera.setPos(base.camera, (0, 0.3, 0))

moves the camera forwards a bit, and

base.camera.setPos(base.camera, (0.3, 0, 0))

moves the camera a bit to the right.

David

Awesome, thanks so much David!