# Thread: 2-d object avoidance. Help please! (basic stuff, I think)

1. ## 2-d object avoidance. Help please! (basic stuff, I think)

Hello. Is there any way to do simple collision avoidance for a system where one object is being chased by two others? The "prey" should be avoiding the "two predators". Two dimensional would be preferred. I can't seem to get anything to work correctly . Thank you! Here's the list of variables I'm using...(for reference)
Predator 1's x-coord: alpha
Predator 1's y-coord: beta
Predator 1's angle (from x-axis, angle>=0): theta
Predator 2's x-coord: iota
Predator 2's y-coord: kappa
Predator 2's angle (from x-axis, angle>=0): delta
Prey's x-coord: zeta
Prey's y-coord: epsilon
Prey's angle (from x-axis, angle>=0): eta

Here's my previous attempt that met very limited success.

a = alpha+x*cos(theta*(3.141596/180))*(3.141596/180);
b = beta+x*sin(theta*(3.141596/180))*(3.141596/180);
c = iota+y*cos(delta*(3.141596/180))*(3.141596/180);
d = kappa+y*sin(delta*(3.141596/180))*(3.141596/180);
f = zeta;
g = epsilon;
RAX = (f-a);
RAY = (g-b);
RAV = sqrt((RAX)*(RAX)+(RAY)*(RAY));
RAXB = (f-c);
RAYB = (g-d);
RAVB = sqrt((RAXB)*(RAXB)+(RAYB)*(RAYB));
WT=(RAVB/RAV);
if((((WT*(-delta))+(-theta))/2)>0 and (((WT*(-delta))+(-theta))/2)<180) eta+=1.0f;
if((((WT*(-delta))+(-theta))/2)>180 and (((WT*(-delta))+(-theta))/2)<360) eta-=1.0f;

Any suggestions? Thanks! I was referred here from the C++ board by elad.

2. Best way to handle this would be via scripting and script commands. However, if you know the angles of the two pursuers then you can average them and use that as the angle for your unit. Or component wise you can average the xvelocity and yvelocity of the pursuers and use the result of that for the new xvelocity and yvelocity of the pursuee.

Of course, xvelocity and yvelocity should be unit vectors in all case.

3. Thanks Bubba. I'll try averaging the angles. Yep x and y-coords are unit vectors. Thanks.

4. Eventually you will have a situation where there exists no angle to use to flee from the pursuers. My idea is to deduce which unit poses the most threat and then flee from that one.

For instance if you have a heavy tank....you probably wouldnt flee from a soldier into another tank. You would flee from the tank and not worry about the soldier whom you could crush at will.

If all units are of equal strength then you could simpy eliminate units based on distance from the unit being chased.

Once you narrow down which unit to run from you could simply get the velocity vector of the pursuer. Then set the velocity vector of the fleeing unit to that vector. Of course you would want your fleeing unit to turn gradually to the vector of the pursuing unit. This would cause the fleeing unit to run away from the pursuing unit. You could then alter this somewhat so that the fleeing unit does not look like it is simply copying the pursuer.

Another idea is to start at the pursuing unit. Cast a ray from the pursuing unit to a set distance in back of or on the opposite side of the pursuing unit. (ie: you will need to find the correct vector that will cause this to happen). Then use this point as a point to flee to for the fleeing unit. Continual calculation of this point should cause the fleeing unit to look more natural when it flees from the pursuing unit.

Example: The pursuer is south of or directly below the fleeing unit.

• 1. Calculate the angle between the units. In this case it will be 0 (if 0 degrees is up on your screen in your angle orientation).
• 2. Use a set distance from the fleeing unit as the end point of your ray cast. - probably will be an average of or a factor of the max pursuing unit speed averaged with the max speed of the fleeing unit. It would do no good to flee in the correct direction at a speed slower than the pursuing unit. If the max speed is too high for the fleeing unit....then you should probably turn and fight it out.
• 3. In this case we know the 'fleeing' vector will be 0 degrees (cos(0),sin(0)). So let's say we want to set a point 20 world units from the fleeing unit as a destination. That is the point we need to move to.
• 4. If our fleeing unit or the chicken is not facing that direction then it would probably be a good idea to turn him so that he does.
• 5. You could gradually increase speed as the chicken turns towards the new point - this would look more natural than just wasting time sitting and turning and then moving. So you turn and accelerate at the same time. Of course you will have to ensure that the chicken doesn't bump into anything else like other tanks, perhaps his own soldiers who wouldn't appreciate being squashed by him, and perhaps some trees that wouldn't take to kindly to being moved....and would probably resist to being moved at all.

Hope this helps.

5. Sweet! Thanks again Bubba. I was figuring on having to do some sort of a weighting system judging on distances from the 'chicken' to the 'chaser' but eliminating a chaser's effect after a certain distance is a good concept, thanks! Also your idea of a point to flee from is great, a whole new way to look at the problem. You're great! see ya on the flip-flop

Popular pages Recent additions