Artificial Intelligence III – Repulsion behavior

As explained in my previous post, I had a problem with the movement of the mob. When the mob was following the kitten, and suddenly encounter a collidable obstacle in the way, he was getting stuck at that obstacle and wasn’t able to pursue the kitten anymore.

I noticed that, after that happened, the ray thrown by the mob was colliding all the time, making impossible for the mob to see the kitten. Hence, the mob was not just colliding with the obstacle, but actually getting inside the obstacle.


The movement is generated from an acceleration vector. The acceleration vector defines the forces affecting the entity, and these forces cause the entity to move with a direction and a velocity.

In the real world, if you run like a madman towards a wall, and at the very last moment you try to stop, the inertia will make you crash against the wall. You changed your acceleration, but changing your velocity and direction requires a delay named inertia that produces the consequent facial bones breakdown. For more information about this physical law, ask Richard Hammond.

In the same way, the mob crashes into the wall because, although he tries to deviate modifying his acceleration, the inertia hinders it. For when he manages to stop, he is already “inside” the wall, and his vision ray collides all around, preventing him to see the kitten.

blog1Although I prefer the kittens not to be hunting prey, this problem must be solved. For science’s sake.



>> I recommend you to have this class opened around because I’ll be using some of its methods.

The answer lies in the relationship between the mob and the obstacles. The mob must be aware of his environment and interpret the feedback, just like radars do.


Starry ray

My radar consists in a set of short rays that will constantly check if there exists any collision. I called it the “Starry Ray”, because it looks like a star glowing around the mob.



Code excerpt:


Here‘s the complete implementation.


The algorithm

When one or more rays detect a collision, then the mob should move in the opposite direction. For instance, if there is a wall in the north and another wall in the west, then the mob should move to the south-east.

Simultaneously, the closer the mob is from the wall, the faster he should be repelled.

So the goal here is to create an acceleration vector, which is composed by a direction vector and a velocity vector.

The direction is a vector that defines where should the mob go. It is calculated adding all the rays implied in collisions, and taking its inverse. In the image below, the rays a and b are colliding, therefore, the direction vector will be the opposite of a+b:


In the code:


The velocity vector defines the speed at which the mob should move away from the wall. This speed corresponds to the magnitude or length of the vector. As said before, the mob’s velocity is inversely proportional to the distance to the wall, just like an inverted gravitational force. So, if we take the equation:

and apply it to our case, the result is:


  • The gravitational constant is the maximum acceleration at which the mob can move.
  • The distance is the shortest ray involved in a collision.

Once we’ve calculated this value, we apply it  to the direction vector, and that’s when all comes together:

There’s only one step left, at that is, applying this acceleration to the mob. To understand how, it’s helpful to think about acceleration as a force. The mob is affected by other forces of the world. We can’t just ignore them all and set the repelling force as the one. Instead, we combine them:

As result, we got the implementation of the AvoidWalls behavior.



We know how the mob should act regarding kittens, and regarding walls. In other words, we know the way he should behave.

The BlendedSteering behavior is not a behavior by itself, but a composition of several behaviors.

With BlendedSteering you can combine multiple behaviors to get a more complex behavior.

This way:

Where the second parameter (Xf) is the weight of the behavior.

Here, all the behaviors have the same weight, meaning, purusing the kitten is as important as avoiding the walls.

At the moment we have two forces, the attraction (towards the kitten) and the repulsion (from the walls), handled by their corresponding Steering Behaviors. In the future, other forces may intervene, and BlendedSteering will always keep up the harmony between the two sides of The Force them.

Find the complete implementation here.



I guess this is the last post of the year. I love all the year’s events, like Christmas, Valentine’s Day, Carnival, etc. There’s no better excuse to spend time and money in crafts, decoration, bakery, gifts and organization. So from now until everything calms down, I’ll be laughing madly surrounded by christmas ornaments.

Please, gift me a Like/Share/Subscription if I’ve been a good girl. See you next year! ^^


Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑