Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Get direction of camera (player is looking at)  (Read 1078 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline 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.
Offline 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?

Thanks for  your help.

Steve
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 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();
Offline 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
Offline 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.

Click to Play


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

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!