Angle algorithm

Hi

I’m having lots of trouble coming up with a solution for this problem. I
have a player sprite and an enemy sprite. They are both located on a 2d
grid setup (each with x,y coords) - I need to find the angle between them.

I need this because they can both shoot at each other, they are using
"laser" weapons - where the ‘bullet’ is a straight line. I need the angle,
because I intend to use the excellant ‘rotoZoom’ library to rotate the
’bullet’ sprite according to the angle between the two sprites, so it looks
like the laser is in a straight line between them.

If I just fire the ‘bullet’, and they aren’t both on the same y coord then
it looks funny, like a straight line going at a angle.

I’ve spent ages searching the web for a algorithm to help me out - I came
across a DOT Product, but I’m not sure that’s what I want - can anybody help
???

Sorry if this is OT - but I’m out of ideas.

Thanks
David.

Hi,

You want the arc tangent (e.g. arctan(abs(opposite/adjacent)) )

For a good explanation see the following page:

http://www.sonify.tv/tutorials/lindsey/angle.html

Hope that helps.

From: owner-sdl@lokigames.com [mailto:owner-sdl at lokigames.com]On Behalf
Of Berry, David
Sent: Tuesday, July 03, 2001 1:11 AM
To: 'sdl at lokigames.com
Subject: [SDL] Angle algorithm
Importance: High

Hi

I’m having lots of trouble coming up with a solution for this problem. I
have a player sprite and an enemy sprite. They are both located on a 2d
grid setup (each with x,y coords) - I need to find the angle between them.

I need this because they can both shoot at each other, they are using
"laser" weapons - where the ‘bullet’ is a straight line. I need the angle,
because I intend to use the excellant ‘rotoZoom’ library to rotate the
’bullet’ sprite according to the angle between the two sprites, so it looks
like the laser is in a straight line between them.

If I just fire the ‘bullet’, and they aren’t both on the same y coord then
it looks funny, like a straight line going at a angle.

I’ve spent ages searching the web for a algorithm to help me out - I came
across a DOT Product, but I’m not sure that’s what I want - can anybody help
???

Sorry if this is OT - but I’m out of ideas.

Thanks
David.

Hint: atan2()

http://www.google.com/search?q=basic+trigonometry+tutorial&hl=fi&lr=

– Timo Suoranta – @Timo_K_Suoranta

Hi,

“Berry, David” <David.Berry at dcita.gov.au> wrote in
:

I’m having lots of trouble coming up with a solution for this problem.
I have a player sprite and an enemy sprite. They are both located on a
2d grid setup (each with x,y coords) - I need to find the angle between
them.

short answer: man atan2

longer: angle=atan2(ydelta,xdelta);

hope that helps,
Walter

“Berry, David” <David.Berry at dcita.gov.au> wrote in message
news:EEAD847E742DD51196550000E896881DA6363D at g5dccbr0ms01.dcita.gov.au

Hi

If I just fire the ‘bullet’, and they aren’t both on the same y coord then
it looks funny, like a straight line going at a angle.

I’ve spent ages searching the web for a algorithm to help me out - I came
across a DOT Product, but I’m not sure that’s what I want - can anybody
help
???

You spent ages? May I recommend www.google.com? Entering in “angle between
two points” got several relevant hits.

Your question is basic trigonometry. You should pick up a text on it if you
plan to do much games programming. I’d recommend taking a course in it if
you can.

If the ‘x’ distance betwen the points is 6, and the ‘y’ distance between the
ships is 4, than the distance or hypoteneuse between the ships is

sqrt( xx + yy )

or 7.2111.

taking the arcsine of (4/7.211) gives 33.69 degrees. (You may need to
convert the final angle from radians).
This is because the sine of an angle of a right triangle is defined as the
ratio of the opposite side of the triangle over the hypoteneuse of the
triangle. The arcsine function goes in ‘reverse’, so that is how you get
the angle from the ratio. Arcsine is also known as ‘inverse sine’.

Alternatively, you could take the arctan of (4/6) directly, because tangent
is defined as the opposite side over the adjacent side.

Trig functions are slow, so you will probably want to precompute them for
incremental angles and store the results in a table, and define your own
trig functions or macros which simply do a table lookup.

-Daniel

“Berry, David” wrote:

Hi

I’m having lots of trouble coming up with a solution for this problem. I
have a player sprite and an enemy sprite. They are both located on a 2d
grid setup (each with x,y coords) - I need to find the angle between them.

I need this because they can both shoot at each other, they are using
"laser" weapons - where the ‘bullet’ is a straight line. I need the angle,
because I intend to use the excellant ‘rotoZoom’ library to rotate the
’bullet’ sprite according to the angle between the two sprites, so it looks
like the laser is in a straight line between them.

If I just fire the ‘bullet’, and they aren’t both on the same y coord then
it looks funny, like a straight line going at a angle.

I’ve spent ages searching the web for a algorithm to help me out - I came
across a DOT Product, but I’m not sure that’s what I want - can anybody help
???

Sorry if this is OT - but I’m out of ideas.

Thanks
David.
-------------- next part --------------
#include <math.h>
#include

class Point
{
public:
Point(int nx, int ny)
: x(nx), y(ny)
{ }
int x;
int y;
};

double angle(const Point& first, const Point& second)
{
return atan2(second.y - first.y, second.x - first.x);
}

#define PI 3.1415926535898

double rad2deg(double rad)
{
return rad / PI * 180.0;
}

int main(int argc, char *argv[])
{
cout << rad2deg(angle(Point(0, 0), Point( 1, 0))) << endl;
cout << rad2deg(angle(Point(0, 0), Point( 1, 1))) << endl;
cout << rad2deg(angle(Point(0, 0), Point( 0, 1))) << endl;
cout << rad2deg(angle(Point(0, 0), Point(-1, 1))) << endl;
cout << rad2deg(angle(Point(0, 0), Point(-1, 0))) << endl;
cout << rad2deg(angle(Point(0, 0), Point(-1, -1))) << endl;
cout << rad2deg(angle(Point(0, 0), Point( 0, -1))) << endl;
cout << rad2deg(angle(Point(0, 0), Point( 1, -1))) << endl;

return 0;

}

Try something like:

angle = atan( (y1-y2) / (x1-x2) )

This will give the angle in radians.
To convert to degrees multiply by 180/PI.

However this will only give angles from -90 degrees to +90 degrees.

You then need to look at whether (x1-x2) is negative and if it is change the angle by 180 degrees.

I suggest you look search the net for info on trigonometry so you understand what I am talking about before you actually do it.

Also it would be a good idea to pre-generate the bullets at each angle so you aren’t wasting valuable resources by doing it in real-time.

I think you use dot products to test for line intersections but I could be wrong… I’ve used them before but can’t remember what for…

James.

At 16:10 3/07/01 +1000, you wrote:>Hi

I’m having lots of trouble coming up with a solution for this problem. I
have a player sprite and an enemy sprite. They are both located on a 2d
grid setup (each with x,y coords) - I need to find the angle between them.

I need this because they can both shoot at each other, they are using
"laser" weapons - where the ‘bullet’ is a straight line. I need the angle,
because I intend to use the excellant ‘rotoZoom’ library to rotate the
’bullet’ sprite according to the angle between the two sprites, so it looks
like the laser is in a straight line between them.

If I just fire the ‘bullet’, and they aren’t both on the same y coord then
it looks funny, like a straight line going at a angle.

I’ve spent ages searching the web for a algorithm to help me out - I came
across a DOT Product, but I’m not sure that’s what I want - can anybody help
???

Sorry if this is OT - but I’m out of ideas.

Thanks
David.