Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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  
  Geting XYZ of a point along a line (vector)  (Read 505 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Member


Medals: 3



« Posted 2013-03-31 13:50:59 »

I know this may seem like a dumb question, but I'm having some odd problems when trying to calculate the position of a point in space along a vector.

My objective is to grab the XYZ coordinates of a point in space directly in front of the camera with a set distance.

I know the pitch, roll and yaw angles of the camera already, and my existing calculation looks like the following (angles in this example are originally stored as degrees, then converted into radians):

1  
2  
3  
4  
5  
6  
7  
float addX = 5  * (float)Math.sin(Math.toRadians(camera.yaw));
float addZ = 5  * (float)Math.cos(Math.toRadians(camera.yaw));
float addY = 5  * (float)Math.sin(Math.toRadians(camera.pitch));

float newX = camera.x + addX;
float newZ = camera.z + addZ;
float newY = camera.y + addY;


The problem I have is that the new point - which does project in front of the camera as intended - doesn't seem to work along the Y axis correctly. It 'clamps' between the angles of -45° to +45°.

So, rotating the camera around the Y axis (yawing) seems to work great - the point 'fires' off from the camera in the right direction - but the plotting of the point in around the camera's pitch arc is out of whack!

For reference, this code is intended to be used as a line-of-sight calculation for detecting what the player is looking at.

Any help would be greatly appreciated. Thank you!
Offline quew8

JGO Coder


Medals: 23



« Reply #1 - Posted 2013-03-31 15:56:56 »

Your problem is that you've forgotten that pitch effects x, y and z values. It's very hard to explain without a diagram so I'm afraid I'm not even going to try.
Anyway here is the code I think you want:

1  
2  
3  
4  
5  
6  
double pitchRad = Math.toRadians(camera.pitch);
double yawRad = Math.toRadians(camera.yaw);
float f = (float)Math.cos(pitchRad);
float addX = 5  * f * (float)Math.sin(yawRad);
float addZ = 5  * f * (float)Math.cos(yawRad);
float addY = 5  * (float)Math.sin(pitchRad);


The important bit is the times f. The rest is just me absent mindedly changing code.
Please do bear in mind that I have to draw this out at least 3 times every time I try to solve the problem and I still get it wrong sometimes.
Offline pixelprime

Junior Member


Medals: 3



« Reply #2 - Posted 2013-04-01 16:21:55 »

Thanks for taking the time to post an example. Yes, it appears I've missed this somewhat obvious factor here, so I'll give your modifications a try and report my findings back here.

Thanks again!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pixelprime

Junior Member


Medals: 3



« Reply #3 - Posted 2013-04-01 21:58:10 »

I just wanted to drop a final post on to say that the code worked a treat - thank you very much!

I modified my original XYZ projection code with the coefficient cosine / sine angles and everything now behaves as expected, thanks a bundle!
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.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (48 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!