Message-ID: <37303898.7178CF0@vortex.ufrgs.br> Date: Wed, 05 May 1999 09:24:56 -0300 From: "Luciano R. M. Silva" X-Mailer: Mozilla 4.51 [en] (Win95; I) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Calculating direction to shoot in References: <372AFE6B DOT D048BBCE AT webmail DOT co DOT za> Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit X-Original-NNTP-Posting-Host: prt01u15.ez-poa.com.br Lines: 28 NNTP-Posting-Host: irc.ez-poa.com.br To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Unigni wrote:
The code used in QBasic is:

 Angle = ATN((x2 - x1) / (y2 - y1))

where x1,y1 would be the co-ordinates of the enemy ship, and x2,y2 would


I think

Angle = atan2(x2 - x1, y2 - y1);

should work for you.
It returns in radians, and is definded in math.h. Also prevents you from doing
divisions by zero and works for all angles outside +/- pi/2, too.
Maybe these functions are a bit slow for games (they use doubles), there are
also atan2f(x, y) an float version but I can't say if it's faster (haven't tested).
Do you really need the angle ? I think you could always use just the cos(Angle)
and sin(Angle) that you can get more directly:

R = hypot(x2 - x1, y2 - y1);      /* sqrt((x2- x1)*(x2-x1)+(y2-y1)*(y2-y1)) */
CosAngle = (x2 - x1) / R;
SinAngle = (y2 - y1) / R;

That are the values you may need for making one object move in the Angle
direction...

LRMS