Get direction of camera (player is looking at)

Hi,

I’ve got a camera set up with a vector for x,y,z position. I have the camera’s xrot and yrot (no z rotation is needed). How do I get the direction the player is looking?

I did try:


Vector3f dir = new Vector3f();//camera.getCameraPosition();
float yRotRads = (float)Math.toRadians(camera.getCameraYaw());
float xRotRads = (float)Math.toRadians(camera.getCameraPitch());
float cosY = (float)Math.cos(yRotRads);
dir.x = (float)Math.sin(xRotRads) * cosY;
dir.y = (float)-Math.sin(yRotRads);
dir.z = (float)-Math.cos(xRotRads) * cosY;

Reason I’m asking is I want to cast a ray from the direction the camera is facing. Am I over complicating this?! Sorry, my brain is a bit ‘mushy’.

Thanks

Since you know the current pitch and yaw of the camera, you can use the following calculation to obtain an XYZ position along the camera’s view vector (from the perspective of the center of the screen):



double cameraPitchInRadians = Math.toRadians(camera.getCameraPitch());
double cameraYawInRadians = Math.toRadians(camera.getCameraYaw());
float f = (float)Math.cos(cameraPitchInRadians);
			
float newX = distanceFromCameraYouWantToMeasure * f * (float)Math.sin(cameraYawInRadians);
float newZ = distanceFromCameraYouWantToMeasure * f * (float)Math.cos(cameraYawInRadians);
float newY = distanceFromCameraYouWantToMeasure * (float)Math.sin(cameraPitchInRadians);


You then add the ‘newX/newY/newZ’ positions to the current camera position to obtain the new XYZ position in space as projected from the camera.

If you’re doing line-of-sight calculations, you may want to iterate through this calculation a number of times with different ‘distance from camera’ values until you intersect with (or pass through) an object you want to ‘pick’ out of the scene.

I hope you find this helpful.

Hi,

Thanks for that.

I need to brush up on my trig I think, been a while.

How would I display a cube that is just in front of the camera as to determine where
player is so to speak?

Thanks for your help.

Steve

If you’re looking at simply placing an object in front of the camera (regardless of its current rotation / position), you’re fortunate in that the code I posted pretty much takes all that leg work out for you.

Just use the resulting newX / newY / newZ values to use as an anchor point for rendering a new cube (series of quads), or a quick and dirty to test it out would be to draw a gluSphere or something like that, which will be quicker.

In LWJGL, you can do it quickly during your render cycle with something like this:


glLoadIdentity();

// <-- do your camera positioning / rotation stuff here

glPushMatrix();
glTranslatef(newX, newY, newZ);   // move to the point projected in front of the camera

glDisable(GL_TEXTURE_2D);   // turn this off to make sure the un-textured sphere surface will draw correctly

Sphere s = new Sphere();    // not optimal to declare here, but this is just an example!
s.draw(2, 12, 12);              // draw the sphere with a radius of 2, with 12 segments and sections

glEnable(GL_TEXTURE_2D);  // re-enable texturing
glPopMatrix();


Hey thanks for that!

That works a treat, would you suggest or recommend any trig for me to look at, I really would like to understand how you came to the code you gave me :wink:

I was thinking in order to get the newY I would have to get the angle of rotation around X (Yaw) and do something like:

newY = sin(Pitch) * distanceFromCamera;
newZ = cos(Yaw) * distanceFromCamera;
newX = sin(Yaw) * distanceFromCamera;

Just struggling with how you added the the cos in yours?

I really need to draw this down or something to see it.

PS - I guess I also need to add the camera’s X,Y,Z position to the above?

Thanks

Yes, you’ll need to add the camera’s current position to the newly generated x,y,z values to get the destination position for the object you’re trying to draw in front of the camera.

Regarding the trig - remember that the X,Y and Z positions are all going to be affected by the current camera pitch.

In the diagram you can see how calculating the X position of the end of a line is directly affected by its angle. The same can be said in 3D. Your X and Z positions, extrapolated from your YAW angle, need to take the current pitch angle as a coefficient, since that too will affect its output.

To be honest, just by using the method described earlier to grab the XYZ position based off of the camera angles is the only code you’ll likely need to do this in future, since it seems to be a pretty standard method.

I hope you find this helpful.

Hi,

Many thanks for this. I did work this out on paper by drawing triangles and I got the following:


float distfromcamera = 3.0f;  // this seems a good choice
newY = -cameraPos.y + ( distfromcamera * Math.sin(cameraPitchInRadians) );
newX = -cameraPos.x + ( distfromcamera * Math.sin(cameraYawInRadians) );
newZ = -cameraPos.z + ( distfromcamera * Math.cos(cameraYawInRadians) );
renderCube(-newX, newY, newZ);

This works a treat :slight_smile:

Just pulled out one of my old trigonometry books - think a little reading will help me work these things out in future.

Many thanks again,
Steve