# Sin, Cosin, Trigonometry

This topic is 2747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I'm trying to program a simple navigable 3d World.
I've got a Camera class that has 2 variables: directionX and directionZ.

When I try to move the camera around, I send a value (ex. 10) and the value is multiplied by the directionX and Z (ex. 0.5 and 0.2), giving me how many units I should move the camera on the X and Z (X being horizontal and Z being depth).

I'm having trouble calculating the directions values after a rotation has been detected, how do I do it?
The camera has a rotationX and rotationY value which is in angles (positive/negative), and I'm trying to get the direction values from this.

For example, if the camera has been rotated -45 degrees (left), the directionZ (depth) should be 0.5 and the directionX (horizontal) should also be 0.5.
If the camera has been rotated 30 degrees (right), the directionZ should be 0.677... and the directionX should be 0.333...

I've been looking for sin and cosin, their values ranging from -1 to 1 fits exactly what I need, but for some reason, my code isn't working as it should (or my theory is messed up, or both).

Any help is appreciated.

##### Share on other sites
You can transform you right and look vector by the rotated matrix, that would give you a new vector which is pointing in the proper direction for both the x and z axis.

##### Share on other sites

Hi,

I'm trying to program a simple navigable 3d World.
I've got a Camera class that has 2 variables: directionX and directionZ.

When I try to move the camera around, I send a value (ex. 10) and the value is multiplied by the directionX and Z (ex. 0.5 and 0.2), giving me how many units I should move the camera on the X and Z (X being horizontal and Z being depth).

I'm having trouble calculating the directions values after a rotation has been detected, how do I do it?
The camera has a rotationX and rotationY value which is in angles (positive/negative), and I'm trying to get the direction values from this.

For example, if the camera has been rotated -45 degrees (left), the directionZ (depth) should be 0.5 and the directionX (horizontal) should also be 0.5.
If the camera has been rotated 30 degrees (right), the directionZ should be 0.677... and the directionX should be 0.333...

I've been looking for sin and cosin, their values ranging from -1 to 1 fits exactly what I need, but for some reason, my code isn't working as it should (or my theory is messed up, or both).

Any help is appreciated.

type sin 45 and you'll soon see why.
remember that the length of a line r = sqrt(z*z + x*x); so your z and x direction should be sqrt(0.5) and -sqrt(0.5) for a 45 degree angle.

##### Share on other sites

type sin 45 and you'll soon see why.
remember that the length of a line r = sqrt(z*z + x*x); so your z and x direction should be sqrt(0.5) and -sqrt(0.5) for a 45 degree angle.

sin(45) is resulting a 0.707, the same as sqrt(0.5).
The values aren't matching what I was expecting (I expected a 0.5 for a 45 degree), so now I'm getting the angle value and dividing by 90.
The result decimals are exactly what I need, but I'm having trouble with the signals.
When it's more than 0 and less than 90, it's + for X and Z.
When it's more than 90 and less than 180, it's + for X and - for Z.
etc

I remember vaguely having classes about this, but it's been so long I don't even remember what it was about... I thought there was a simple way to calculate the value with just the angle, because I didn't want to code alot of ifs and elses or math heavy equations... but I guess it's not that simple at all.

You can transform you right and look vector by the rotated matrix, that would give you a new vector which is pointing in the proper direction for both the x and z axis.

Hmm, sorry, could you explain in newbie terms?

##### Share on other sites
Why would you expect (0.5, 0.5) for a 45 degree angle? That vector is not normalized to unit length, so it would be incorrect to use, resulting in slower-than-expected movement.

Given a 2D vector and an angle, the vector can be rotated by the angle using:

 NewX = cos(angle)*x - sin(angle)*y NewY = sin(angle)*x + cos(angle*y 

So if you rotate the vector (1,0) by 45 degrees (sin(45)=0.7071067812, cos(45)=0.70710678), then you end up with:

 NewX = cos(45)*1 - sin(45)*0 = cos(45) = 0.7071067812; NewY = sin(45)*1 + cos(45)*0 = sin(45) = 0.7071067812; 

Now, the vector (0.5,0.5) does represent the correct direction, but a rotated unit-length vector as (1,0) is will also be a unit-length vector, and (0.5,0.5) is not unit length. It is actually sqrt(0.5*0.5 + 0.5*0.5), or 0.7071067812 in length, so any movement done using that vector would be slower than expected.

##### Share on other sites

[quote name='Burnt_Fyr' timestamp='1319992755' post='4878604']
type sin 45 and you'll soon see why.
remember that the length of a line r = sqrt(z*z + x*x); so your z and x direction should be sqrt(0.5) and -sqrt(0.5) for a 45 degree angle.

sin(45) is resulting a 0.707, the same as sqrt(0.5).
The values aren't matching what I was expecting (I expected a 0.5 for a 45 degree), so now I'm getting the angle value and dividing by 90.
The result decimals are exactly what I need, but I'm having trouble with the signals.
When it's more than 0 and less than 90, it's + for X and Z.
When it's more than 90 and less than 180, it's + for X and - for Z.
etc

I remember vaguely having classes about this, but it's been so long I don't even remember what it was about... I thought there was a simple way to calculate the value with just the angle, because I didn't want to code alot of ifs and elses or math heavy equations... but I guess it's not that simple at all.

You can transform you right and look vector by the rotated matrix, that would give you a new vector which is pointing in the proper direction for both the x and z axis.

Hmm, sorry, could you explain in newbie terms?
[/quote]

So rather than storing a direction X, and a direction Z, lets store a 3d vector, look, instead.
[source]
Vector3 look;
[/source]

Instead of using direction x and direction z to adjust your position, we'll just use the look vector.

[source]
position += look * distance;
[/source]

Now, when you want to turn, you can rotate the look vector, using the formulas JTippits has posted, or via a matrix math library.

[source]
look = Rotate(look, YAxis, 5/180*pi);
[/source]

next up, bonus points for you to do the same for a right vector.
next, how about an up vector?

Now that you have right, up, and look vectors, place them in rows, and add your position as a forth row, surprise, You now have a world matrix.

##### Share on other sites
What i am trying to say is that if you have a look vector of (0,0,-1) and you have a rotated matrix that has a Yaw rotation of 90 degrees for example. By transform the look vector by that matrix assuming counter clokwise is positive then the new look vector would be (-1,0,0). So all in all transform your current look vector by a matrix will rotate that vector to point in a direction that is relative to the matrix.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 22
• 11
• 25