Warfront Infinite Dev Blog #2: Obstacle Avoidance
What have I done this week
This week I have fixed few bugs and finally implemented a fully working obstacle avoidance system which makes my pathfinding and collision/obstacle avoidance system done.
Some minor things which I did include:
- Fixed bug where text rendering causes lighting issues
- Added calculation of game entity’s axis-aligned bounding box from data contained in .obj file
- Added AABB to AABB collision detection and response
- Added Ray to AABB intersection detection
- Made the map size resizable
My own solution to obstacle avoidance problem
I had really hard time finding information about an easy way to do obstacle avoidance in the way I wanted it to be. So instead I worked few days and came up with my own solution which works pretty well. I think I kind of reinvented a wheel and someone might have a better approach to this problem than I do. But anyways, it’s already done and it works the way I wanted it to, which is all I care about now.
What I wanted from my obstacle avoidance/collision system
For the obstacle avoidance system I wanted it to do few things:
- Stop the vehicle if it gets too close to other vehicle
- If vehicles are about to collide (traveling towards each other at the angle less than the threshold angle) then make them steer away from each other
- Don’t ever let two vehicles overlap
I tried and I failed
Before I explain how it works, I will say things which I tried and which didn’t work that well.
First thing I tried was to create a separate AABB in the front of the enemy’s car and check if it collides with any other car. If it does, stop the vehicle. I thought this would make it so that the vehicles wouldn’t get too close to each other and hence wouldn’t overlap. Well, that didn’t work. Because when both vehicles collide to each other, they will both stop and will get stuck. To fix this, I added an if statement which checks whether the vehicles collide at each other if so, make it so that only one of two vehicles would stop and other would continue driving. But this made them overlap some of the times, which I didn’t want. So after thinking for a while, I decided I should add AABB collision response, so that when cars hit each other they don’t overlap, but get pushed back. So I did that and now it works pretty good, BUT if the vehicles are travelling towards each other there’s no way of knowing which way to turn to avoid the collision. So I decided to scrap this AABB in front of the vehicle approach and try casting rays.
Approach which worked
My last and final try was to use rays instead of AABB to check for collisions with other entities. This time I say entities because I also want to check if the ray intersects the towers as well, this way we will know which way the vehicle can turn to avoid collisions.
So the way I do it is pretty simple. The vehicle casts number of rays from its center towards the front of the car which check for intersection between towers’ and enemies’ bounding boxes. Then I have a function which does some magic and calculates (from the ray intersection information) which way the vehicle should steer. This steering is only done if two vehicles are facing each other and moving towards. If they are not moving towards, I check if the ray distance is smaller than the threshold value and if it is I just stop the vehicle. There are other few tiny hacks and tricks which I did to polish the system. But this is mainly how it works.
I still haven’t decided what I am going to do this coming week. But I think I will add different tower types, add GUI and make it so that enemies will spawn inside a building or in a hidden area from where they will come to the game’s map.
You can see the state of the game here: