How to move camera forward in panda3d?

420 views Asked by At

I am beginner in Panda3D module, so I am playing around. I want to move the camera forward on the press of "w". So I did this in the init class.

self.accept('w', self.forward)

Now I am clueless about what I should write in the forward function.

3

There are 3 answers

0
whatever On

You can use vectors to set it directly using camera position (dt = delta time) base.camera.setPos(base.camera.getPos() + Vec3(5*dt, 0, 0))

If your looking for FPS forward in view direction you probably want

quaternion = base.camera.getQuat()
fwd = quaternion.getForward()            
base.camera.setPos(base.camera.getPos() + fwd*dt*5)

The getQuat gets a vector with convenient direction information.

0
error 1044 On

so I would create three variables to store the camera's possession and then modify the variables and then finally change the angle of the camera:

        if key_map['up']:
            self.camY = self.camY + 0.09

        if key_map['down']:
            self.camY = self.camY - 0.09

        if key_map['right']:
            self.camX = self.camX + 0.09

        if key_map['left']:
            self.camX = self.camX - 0.09

        if key_map['space']:
            self.camZ = self.camZ + 0.09

        if key_map['shift']:
            self.camZ = self.camZ - 0.09


# sets the camera posision
        self.camera.setPos(self.camX, self.camY, self.camZ)

just to say I used a key map and then adjusted the key map according to the keys pressed, but instead you can turn all the if statements into functions

0
error 1044 On
def forward(self):
   self.cam_x = self.cam_x + 1
   self.camera.setPos(self.cam_x, 0, 0)

in the init you need to set a variable of

self.cam_x = 0

This will increment cam_x by one and then set the camera position to cam_x