Hi ,
i have a question that i found difficult , knowing the position and direction of 2 cars, how do I calculate which one is ahead or on the right?
Vehicle ahead other
 CarloMaker
 Gnoblar
 Posts: 24
 Joined: Fri Sep 26, 2008 8:48 am
 Location: Italy
Vehicle ahead other
SOOORY FOOOR MMMY ENGLISHSS
Ogre 1.8  Physx 3.3   Poco 1.7
Ogre 1.8  Physx 3.3   Poco 1.7
 Zonder
 Ogre Magi
 Posts: 1144
 Joined: Mon Aug 04, 2008 7:51 pm
 Location: Manchester  England
 x 61
Re: Vehicle ahead other
My math not great but I think you want something along the lines of:
Difference = CarA  CarB
Difference.X < 0 CarB is Left
Difference.X > 0 CarB is Right
Difference.X = 0 CarB is on the same line
You can do the same with y and z to determine if it's ahead, behind, above or below as well
Not 100% if you need to remove the rotation from Car A making B relative to it or not am sure a math wizz will correct me
Difference = CarA  CarB
Difference.X < 0 CarB is Left
Difference.X > 0 CarB is Right
Difference.X = 0 CarB is on the same line
You can do the same with y and z to determine if it's ahead, behind, above or below as well
Not 100% if you need to remove the rotation from Car A making B relative to it or not am sure a math wizz will correct me
There are 10 types of people in the world: Those who understand binary, and those who don't...

 Gnoblar
 Posts: 11
 Joined: Wed Oct 02, 2013 6:10 am
Re: Vehicle ahead other
Unfortunate the concepts of ahead or to the right are relative so the answer depends somewhat on exactly what you mean by that. To answer I'm assuming that ahead/behind/right/left is relative to a given cars direction of travel (ie, if car a is headed due north and car b is further to the north, then car b would be considered ahead of (or at least in front of) car a.) This answer also assumes you don't care about above or below. Also, note this is one way to do this, it is not necessarily the 'right' way just as any other method isn't necessarily the 'wrong' way. It really just depends on what information exactly you are after.
What we are going to do is calculate the angle between the direction the nose of car A is pointer and the relative vector between the position of car A and car B or world coordinates. Based on the resulting angle we can determine if car B is Ahead of or Behind Car A and wither it is to the right or left. This has the advantage that it will work regardless of what plane the vehicles are traveling on in world space.
Step 1: We need to determine the direction car A is pointed. First find the mid point in world coordinates of car A and follow along the car's axis to the bumper of Car A. Subtract the midpoint from the 'bumper' point and that is your directional vector. a = [x1,y1,z1]
Step 2: We need to determine the vector between Car A and Car B. (again in world coordinates) subtract the mid point of car A from the mid point of Car B which will give you the vector b = [x2, y2, z2]
Step 3: Find and store the dot product of a*b (It's possible that your math library with have a dot product function, otherwise you can easily look it up online (dot product of two vectors))
Step 4: Find and store the magnitude of each of your two vectors a & b. for example vector a's magnitude would be = (sqroot(x1^2 + x2^2 + x3^2))
Step 5: Divide the dot product of a*b by ((the magnitude of a) * (the magnitude of b))
Step 6: find the arc cos of the results of step 5 (The c++ math library will have a function arccos()) this is your angle in radians. (between 0 and 2*PI) you can convert this to degrees if you like (just look up radians to degrees for the equation) but it isn't strictly speaking necessary to do so.
Okay, the result of step 6 is your angle relative to the nose of Car A. If the angle is:
between 0 and PI/2  2PI/3 and 2PI then Car B is Ahead and to the left
between PI/2 and PI  PI and 2PI/3 then Car B is Behind and to the left
between PI and 2PI/3  PI and PI/2 then Car B is Behind and to the right
between 2PI/3 and 2PI  PI/2 and 0 then Car B is Ahead and to the right.
if the result is directly on a cardinal heading (0  2PI, PI/2, PI, 2PI/3) then Car B is directly ahead, left, behind, or right respectively.
Hope that helps.
What we are going to do is calculate the angle between the direction the nose of car A is pointer and the relative vector between the position of car A and car B or world coordinates. Based on the resulting angle we can determine if car B is Ahead of or Behind Car A and wither it is to the right or left. This has the advantage that it will work regardless of what plane the vehicles are traveling on in world space.
Step 1: We need to determine the direction car A is pointed. First find the mid point in world coordinates of car A and follow along the car's axis to the bumper of Car A. Subtract the midpoint from the 'bumper' point and that is your directional vector. a = [x1,y1,z1]
Step 2: We need to determine the vector between Car A and Car B. (again in world coordinates) subtract the mid point of car A from the mid point of Car B which will give you the vector b = [x2, y2, z2]
Step 3: Find and store the dot product of a*b (It's possible that your math library with have a dot product function, otherwise you can easily look it up online (dot product of two vectors))
Step 4: Find and store the magnitude of each of your two vectors a & b. for example vector a's magnitude would be = (sqroot(x1^2 + x2^2 + x3^2))
Step 5: Divide the dot product of a*b by ((the magnitude of a) * (the magnitude of b))
Step 6: find the arc cos of the results of step 5 (The c++ math library will have a function arccos()) this is your angle in radians. (between 0 and 2*PI) you can convert this to degrees if you like (just look up radians to degrees for the equation) but it isn't strictly speaking necessary to do so.
Okay, the result of step 6 is your angle relative to the nose of Car A. If the angle is:
between 0 and PI/2  2PI/3 and 2PI then Car B is Ahead and to the left
between PI/2 and PI  PI and 2PI/3 then Car B is Behind and to the left
between PI and 2PI/3  PI and PI/2 then Car B is Behind and to the right
between 2PI/3 and 2PI  PI/2 and 0 then Car B is Ahead and to the right.
if the result is directly on a cardinal heading (0  2PI, PI/2, PI, 2PI/3) then Car B is directly ahead, left, behind, or right respectively.
Hope that helps.