trigonometry!!!!!!!!!!!!!!
Ok, i need help, i am trying to make a space ship follow me on a 2d plane...
i know there is a function for cosine and sine and tan.. in math.h
but how do u do the other 1, like cos-1 and so on...
also i need to convert the radians to degrees after so if there is a function that just does it all for u that would be good....
Here is my code so far:
#define DEGTORAD(x) ((x)*0.0174532925199432957692369076848861)
#define RADTODEG(x) ((x)*57.2957795130823208767981548141052)
That is my converters...
E_direction = float(RADTODEG("Insert Function"(MposY/MposX)));
MposY\X is a float that stores the players position...
I want E_direction to store a angle in degrees that points to
the player from the computer player...
shit. just remembered i had to add the position of the computer player in there somwhere!
Oh well, any help would be apresiated!
----- ShCiPwA -----
Please correct me if I am wrong anyone, but, I think the commands are arcsin, arccos and arctan.
Brendan ''Locke'' Hennessy"I heard life sucks, so that''s why I don''t have one"
Well i have looked in MSDN and there are
sin()
sinh()
asin()
asin() would be arcsin and sinh would be sin hyperbaric
i dont know what either of them are.
maybe there is an easyer way to do it...
Ok i have 2 points on a 2d plane...
i need the angle from the first point to the second in degrees?
this would be needed heaps is graphics programming so can som1 help!
THanx
----- ShCiPwA -----
sin()
sinh()
asin()
asin() would be arcsin and sinh would be sin hyperbaric
i dont know what either of them are.
maybe there is an easyer way to do it...
Ok i have 2 points on a 2d plane...
i need the angle from the first point to the second in degrees?
this would be needed heaps is graphics programming so can som1 help!
THanx
----- ShCiPwA -----
----- ShCiPwA -----
You want to check out something called ''dot product,'' a concept from linear algebra / vector math.
If you have two points in a 2Dplane, that imples two vectors w.r.t. the coordnate system origin. You an easily calculate the angle between two such vectors by equating two expressions fro dot product and solving for the angle as follows.
Let the two pts be A (Ax Ay) and B (Bx By).
The DotProduct(DP) = MagA * MagB * cos Angle
where
MagA = (Ax^2 + Ay^2)^.5
MagB = (Bx^2 + By^2)^.5
and Angle is the angle between the two vectors A,B
But, DP also = (Ax*Bx + Ay*By), so equaing the two expressions and solving for cos Angle:
cos Angle = (Ax*Bx+Ay*By)/(MagA*MagB)
so, Angle = acos() of that.
Exceptions:
MagA=0 or MagB =0; that means, one point is at the origin(0,0), so the Angle is just atan(y/x)(ie, inverse tangent of (y/x)), but if x=0, then if y is positiive, the angle is 90 degrees, else if y is negative, the angle is -90 degrees. (Draw a diagram)
So, normally, you would need acos(), but for the exception, you may need atan(). Most mathlibs returns angles as radians.
A search of ''dot product'' on google will yeild ton of references. Another useful concept is ''cross product'', for when you want to calculate a line normal to/ perpendicular to a plane defined by two other lines. For a useful math course, check out ''linear algebra.''
regards,
fgb
If you have two points in a 2Dplane, that imples two vectors w.r.t. the coordnate system origin. You an easily calculate the angle between two such vectors by equating two expressions fro dot product and solving for the angle as follows.
Let the two pts be A (Ax Ay) and B (Bx By).
The DotProduct(DP) = MagA * MagB * cos Angle
where
MagA = (Ax^2 + Ay^2)^.5
MagB = (Bx^2 + By^2)^.5
and Angle is the angle between the two vectors A,B
But, DP also = (Ax*Bx + Ay*By), so equaing the two expressions and solving for cos Angle:
cos Angle = (Ax*Bx+Ay*By)/(MagA*MagB)
so, Angle = acos() of that.
Exceptions:
MagA=0 or MagB =0; that means, one point is at the origin(0,0), so the Angle is just atan(y/x)(ie, inverse tangent of (y/x)), but if x=0, then if y is positiive, the angle is 90 degrees, else if y is negative, the angle is -90 degrees. (Draw a diagram)
So, normally, you would need acos(), but for the exception, you may need atan(). Most mathlibs returns angles as radians.
A search of ''dot product'' on google will yeild ton of references. Another useful concept is ''cross product'', for when you want to calculate a line normal to/ perpendicular to a plane defined by two other lines. For a useful math course, check out ''linear algebra.''
regards,
fgb
Have a look at the atan2(x,y) function, which returns the angle (in radians) in the appropriate quadrant depending on x and y.
A = atan2( cos(A), sin(A) );
A = atan2( cos(A), sin(A) );
"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
My problem is not so much the fucnction yet...
On a 2d scale, i need to be able to find the direction at ANY 2 points...
Now correct me if i am rong but i dont think you can enter negative numbers into a sin function?
i need it to aim even if AI player is above me.
----- ShCiPwA -----
On a 2d scale, i need to be able to find the direction at ANY 2 points...
Now correct me if i am rong but i dont think you can enter negative numbers into a sin function?
i need it to aim even if AI player is above me.
----- ShCiPwA -----
----- ShCiPwA -----
quote:
On a 2d scale, i need to be able to find the direction at ANY 2 points...
direction = atan2( xB-xA, yB-yA );
quote:
Original post by ShCiPwA
Now correct me if i am rong but i dont think you can enter negative numbers into a sin function?
You are wrong.

"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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement