Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


    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


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

share|improve this question
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. – ngoaho91 May 5 '13 at 16:52
Have you tried looking into steering behaviours? provides an overview, for what you want, flocking/queuing behaviours might be relevant – sarahm May 5 '13 at 17:00
up vote 2 down vote accepted

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.