Calculating angles from those Sin or Cos values

Started by
7 comments, last by RedZen 22 years, 5 months ago
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.
Green and blue was the dimension Zen, until I went through it...
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 ?


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.
Green and blue was the dimension Zen, until I went through it...
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
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 ?
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.
Use atan2( x, y ), not atan( x/y ). It will put it in the right quadrant.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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...
Well, if you label as above you have to remember CAST (Cos All Sin Tan) else you have to remember ASTC.

This topic is closed to new replies.

Advertisement