Rotate around a center point

I am trying to get my camera to rotate around center point in my level.

#get distance to center
distance = self.GetDistanceToCenter() * -1;

#move camera to center
self.SetPos(self.__centerX, self.__centerY, self.__centerZ);

#rotate the camera around the world's z(up) axis
self.SetHprRelative(self.__sideDirection * self.__xHprSpeed * time, 0, 0);

#translate the camera back to the same distance it was before relative to its own coordinate axis
self.SetPosRelativeSelf(0, distance, 0);

This is behaving as a I would expect but I do not think I did this in a good way. I am translating, rotating, and translating again when all I really need to is rotate. This seems too processor intensive for my needs.

how about

yourcamera.setHpr( yourLevelNode, h,p,r)

this should rotate the camera relative to yourlevelnode.

I have actually tried something similar to what your code is. I just couldn’t get it to work how I expected it to.

Is my way a bad way of doing it? I would think since I am performing multiple translations/rotations, that the camera would keep flickering between positions but it is not. I’m guessing this is because the camera’s new location is not actually drawn after each translation/rotation, but rather, sometime after my code is executed.

I have used similar methods to this in XNA and OpenGL so that is why I defaulted to it. I just want to be sure that this is still a good way of doing it with Panda3D.

It’s not a terrible way to do it. You’re right about the absence of flickering: the frame is rendered only when the igloop task is executed, which is after all of the other tasks have run; the frame is not rendered in the middle of your task.

But Panda’s scene graph gives you an easier way. The idea is to have an intervening node which provides the offset you need. Then you can just parent your camera to this other node, and when you rotate the camera, it will rotate about its parent’s offset. This way, you need to do only one step, not three.

The relative rotation that Thomas suggests is used to solve a different problem; it’s not related to changing the point of origin of rotation.



I enacted a camera system similar to what I think you want to accomplish using the technique which drwr pointed out. I imagined the camera being on a boom attached to a pivot. Here’s my code:

cameraPivot = render.attachNewNode("CameraPivot")
cameraArm = cameraPivot.attachNewNode("CameraArm")

## zooming in/out by dr:
cameraArm.setPos(cameraArm, 0, dr, 0)

## swivelling camera in place

## rotating around center

It also has the added bonus that you can change where the center is, and keep the camera trained on an object while swinging by moving the pivot and setting the arm to cameraArm.lookAt(…), etc.

Hope that helps,