Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Pitch/Yaw to Directional Vector  (Read 2651 times)
0 Members and 1 Guest are viewing this topic.
Offline Agro
« Posted 2012-11-16 05:10:04 »

This calculation can take a pitch and yaw value and convert it into a directional 3D vector.

https://gist.github.com/4083992

theagentd simplification(recommended)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// pitch and yaw are in degrees
Vector3D getDirectionVector() {
   double pitchRadians = Math.toRadians(pitch);
   double yawRadians = Math.toRadians(yaw);

   double sinPitch = Math.sin(pitchRadians);
   double cosPitch = Math.cos(pitchRadians);
   double sinYaw = Math.sin(yawRadians);
   double cosYaw = Math.cos(yawRadians);

   return new Vector3D(-cosPitch * sinYaw, sinPitch, -cosPitch * cosYaw);
}


In this circumstance, pitch and yaw are easier to keep in degrees, personally. But for improved performance, by all means, translate this to go to radians(just put pitch or yaw instead of  (pitch or yaw) * PI / 180)

Offline theagentd
« Reply #1 - Posted 2012-11-16 11:48:15 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// pitch and yaw are in degrees
Vector3D getDirectionVector() {
   double pitchRadians = Math.toRadians(pitch);
   double yawRadians = Math.toRadians(yaw);

   double sinPitch = Math.sin(pitchRadians);
   double cosPitch = Math.cos(pitchRadians);
   double sinYaw = Math.sin(yawRadians);
   double cosYaw = Math.cos(yawRadians);

   return new Vector3D(-cosPitch * sinYaw, sinPitch, -cosPitch * cosYaw);
}


1 less cos() call, a lot of fewer multiplies and divides...

Myomyomyo.
Online Roquen
« Reply #2 - Posted 2012-11-16 11:53:37 »

My recommendation is to not use euler angles (at least at runtime) to represent orientations.  Likewise for degrees (use radians).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-11-16 12:50:17 »

It's practical for a first person camera?

Myomyomyo.
Online Roquen
« Reply #4 - Posted 2012-11-16 13:53:44 »

I personally won't, but yeah that would be an example of a reasonable usage.  Another would be flight simulation.
Online Roquen
« Reply #5 - Posted 2012-11-16 17:00:59 »

Follow up:  remember when you're talking about cameras, then getting any of: forward, right & up vectors from the orientation is the same as converting it to a matrix which you'll be most likely doing anyway.  Of course this is true for any orientation that is going to get converted to matrix form.
Offline Agro
« Reply #6 - Posted 2012-11-16 23:24:28 »

I'll modify the code to what theagentd posted. Smiley I'm not much in the habit of simplifying code/equations if you saw my previous codes.

Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-11-16 23:34:35 »

So.....err......what exactly do you use this Vector for? persecutioncomplex

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-11-16 23:39:49 »

So.....err......what exactly do you use this Vector for? persecutioncomplex

Think of a minecraft game.

You have a first person camera with yaw and pitch.

Then you want to get the block you click on. You create yourself this vector from your camera's yaw and pitch, and then raycast the vector from the mid of your camera's / eye's position forward, until it hits a block (or doesn't), and then you can mine it...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-11-16 23:42:21 »

Oh! Right I knew that......

*slaps himself silly a few times*

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

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2012-11-16 23:43:17 »

Oh! Right I knew that......

*slaps himself silly a few times*

Always glad to be able to help Grin

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!