Jump to content
  • Advertisement

Archived

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

kc_0045

direction to point?

This topic is 5154 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 posted this on the NeHe fourm and they told me to ask in here so ya heres my Q Hey im having some problems trying to make a function that well return the angle between to points i 2d like heres a eg directionpoint(0,0,0,-10); and it would return 90 thats if the y was going up :D is there anyway of doing this?

Share this post


Link to post
Share on other sites
Advertisement
I''ve had this problem before also. Use another point (some static point) to create a triangle. Then find the distance between all 3 points. Then, using some Trig, find the angles.

Share this post


Link to post
Share on other sites
There no such thing as an angle between two points. There is such a thing, however, as an angle between two vectors. If, for two points P1 and P2, you define the vectors V1 and V2 to be the vector from the origin to P1 and P2 respectively, then you're in business. Just as a quick reference, whenever someone says "the vector from A to B," that means you do B - A. The most straightforward method at this point is to use a few dot product equations, which are namely:

(V1.x * V2.x) + (V1.y * V2.y) = |V1||V2|cos( θ )

You know the values of the vector components, and can calculate the lengths of both vectors, so all you're left with is one unknown, the angle, and can easily solve for that.

[edited by - Zipster on April 12, 2004 3:47:01 AM]

Share this post


Link to post
Share on other sites
so, you are basically using point p0(0, 0) as the origin, and try to find the counter-clockwise angle of point p1(0, -10) form the horizontal direction. Like in a trigonometric circle.


inline float Pi()
{
static const float pi = atan(1) * 4.0f;
return pi;
}

inline float RadiansToDegrees(float a)
{
static const float rad_to_deg = 180.0f / Pi();

return a * rad_to_deg;
}

inline float AngleInDegrees(float x0, float y0, float x1, float y1)
{
float dx = x1 - x0;
float dy = y1 - y0;
float angle = RadiansToDegrees(atan2(dy, dx));
return angle;
}


note that, since you have your coordinates in screen space (minus y direction is pointing up on the screen), you''ll probably need to use

float dy = y0 - y1;

or the angle will be ''upside down''.

Share this post


Link to post
Share on other sites

  • Advertisement
×

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!