Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (764) 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
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Pitch/Yaw to Directional Vector  (Read 9369 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).
 Games published by our own members! Check 'em out!
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*

 Games published by our own members! Check 'em out!
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 (591 views) 2018-06-10 19:43:48 EgonOlsen (694 views) 2018-06-10 19:43:44 EgonOlsen (478 views) 2018-06-10 19:43:20 DesertCoockie (831 views) 2018-05-13 18:23:11 nelsongames (1026 views) 2018-04-24 18:15:36 nelsongames (1083 views) 2018-04-24 18:14:32 ivj94 (1664 views) 2018-03-24 14:47:39 ivj94 (590 views) 2018-03-24 14:46:31 ivj94 (1501 views) 2018-03-24 14:43:53 Solater (568 views) 2018-03-17 05:04:08
 Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46Deployment and Packagingby philfrei2018-08-19 23:53:08Deployment and Packagingby philfrei2018-08-19 23:50:04Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39
 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