# 2D object move & evade obstacle

## Description:

Actor{
int x,y,width,height;
float speed, angle;
}


actor is an object that move around the map
every frame, actor change position(x,y) according to it’s speed & angle

## Problem:

If: Next frame actor A will collide with actor B
Then: A should turn to what angle to evade B?

• i tried - connect A's position with B's 4 coners - calculate angles of these 4 lines - and what is farest from B's angle is the best angle. choose this angle. it's slow. May 5, 2013 at 16:52
• Have you tried looking into steering behaviours? red3d.com/cwr/steer provides an overview, for what you want, flocking/queuing behaviours might be relevant May 5, 2013 at 17:00

## 1 Answer

Do you evade in the sense of "to flee" or "to (non-)cooperatively walk around each other"?

The answer to both questions is quite easy:

1) To let A flee away from B... let move A in the direct opposite direction of B relative to A. Which means: The direction vector should be Position(B) - Position(A). You'll then need to calculate the angle from this Vector for you data model (I think using atan(y/x) of the direction vector; or maybe atan(x/y) ... just try).

2) The let them -cooperatively- "walk around each other" just remind, what you do on the street, if you confront someone walking in your direction. You have to decide for a general "evasion"-direction. Which means: left or right in 2D. So either both actors evade on the right... or both of them evade on the left around the obstacle / other actor. This means you have to adapt your moving angle in one direction. The amount depends on the size of your bounding box. Note: I suggest you to use a bounding circle instead of a bounding box (as the corners of the bounding box will cause problems). If you run into performance problems only check the distance in case of collision. Use a hard limit and a soft limit for the obstacle distance. So you'll have 3 states: No Obstacle, Obstacle in Soft Limit, Obstacle in Hard Limit. Then observe the state changes. When the state changes from "No obstacle" to "Obstacle in Soft Limit" adjust your moving angle to evade. If it changes back to "No obstacle" adjust it back to move to your target position... It it changes to "Obstacle in Hard Limit" stop & wait! If you don't have a state, it will be quite complicated... as your actor will turn and turn and turn while it detects a bounding box collision, which makes it actually move in a circle and will maybe even let it crash into the obstacle.

3) For non-cooperative evasion you'll need to additionally 'evade' in a stronger manner as the opponent is not evading (passive, following a static path) or even trying to hit you (i.e. a missile).

Hope this helps Stefan