Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (570) Games in Android Showcase (154) games submitted by our members Games in WIP (619) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Get direction of camera (player is looking at)  (Read 1232 times) 0 Members and 1 Guest are viewing this topic.
steg90

Senior Devvie

Medals: 1

 « Posted 2013-04-04 09:34:10 »

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:

 1  2  3  4  5  6  7 `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
pixelprime

Junior Devvie

Medals: 3

 « Reply #1 - Posted 2013-04-04 09:41:05 »

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):

 1  2  3  4  5  6  7 `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.
steg90

Senior Devvie

Medals: 1

 « Reply #2 - Posted 2013-04-04 09:59:27 »

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?

Steve
pixelprime

Junior Devvie

Medals: 3

 « Reply #3 - Posted 2013-04-04 12:34:04 »

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:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `glLoadIdentity();// <-- do your camera positioning / rotation stuff hereglPushMatrix();glTranslatef(newX, newY, newZ);   // move to the point projected in front of the cameraglDisable(GL_TEXTURE_2D);   // turn this off to make sure the un-textured sphere surface will draw correctlySphere 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 sectionsglEnable(GL_TEXTURE_2D);  // re-enable texturingglPopMatrix();`
steg90

Senior Devvie

Medals: 1

 « Reply #4 - Posted 2013-04-04 14:45:50 »

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 ;-)

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
pixelprime

Junior Devvie

Medals: 3

 « Reply #5 - Posted 2013-04-04 16:20:51 »

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.
steg90

Senior Devvie

Medals: 1

 « Reply #6 - Posted 2013-04-04 16:39:41 »

Hi,

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

 1  2  3  4  5 `float distfromcamera = 3.0f;  // this seems a good choicenewY = -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 :-)

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
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (24 views) 2015-04-16 10:48:47 Duke0200 (39 views) 2015-04-16 01:59:01 Fairy Tailz (28 views) 2015-04-14 20:13:12 Riven (30 views) 2015-04-12 21:36:37 bus hotdog (47 views) 2015-04-10 02:39:32 CopyableCougar4 (50 views) 2015-04-10 00:51:04 BurntPizza (49 views) 2015-04-06 22:06:58 ags1 (52 views) 2015-04-02 10:58:48 Riven (50 views) 2015-04-01 18:27:05 ags1 (68 views) 2015-03-31 10:55:12
 BurntPizza 24x theagentd 21x wessles 15x Rayvolution 12x kingroka123 11x 65K 11x alwex 10x KevinWorkman 9x kevglass 8x ra4king 8x phu004 8x chrislo27 7x Ecumene 7x Roquen 7x Hanksha 7x Olo 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org