Problem with vectors
by Dracola · in Torque Game Engine · 05/06/2006 (8:44 pm) · 3 replies
I am trying to find out whether on player is moving towards another player. But I ran across an annoying problem I can't find a way around.

theres a little diagram of how I have been doing it. I put it through the equation up top and if it comes out near 1 then the player at the start location is heading towards the one at the target location. But when it is at a 0,90,180,270 degree angle I ran across some problems it is either doing (0)/(something) which always returns zero or (something)/(0) and we all know what happens there :P. I've tried to find a way to do this using trig and polar coordinates but it always ends up to be the same basic problem. If anybody knows a way I could get around this or even a completely different method that works that would be very helpful.
Thanks in advance.

theres a little diagram of how I have been doing it. I put it through the equation up top and if it comes out near 1 then the player at the start location is heading towards the one at the target location. But when it is at a 0,90,180,270 degree angle I ran across some problems it is either doing (0)/(something) which always returns zero or (something)/(0) and we all know what happens there :P. I've tried to find a way to do this using trig and polar coordinates but it always ends up to be the same basic problem. If anybody knows a way I could get around this or even a completely different method that works that would be very helpful.
Thanks in advance.
About the author
#2
05/07/2006 (8:59 am)
Expanding just a little on what Gary wrote, normalize both vectors before getting the dot product. You'll get a result between -1 and 1 that you can feed to the aCos function.
#3
05/07/2006 (11:54 am)
Thanks the dot product method will work very well, I'm making a tag game and I need the bots to see if the person who is "it" is moving in their general direction. I can see if the return is between say .5 and 1 and if it is than the bot will run.
Torque Owner Gary Preston
VectorF vec = B.getPosition() - A.getPosition();
F32 dist = vec.lenSquared();
Then look at where the player will be next tick by adding his current velocity onto his current position and working out the distance between the two players again. If vec2.lenSquared() < vec.lenSquared() then player A is moving towards player B.
This does not account for player B moving away from player A at a faster velocity, which you may or may not want to take into account.
If on the other hand you're more interested in whether player A is simply facing player B (then you could test for a none zero velocity) you can use the dot product. Obtain a vector from player a to player b (eg PlayerB.pos - playerA.pos) then dot product with the players forward vector (which can be obtained by taking column 1 of the playerA's transform).
If the result of the dot product > 0 then Player A is facing within 90 degrees of player B. If you want to know the exact angle then you can use ACos( ) on the result to get the real angle.
Hope one of those helps :)