Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (763) Games in Android Showcase (229) games submitted by our members Games in WIP (852) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Pitch/Yaw to Directional Vector  (Read 9353 times) 0 Members and 1 Guest are viewing this topic.
Agro
 « Posted 2012-11-16 04: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 degreesVector3D 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)

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

 1  2  3  4  5  6  7  8  9  10  11  12 `// pitch and yaw are in degreesVector3D 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.
Roquen

JGO Kernel

Medals: 517

 « Reply #2 - Posted 2012-11-16 10:53:37 »

My recommendation is to not use euler angles (at least at runtime) to represent orientations.  Likewise for degrees (use radians).
theagentd
 « Reply #3 - Posted 2012-11-16 11:50:17 »

It's practical for a first person camera?

Myomyomyo.
Roquen

JGO Kernel

Medals: 517

 « Reply #4 - Posted 2012-11-16 12:53:44 »

I personally won't, but yeah that would be an example of a reasonable usage.  Another would be flight simulation.
Roquen

JGO Kernel

Medals: 517

 « Reply #5 - Posted 2012-11-16 16: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.
Agro
 « Reply #6 - Posted 2012-11-16 22:24:28 »

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

ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

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

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

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

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

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

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
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

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

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

*slaps himself silly a few times*

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

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

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

*slaps himself silly a few times*

Always glad to be able to help

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

 EgonOlsen (541 views) 2018-06-10 19:43:48 EgonOlsen (645 views) 2018-06-10 19:43:44 EgonOlsen (442 views) 2018-06-10 19:43:20 DesertCoockie (788 views) 2018-05-13 18:23:11 nelsongames (1019 views) 2018-04-24 18:15:36 nelsongames (1006 views) 2018-04-24 18:14:32 ivj94 (1587 views) 2018-03-24 14:47:39 ivj94 (560 views) 2018-03-24 14:46:31 ivj94 (1393 views) 2018-03-24 14:43:53 Solater (566 views) 2018-03-17 05:04:08
 SkyAphid 17x orange451 17x Spasi 10x beeaware 8x nsigma 7x dime26 7x philfrei 6x Mad Hatter 5x LeoOno 4x KaiHH 4x FabulousFellini 4x ral0r2 4x Sickan 4x jezek2 3x lacombej 3x SHC 3x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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