www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/05/09:07:04

Message-ID: <37303898.7178CF0@vortex.ufrgs.br>
Date: Wed, 05 May 1999 09:24:56 -0300
From: "Luciano R. M. Silva" <lrms AT vortex DOT ufrgs DOT br>
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> <lwxXWQA8TyK3Ewyb AT zaynar DOT demon DOT co DOT uk>
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

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Unigni wrote:
<blockquote TYPE=CITE>The code used in QBasic is:
<p>&nbsp;Angle = ATN((x2 - x1) / (y2 - y1))
<p>where x1,y1 would be the co-ordinates of the enemy ship, and x2,y2 would</blockquote>

<p><br>I think
<p>Angle = atan2(x2 - x1, y2 - y1);
<p>should work for you.
<br>It returns in radians, and is definded in math.h. Also prevents you
from doing
<br>divisions by zero and works for all angles outside +/- pi/2, too.
<br>Maybe these functions are a bit slow for games (they use doubles),
there are
<br>also atan2f(x, y) an float version but I can't say if it's faster (haven't
tested).
<br>Do you really need the angle ? I think you could always use just the
cos(Angle)
<br>and sin(Angle) that you can get more directly:
<p>R = hypot(x2 - x1, y2 - y1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* sqrt((x2-
x1)*(x2-x1)+(y2-y1)*(y2-y1)) */
<br>CosAngle = (x2 - x1) / R;
<br>SinAngle = (y2 - y1) / R;
<p>That are the values you may need for making one object move in the Angle
<br>direction...
<p>LRMS</html>

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019