# Look At function

Hi all,

I was wondering if anyone can help me with some maths…

I need to look at a point in space, i can find the direction vector by subtracting the point from the camera position. But then im stuck at the left and up vector. Also, i dont want the camera to roll, so that everything stays nice and explanative for the player. I know if I can find one of the vectors i can cross the direction and that vector to obtain the 3rd…but finding one of those vectors is very illusive for me…

Any ideas are welcome…

DP

isn’t there a lookAt func in Camera?

Camera.lookAt does not compute the up/left vectors, which are needed for some things (e.g. camera motion handlers from jME itself).

To compute the up vector I recommed to consider it to be orthographic to the direction (direction dot up = 0) and then contrain the coordinates by the latter/usual camera orientation. As length could be normalized afterwards you can consider one of the values to be 1 if you are sure it is not 0 (or you can add the condition x^2+y^2+z^2 = 1 to have them normalized already in the first computation, the direction must be already normalized in this case).

So the general equation is: xvx+yvy+zvz = 0

where [vx,vy,vz] is your direction and [x,y,z] is up.

E.g. y could be kept at 0 and z positive to have the user see the z axis pointing up, so z is considered to be 1. You would end up with:

z = 1

y = 0

x = -vz/vx

afterwards normalize [x,y,z] and compute the left vector.

Edit: not recommended:

(If you put in the second equation instead you get:

z = sqrt( 1 - x^2 )

y = 0

x
vx + sqrt( 1 - x^2 )*vz = 0

which will give you quite some headache to solve )

and im gonna be doing this every frame…lol!

Got a better idea…use Quaternions to help me solve it.

Translate the position the camera is looking at to the same plane the camera is in. This would mean that the up vector is always (0, 1, 0). Compute the direction of the camera and cross the up with the direction to obtain the left vector.

Then, find the angle between the real look object and the camera’s up vector by dotting and doing acos(dot), then create a new quaternion with the rotational columns the same as the left, up and direction vectors.

Then rotate quaternion by theta around the left axis and set the camera’s direction and up to be that of the rotational matrix.

Sounds like a doosy, but id rather do less maths since I just did an exam about it and I’d rather forget any maths!

Regards,

DP

"DarkProphet" wrote:
and im gonna be doing this every frame...lol!

Yes that's exactly why I recommend the equation stuff. Computing acos and quaternions costs much more computing time than a single (or two) divisions.

Maybe you misunderstood me? I did not mean to solve the equation again and again but only once and then hardcode it...

Yup, maths isn’t exactly my strong point. I should get that maths book that renanse suggested. Your probably know abit more about this than me.

I never realised why games take so long to write…untill last night

Heh,

DP

Heh…as chaosdeathfish says…its the simplest solutions that always elude us:

``` // find the distance between the target and the camera, then       // normalise it       dirVec.set(targetPos).subtractLocal(cameraPos).normalizeLocal();       // cross the up and direction to find the left       absoluteUpVec.cross(dirVec, leftVec);              // cross the direction and left to find up       dirVec.cross(leftVec, upVec);          // set the axis of the camera as that.       cam.setAxes(leftVec, upVec, dirVec);       cam.onFrameChange(); ```

heh...lets all hang our heads in shame..

Renanse, maybe the lookAt function should be changed so that it has this code instead? It would be soo much easier...

DP :D
Renanse, maybe the lookAt function should be changed so that it has this code instead? It would be soo much easier...

No, your "absolute up" may not be the same as mine. The current lookAt function is appropriate.

absolute up is just "0, 1, 0"…Its the initial setting of the camera frame before any transformation has occured…

DP

(0,1,0) does not HAVE to be up though. In fact, very commonly it’s (0,0,1).

This worked for me:

Vector3f loc = new Vector3f(20.0f, 300.0f, 0.0f);

Vector3f dirY = new Vector3f(0.0f, -1.0f,0.0f);

cam.setFrame(loc,

new Vector3f(0,1,0).crossLocal(dirY).normalizeLocal(),

new Vector3f(1,0,0).crossLocal(dirY).normalizeLocal(),

dirY);

cam.update();

I think the current method lookAt should either be renamed or changed to do the above on input of a location and direction , as in other API’s i’ve seen it typically means “look at the object/direction in question”, doing all the relevant maths under the hood.

my 0.2 cents,sv

it’s basically gluLookAt, that’s why it’s named as such.

kk, np.

Maybe just a sticky to this thread or a new method?