• ### What is your GameDev Story?

#### Archived

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

# Fast Angle&Distance Count between two Points???

This topic is 6178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Do someone know a fast way to count the angle and the distance between two points? I know the standart geometric formels in a triangle (with ArcTangens and so) but they are a little to slow...

##### Share on other sites
Do you need really distance and angle?

I say that because if you need that data for compare with other distance/angle then you can avoid operations.

for example, if you need to compare distances you don''t need to compute square roots:

distance(a) > distance(b)
<==>
sqrt(a.x*a.x + a.y*a.y) > sqrt(b.x*b.x + b.y*b.y)
<==>
(a.x*a.x + a.y*a.y) > (b.x*b.x + b.y*b.y)

##### Share on other sites
The distance isn''t as much important...
at first I need the Angle to set the view point (so that my player looks forward his enemy).
And the distance is needed to count the chance to hit...

##### Share on other sites
I don''t know your code.

Really needs to work with real angles. Maybe you could do many of your calculus with x/y quotients (not only this case, in all your code). Computing real angle only when needed.

You could try too Lookup tables.
Or some math approach (Taylor or Chebyschev polynomials) with little accuracy --> little calculus.

##### Share on other sites
I don''t need real angles... 8 or 16 will be enought

How should this x/y quotients look like?
For Example when we have Point A (x=0,y=0) and Point B (x=50,y-50) how is the fastest way to count the angle and Distance between them?

##### Share on other sites
When I talk about not to use real angle I''m not talkin about accuracy. I''m talking about use another function related with the angle but more easy of calculus.

I suppose you need distance and angle of every enemy (many) to choose the more easy to shoot it.

You could choose another functions as {square(distances)} and {distance_x/distance_y} for make your calculus. For discard a great number of enemies (or for really choose the enemy). And then compute distance and angle only for a little set of enemies.

[I go out to eat some thing, in a couple of hours I''ll be online again]

##### Share on other sites
yes this is what I try to do and what I looking for.

Bon Appetit

##### Share on other sites
The dot product of two unit vectors returns the cosine of the angle between the vectors. This result can be passed to acos to get the angle.

Say you have your two points in space represented by vectors v1 and v2. You need to normalise these vectors. This is done by scaling the vector so that it has a length of one (a unit vector).

magnitude = sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z)
invmag = 1.0 / magnitude
v1.x = v1.x * invmag
v1.y = v1.y * invmag
v1.z = v1.z * invmag

Do the same to v2. Now that you have your unit vectors, you can find the dot product.

dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z

This gives you the cosine of the angle between the two vectors. Pass this result to acos and you will have the angle (in radians) between the two vectors.

The above was assuming that the pivot point (viewer position) was the origin (0, 0, 0). If the viewer position is not the origin, then the viewer position must be subtracted from both vectors prior to the above operations.

Steve 'Sly' Williams   Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

Edited by - sly on February 19, 2002 4:49:13 PM

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×