Archived

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

RedZen

Calculating angles from those Sin or Cos values

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


Link to post
Share on other sites
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites