Camera setDirection not working?

hey, trying to execute this line:


gsMain.getCamera().setDirection(new Vector3f(0.0138f, -0.479f, -0.877f));



while im setting up my project, but for some reason the camera direction always becomes (0,0,1) when the scene is done loading.  How do I fix this?  (the camera position sets properly, its just looking the wrong way.  Also camera will be fixed for the whole program, so no need to move it around).

I don't really understand the camera code and hit the same problem a while ago. Never did really work out how to use setDirection, but managed to get round it by using the cam.lookAt( … ) or cam.setFrame( … ) instead.



e.g.

    gsMain.getCamera().lookAt(new Vector(…), Vector3f.UNIT_Y);



or

    gsMain.getCamera().setFrame(cam.getLocation(), new Quaternion().fromAngles( x, y, z ));

The issue is that by setting just the direction vector the left and up are not correctly update.



The lookAt() method will adjust those vectors for you; to look in a direction simply create the direction vector then multiply it by a 'large' number:


final Vector3f lookAtLocation = new Vector3f( 0.0138f, -0.479f, -0.877f ).normalizeLocal().multLocal( 1000 );



Couple of followup questions if I may as the whole maths side of 3d is the bit I struggle with most so always looking for a good explanation of stuff that doesn't make sense to me :slight_smile:



What is the purpose of the up and left vectors for the camera object?  As I see it all a camera really needs is a position to place it and a direction to point it in so it seems the setLocation and the setDirection should be enough?



In your (basixs) example above, when creating the "look at" vector, what is the purpose of the normalize and scale step as it seems to work just fine without it? (or is it just to give better precision in later calculations?)

In your (basixs) example above, when creating the "look at" vector, what is the purpose of the normalize and scale step as it seems to work just fine without it?

That's because it was a normalized vector to begin with (which directions should be anyways, so basically its just a 'safety'); one could supply (100, 0, 100) which normalized would be (0.5, 0, 0.5).
But basically its doing this: normalizing the vector (length == 1), then multiplying it to 'add' the distance. 

This 2 are not the same:


new Vector3f( 100 0, 100 ).normalizeLocal().multLocal( 1000 );   -> ( 500, 0, 500 )
new Vector3f( 100, 0, 100 ).multLocal( 1000 );  -> ( 10,000, 0, 10,000 )



As I see it all a camera really needs is a position to place it and a direction to point it in so it seems the setLocation and the setDirection should be enough?

Allow the camera to 'roll' in your mental image of how the camera is working and you should see why they are needed.

Just use



.LookAt



works very easy,

ok figured out what my particular problem was.  I was using camera and cameraNode, and I didn't call cameraNode.UpdateFromCamera().  Everything works peachy now.