Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Find the point on a vector closest to another point

Author
Message
Philip
20
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 18th Jun 2004 00:58 Edited at: 18th Jun 2004 00:59
This may seem a bit of an esoteric sort of topic but actually its damned useful in games.

One issue that crops up time and time again in games is the need to find out whether the vector that your ship/man/object/thing is moving along comes too close to / collides with another ship/man/object/thing ("the Possible Collision Object") in the game. This is a key navigational issue because frequently you will want to avoid collisions and plot a "safe" course.

One way to check this is demonstrated by the attached example code. The function (which, as you will see, works in 3D) returns the distance from your man to the point on his trajectory vector which is closest to the Possible Collision Object.

Its then a simple matter of normalising the trajectory vector, scaling it by the distance and adding it to your man's X, Y and Z position to get the X, Y and Z position of the closest point.

The distance between the Possible Collision Object and the closest point is then easy to solve.

Incidentally, whilst the attached function works in 3D, as the example program uses a 2D display, if you wanted to run the whole thing in full 3D you'd need to change the lines:



into 3D. The way to do this would be to make 2D vector types numbered 1 and 2 into 3D vector types.

Toodles

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home
kevil
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: Netherlands
Posted: 18th Jun 2004 01:31 Edited at: 18th Jun 2004 01:32
I think that's a bit inefficient. Here's what you're doing:

v = vector 1
w = vector 2

length# = ||w||

v = v / ||v||
w = w / ||w||

angle# = acos(dot(v,w))

distance# = cos(angle#) * length#

Now to simplify it, we fill in your substitutions in the last formula:

distance# = cos(acos(dot(v,w))) * ||w||
distance# = dot(v,w) * ||w||
distance# = dot( v / ||v||, w / ||w|| ) * ||w||
distance# = dot(v,w) / ||v||

So you only normalize vector 1, and then to calculate the distance you only do the dot product between vector 1 and vector 2.
As you said, this can be very usefull, so let's optimize it as much as we can.

Kevil
Philip
20
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 18th Jun 2004 13:18
Yes indeed. In retrospect I have no idea what I was thinking performing both an acos and a cos calculation. Talk about redundancy. Also as acos and cos are both computationally "expensive" getting rid of them, as you suggest, is a v. good idea.

Philip

What do you mean, bears aren't supposed to wear hats and a tie? P1.3ghz / 384 megs / GeForce MX 5200 128meg / WinXP home

Login to post a reply

Server time is: 2024-05-13 09:00:11
Your offset time is: 2024-05-13 09:00:11