Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) 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 3538 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

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « 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
 « 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

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2012-11-16 11:50:17 »

It's practical for a first person camera?

Myomyomyo.
Roquen
 « 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
 « 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: 367
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: 121
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: 367
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: 121
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

You cannot reply to this message, because it is very, very old.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (38 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (57 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 LiquidNitrogen 21x BurntPizza 21x basil_ 20x KevinWorkman 18x EgonOlsen 17x theagentd 16x Roquen 16x 65K 11x wessles 10x Varkas 10x princec 9x Riven 9x SHC 8x Ashedragon 8x Ecumene 7x nsigma 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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