Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Calculating angles from those Sin or Cos values

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I am making a tank for a 3D game. It is looked upon from a 3rd person perspective. I use the mouse cursor to move around a targeting cursor on the terrain. I want the turret of the tank to rotate so that it points at the cursor. Basically I have a right angled triangle with the lengths of all the sides. If I could work out the angle to the cursor then I can just use that in glRotatef to rotate the turret. I am stuck at the moment and I''m getting confusing results. Can anyone help me with this problem. Thanks in advance.

#### Share this post

##### Share on other sites
Advertisement
try this:

alpha=atan((z1-z0)/(x1-x0))
betha=atan((z1-z0)/(y1-y0))

take care when the argument is 90 because the tanget at 90 is infinity , x1-x0 and y1-y0 are the lenght and width in pixel of the amount the cursor is moved from the origin that is at x0,y0
place it in the middle of the screen, let me know if this works
basically is a 2d problem if i got it right , because you move the cursor with mouse and you don''t perform any movement ''inside'' the 3d space , am i right ?

#### Share this post

##### Share on other sites
Well yes and no. There is a quad that moves around on the floor as you move the mouse. Anyway, I tried te method you had shown me, and I get a flickering from left to right. Dunno if I''m doing something wrong or not, but it''s not working.
Thanks for youre help.

#### Share this post

##### Share on other sites
If I got it right, your cursor generates a quad with a 3D position on the landscape.
If you do the same for the tanks aim then you get another 3D position on the landscape.

Calculate the vector from the tank to the tanks aim:
CurrentAimVect = TankAimPos - TankPos

Calculate the vector from the tank to the quad:
TargetAimVect = QuadPos - TankPos

Normalise both vectors.
The Dot product between these two vectors gives you the cosine of the angle:
CosAngle = CurrentAimVect . TargetAimVect

Get the angle:
Angle = acos(CosAngle)

You can now use this angle in glRotatef. The axis to use should be the tank up vector.
Notice that this should work well only if the tank is horizontal.
If the tank is tilted on its side then it would be a bit more compilated.

Hope this helps.
Let us know if it works or not.

Edited by - lusinho on November 15, 2001 1:55:23 PM

#### Share this post

##### Share on other sites
Get the angle:
Angle = acos(CosAngle)

These questions from the triginometry challenged ...

What if CosAngle is greater than 90 degrees ?

Isn''t cosine positive in 2 quadrants and negative in the other 2 quadrants ? If the cosine of the angle is negative is there a way to determine which quadrant the angle "resides" in ?

#### Share this post

##### Share on other sites
Quadrants and signs for trig

|
3 | 2
-------
4 | 1
|

in 2 cos,tan & sin are +
in 1 cos is +, the rest -
in 4 tan is +, the rest -
in 3 sin is +, the rest -

just your basic trig rules, nothing more or less. If you can use these some way, cool.

#### Share this post

##### Share on other sites
Use atan2( x, y ), not atan( x/y ). It will put it in the right quadrant.

#### Share this post

##### Share on other sites
quote:
Original post by lordjarl
Quadrants and signs for trig

I always thought the +x, +y Cartesian quadrant was referred to as Q1, not 2, and they progressed CCW from there...

#### Share this post

##### Share on other sites
Well, if you label as above you have to remember CAST (Cos All Sin Tan) else you have to remember ASTC.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631434
• Total Posts
3000050
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!