# Rotating camera around a point.. Moving towards a point

I've been looking at the Third Person Handler files, and searching the forums… I can't figure this out!

Okay, maybe it's my complete lack of understanding of Quaternions…

I have a Box, center is say, 5,5,5.

How do I move the camera around it? I'm trying to setup the A and D keys to rotate around the center of the box.

I then want to have Q and Z zoom in and out towards the center.

I'm assuming this is quite easy… Cam just seems to have different functions than Nodes… and Quaternions confuse me so much. Does it take the rotation amount? Can it be relative rotation though?

Also, I know about the lookAt function of the camera, is there a moveTowards function?

Since you want to rotate around a place, I assume you also want to be always looking at that point… What you need is to create a controller that has an angle and distance from that point…

```Vector3f center; //Initialize as desired. float angle, distance; //Angle and distance to position the camera WRT center. //On 'A' angle -= 0.1f; //Or something similar //On 'D' angle += 0.1f; //Or something similar //On 'Q' distance -= 0.1; //On 'Q' distance += 0.1; cam.setLocation( center.x + distance*FastMath.cos( angle ), center.y, center.z + distance*FastMath.sin( angle ) ); cam.lookAt( center, Vector3f.UNIT_Y );```

Hope it helps.

create a camera node and attach ur camera to it.

use spatial transformer and a pivot node to rotate ur camera node.

neakor said:

create a camera node and attach ur camera to it.

use spatial transformer and a pivot node to rotate ur camera node.

That sounds easier than using trig, but how do I do so? Pivot node?
dingobiatch said:

neakor said:

create a camera node and attach ur camera to it.

use spatial transformer and a pivot node to rotate ur camera node.

That sounds easier than using trig, but how do I do so? Pivot node?

CameraNode n = new CameraNode(camera);
n.setLocalTranslation(new Vector3f(x,y,z)); // This x, y, z defines the rotation radius.
Node pivot = new Node("Pivot");
pivot.setLocalTranslation(new Vector3f(x,y,z)); // This x,y,z defines the center location of rotation.
pivot.attchChild(n);

then the rest is just set up ur rotation using spatial transformer and quterion.
1 Like

I don't understand quaternions at all so I couldnt use the example with the pivot node. I did however use the trig method and it works perfectly. Still, would anyone be able to explain that last example a little more? Or point me to a good tutorial? (not the starter tutorials…)

Take a look at jmetest.TutorialGuide.HelloAnimation, it uses this approach.

Hi there…
I had a similar problem but did not find a good answer.
I’ve written a class that doesn’t use quaternions at all.
I use the mouse to control the camera, like in a strategy game, but you can assign it other inputs of course.

I have posted my solution here:
hub.jmonkeyengine.org/t/rotating-the-camera-around-a-point-object-solved/31458