# Camera Movement Relative to Rotation

Ok, this has been driving me nuts- how do you move the camera in the direction it’s facing? I have looked at the Normal Mapping tutorial, but I don’t understand how the camera movement works. I have tried moving the camera with sin and cos, but the results could have easily given someone a seizure… Here is a snippet of what I was trying to do. I’m foggy on my trig, so I wouldn’t be surprised if I made a blundering mistake.

``````    def keyboardTask(self,task):
h=base.camera.getH();print h
x=(self.mvX)*(math.cos(h))
y=(self.mvY)*(math.sin(h))
base.camera.setPos(base.camera.getX()+x,
base.camera.getY()+y,
base.camera.getZ()+self.mvZ)

mvX, mvY, and mvZ are either -1, 0, or 1 depending on the key and if it’s pressed.

Any helpful advice will be greatly appreciated! I think that your problem lies in, as I recall, the sin and cos functions taking angles in radians and the getH method returning an angle in degrees.

The simplest solution is probably to convert the value that you get from getH to radians using the deg2Rad function before passing it to the sin and cos functions, like so:

Trigonometry isn’t necessary to do this, if you just want to dolly the camera you just need to move it on Y relative to itself.

``base.camera.setY(base.camera, <value>)``

If you want to move it only on the direction its facing on the XY plane, you can follow the code from roaming ralph, which uses matrices to calculate the movement.

``````forward = base.camera.getNetTransform().getMat().getRow3(1)
forward.setZ(0)
forward.normalize()
base.camera.setPos(base.camera.getPos() + forwards<* multiplier>)``````

Thanks for both of your replies!

Thaumaturge:
I didn’t know that sin and cos required radians in Python- very good information to know for the future-

ZeroByte:
That made life sooo much easier! Thanks for the quick reply- it compressed my block of script down to a few lines!