# Rotate camera with fixed lookAt

Hi guys,

I’m new to JME and I got stuck while trying rotate the camera around the origin and look at the same point the camera was facing before the rotation.

I got the rotation part of the problem working using:

Quaternion leftRotation = new Quaternion();

leftRotation.fromAngleAxis(-FastMath.HALF_PI/20, Vector3f.UNIT_Y);

Vector3f rotationResult = leftRotation.mult(cam.getLocation());

cam.setLocation(rotationResult);

but i can’t figure out the math to set the lookAtDirection right:

cam.lookAtDirection(???, new Vector3f(0,1,0));

Ideas would be appreciated.

You can get that vector before like this:

[java]Vector3f forward = new Vector3f(0,0,1);

cam.getRotation().multLocal(forward);[/java]

thank you for the fast answer normen.

Wouldn’t that be the same as retrieving the camera.getDirection() / not setting the direction after applying the rotation?

However, my question was not how to get the current camera direction (I should have been more clear).

I would like to set the lookAt (or lookAtDirection) to the same point that I was focusing before the rotation. Imagine that I could find out that the camera was facing the point (1,2,3). After the rotation, I want to set the camera to look at the exact point, not another point with the same direction.

Why don’t you just do cam.lookAt(position) then?

My problem is I can’t find a way to determine the coordinates of that point.

Let me try to explain with an example:

I start my app looking at the point (0,0,0). After that, the user can use the mouse to look at any direction. In that direction I will have infinite points.

When I press rotate, I would like for the camera to lookAt() to a point in the previous direction (a far away point to minimize the camera “jump”).

Sorry I don’t quite get what you want. I think you might want to use a “virtual cursor” that your camera looks at and that you move the camera around instead of rotating the camera around itself. Otherwise just imagine that “point” you’re looking for say 1 or 2 units before the camera (camLocation + camDirection x 2). So using the math tutorial you simply rotate the direction vector back… If you want the 0,0,0 point to be on the “direction ray” of the camera, use lookAt 0,0,0.

As soon as I think I understand what you are asking for, I read it again and lose it.

Can you maybe describe it in a different way? With more specifics?

See, for example, if I’m looking north and then I look east and then “rotate”… what am I supposed to be looking at? Nothing in the original view in the old direction is anywhere in my new view… no matter how far out you go.

For making the camera smooth i suggest using interpolation between the previous point and the new point you wish to look at.

For fixing the camera to a specific point and rotating around it you need to use your rotation code in combination with normen’s answer ‘camera.lookAt(VectorToLookAt)’

First of all, thanks again for your replies.

Just to clarify what I’m triying to do (since my descriptions are failing hard) and to help you understand:

The problem is that I don’t have a reference point to use the cam.lookAt() (represented by the red cross in my image), since the camera can be looking at any direction.

I believe normen gave me the solution for the problem:

Otherwise just imagine that “point” you’re looking for say 1 or 2 units before the camera (camLocation + camDirection x 2).

If I “imagine the point” to be 10000 units before the camera I will get a point to use with lookAt().

pedrosousa said:
First of all, thanks again for your replies.

Just to clarify what I'm triying to do (since my descriptions are failing hard) and to help you understand:

The problem is that I don't have a reference point to use the cam.lookAt() (represented by the red cross in my image), since the camera can be looking at any direction.

I believe normen gave me the solution for the problem:
Otherwise just imagine that “point” you’re looking for say 1 or 2 units before the camera (camLocation + camDirection x 2).

If I "imagine the point" to be 10000 units before the camera I will get a point to use with lookAt().

pedrosousa means camera has a joint...
ex: if a car turns left camera looks at leftside view of a car.. Am I right???
That was my problem to...
How could I do this?
Any hints there..

[patch]Help to solve the question above please. Thanks![/patch]

glaucomardano said:

so how can I solve it? Some hints how can I make it.

new—>project---->TestGame—>TestChaseCam.java/TestFancyCar.java

TestChaseCam.java/TestFancyCar.java

Camera there was look around the fancy car…

The problem what I mean is the camera follows always at the back of a fancy car but if fancy car turns left,

camera still looks behind it and also seen the side view of the fancy car…

Normally in car racing game, if car turns, camera looks at the side view and also looks at the back of the car.

I want to do is camera stays at the back of the car always and if car turns, camera stays at the back but camera also seen the side view of the car…

Hints with that.

Thanks… I will try it later…

i tested some and this work for me

edit: i see problem now, yes object it looking at is certainly not in center of rotation

edit2:

loc - location to move camera arround, 30f and 30f are a distance from this point

[java]

camera.setLocation(loc);

Quaternion leftRotation = new Quaternion();

leftRotation.fromAngleAxis((-FastMath.HALF_PI / 20) * rotation, Vector3f.UNIT_Y);

Vector3f rotationResult = loc.add(leftRotation.mult(new Vector3f(0f, 30f, 30f)));

camera.setLocation(rotationResult);

camera.lookAt(loc, Vector3f.UNIT_Y);

[/java]

Ok, I think I have a similar problem with the original one of this post.

I’m trying to make the camera rotate around the origin, but remaining to look at the same point it was looking at before the rotation.

In my tests, I’ve tried this:

[java]camera.lookAt(new Vector3f(30,30,30), new Vector3f(0,1,0));[/java]

And the camera pointed to a different point each time it was rotated. The following image illustrates what I’m talking about. At the left size you can see the result of applying the LookAt(…) method, and to the right you can find what I intend to accomplish:

http://i.imgur.com/IOeNc.jpg

Shouldn’t the point (30,30,30) be seen as the same scene point at all times?

Waht am I missing here?

Thanks!

I guess you are missing that when you look at a point the rotation for the cam is set to achieve that…? What you describe works just fine, look for example the TestPhysicsCar, when you move the cam in a circle using wasd its still always looking at the car.