Camera Rotation

I have looked around for a couple hours now, and haven’t been able to find anything that was helpful, I am new, so please be patient.

I am trying to get the camera to rotate around an object. Essentially, when the mouse moves left and right, I want the camera to rotate around the z axis, and when the mouse moves up and down, I want the camera to rotate around the x axis, always keeping the player in the center, similar to EVE Online (sorry, only example I could come up with off the top of my head).

Currently, I am getting the mouse movement with this bit of code:

x = base.win.getPointer(0).getX()
y = base.win.getPointer(0).getY()

and assigning them like this:

if(base.win.movePointer(0, 100, 100)):
                    self.heading = self.heading - (x - 100)*0.2
                    self.pitch = self.pitch - (y - 100)*0.2

I am then using different combinations of setPos and setHpr values, but they are not really working like I want them to. What would be the correct way to accomplish this?

-cD

I have no much experience with reading the mouse movements manually so I can’t say if you are catching well the mouse motion or not. If you are not sure You can try calling the rotating code with key strokes just to see if it works. (Don’t forget the mouse mess the camera unless you disable the task with base.disableMouse or something like that)

If that’s ok and the problem is in the rotating code, I think I can guess what maybe wrong.

when you do that: self.heading = self.heading - (x - 100)*0.2
you are moving self (I assume the camera) relative to render, not to the player (or ship) So, unless another unposted task moves the camera too, what I would do is:

  • Make a dummy node and reparent to player or ship
  • Reparent the camera to that dummy node
  • when you want to rotate the camera, rotate the dummy node and important make the camera lookAt the player/ship

Well outside of interpreting the mouse info (thats a problem in itself); if you want the camera to be controlled (without LERPS i am assuming), you are going to have to do a bit of math (imo)

I am not sure how familiar you are with trig and planar math, but ill try to explain the best i can.

Essentially what you want here, is the camera is free to move on a sphere of radius r; around the object you are focused on, and making sure the camera is oriented such that is looking at it; long story short, after the long math for the position of the camera; simple call your cameraNode.lookAt(objectNodePath).

The math here is a bit tricky but lets take the simple case first; say that we lock the camera on the xy plane around the object whos position is (0,0,0). We want the camera to stay on the circle laying on the xy plane whos radius is r; if we want to adjust the location of the camera thus the cameras position becomes

base.camera.setPos(radius * cos(angle), radius * sin(angle), 0)
base.camera.lookAt(myObjectNodePath)

where the angle is being adjusted by some means (likely a task).

Now if you also want to be able to move around the sphere (not just on a planar circle); It seems logical to approach the problem with spherical coordinates:

 http://en.wikipedia.org/wiki/Spherical_coordinates

With spherical coordinates we have two angles; one being the angle around the sphere (zenith) , and the other being up the sphere (azimuth). with a little bit of math to foodle with the zenith and azimuth values; the following code can be used to convert spherical coordinates to XYZ position:

base.camera.setPos(radius * sin(zenith)*cos(azimuth), radius * sin(zenith)sin(azimuth), r*cos(zenith))
base.camera.lookAt(myObjectNodePath)

This clearly will take a bit of foodling if you aren’t familiar with trig and spherical/polar coordinates; but its how i approached a similar problem.

EDIT: After posting realized this type of movement can be achieved by creating a dummy node and setting a pos respective to that then rotating; with distance r away from it; after which call the base.camera.lookat(objectNode) function. But hey trig is so much more fun! :smiley:

If you need some more help; feel free to ask.
Roger

1 Like