Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Tower Defense Indie Game Dev Blog #2: Obstacle Avoidance

Tower Defense Indie Game Dev Blog #2: Obstacle Avoidance

EddieK
EddieK
Killer Jack Dev Blog · · 3 min read
3,117 5

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.

Next week

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:


Twitter: https://twitter.com/extrabitgames
Facebook: https://www.facebook.com/extrabitgames
Website: http://extrabitgames.com


Recommended resources

3D Art

See full guide
3D Environment Design with Blender 5 cover
Editor pick

3D Environment Design with Blender 5

Amazon · Book

Based on Blender 5, this updated second edition simplifies the complexities by guiding you through clear, practical workflows to overcome those challenges. You’ll start by addressing common modeling and proportion mistakes, then move on to realistic texturing and unwrapping to create stunning scenes based on actual references.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

Low Poly 3D Modeling in Blender cover
Editor pick

Low Poly 3D Modeling in Blender

Amazon · Book

Step into the world of low poly 3D art with Low Poly 3D Modeling in Blender—your entry point into Blender and mastering the fundamentals of 3D art. This beginner-friendly guide ensures that you’re fully prepared for the creative adventure that follows. Through a step-by-step learning process starting with the principles of low poly art, this book gradually immerses you in the intricacies of modeling. As you progress, you’ll gain hands-on experience creating diverse projects ranging from designing a simple 3D crate to rendering complete low poly scenes. The book covers a wide spectrum of topics as you navigate Blender's interface, mastering essential modeling tools and exploring both basic and advanced modeling techniques. Advancing to the final chapters, you’ll find ways to breathe life into your models with material creation and gain practical insights into modeling a variety of low poly objects. From end-to-end scene construction to configuring Blender for rendering high-quality images, you’ll be equipped with the foundational skills to propel your career in 3D modeling and explore the boundless creative possibilities that Blender offers. By the end of this book, you'll have a solid understanding of Blender, 3D modeling, low poly methodologies, material design, 3D rendering techniques, and the broader world of 3D art.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.

Discussion

Loading comments...