• Create Account

# Sin, Cosin, Trigonometry

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Danicco  Members   -  Reputation: 323

Like
0Likes
Like

Posted 29 October 2011 - 06:30 PM

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.

### #2BornToCode  Members   -  Reputation: 769

Like
0Likes
Like

Posted 29 October 2011 - 06:42 PM

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.

### #3Burnt_Fyr  Members   -  Reputation: 787

Like
0Likes
Like

Posted 30 October 2011 - 10:39 AM

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.

### #4Danicco  Members   -  Reputation: 323

Like
0Likes
Like

Posted 30 October 2011 - 11:16 AM

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?

### #5JTippetts  Moderators   -  Reputation: 6654

Like
0Likes
Like

Posted 30 October 2011 - 11:38 AM

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.

### #6Burnt_Fyr  Members   -  Reputation: 787

Like
0Likes
Like

Posted 30 October 2011 - 06:38 PM

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?

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


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

position += look * distance;


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

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


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.

### #7bzroom  Members   -  Reputation: 646

Like
0Likes
Like

Posted 30 October 2011 - 06:44 PM

Unit circle chart: http://en.wikipedia.org/wiki/File:Unit_circle_angles_color.svg

sin( 30 deg ) = 0.5

### #8BornToCode  Members   -  Reputation: 769

Like
0Likes
Like

Posted 12 April 2012 - 05:12 PM

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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS