Asteroids Plus 2.0 - Milestone 12– collisions, the bane of my existence


So, this is where I failed a few years ago.  

Not misunderstanding collisions… not detecting collisions…

Resolving collisions…

I could not figure it out.

I found this fantastic walk-through regarding basic physics in JavaScript, that I’ll share.

https://spicyyoghurt.com/tutorials/html5-javascript-game-development/collision-detection-physics

It all makes sense to me. It didn’t at first, but I’ve been playing around with this stuff for a while now.  I get it.

But there is a sequence of Vector math that must be juuuuust right for it to work.  And looking back on this, I wasn’t careful enough.  To be clear, I was a bit lucky/fortunate to figure out my mistake.

Let’s discuss briefly what collisions are required in this game to this point.

  1. Bullets colliding with asteroids
  2. Asteroids colliding with asteroids
  3. Asteroids colliding with player’s ship

First order of business is detection.  And I’m starting out with everything having a circle ‘hitbox’ of sorts.  Each entity to this point is assigned a radius, based on their div size.  Also, now that I have a radius, I need to calculate the center point for each div in an ongoing basis.  Not too terrible.

Circle collision detection is the most basic form.  You measure the distance between center points of entities.  Then you compare that distance against the combined radii (rad1 + rad2) and if the distance is less than that sum, then you are colliding.  Super simple.  This is made doubly simple in my Vector class that I have a getDistance method that takes another vector as an input, and it does the math for me and returns a scalar (number) representing the distance.

Again, this is the easy part, then we must discuss resolving a collision.  For Bullets colliding with Asteroids, that’s super simple… the bullets go away.  So, in my Asteroid collision detection code for Bullets, if there’s a hit, I simply call that method on the colliding bullet and bye-bye bullet.  Because I was bored, I went ahead and added to each entity a health value.  And I assigned a damage value to the bullet entities.  So when a bullet hits an asteroid, it reduces its health, and if health is reduced to zero or less, I call the newly added destroy() method on the asteroid, and it becomes un rendered.

That added a lot of simple pleasure and fun to this game just right there….

Asteroid to Asteroid collision is where I struggled.  The site I posted earlier in the entry had all the answers and methodology.  But my first attempt at implementation got be right back to where I failed and quit a few years back when doing this game in JS.  This is what made me use Godot for my first game.


I’m leaving this one full page.  I’ve circled the three different asteroids that magnetically clump together due to my vector math.  All colliding asteroids did this.  It was infuriating.  I had failed AGAIN after a few more years of experience…. I was better than this. 

This went of for a few hours with random guess/check method of trying to manipulate my equations, as this was VERY hard to debug and track down.

I put it down for a couple hours and came back to it.  And I tried my normal attempt to solve complicated code problems… I created a separate project to exercise this in… simple as can be.


Two balls, that will run into each other and bounce back.  This was going to be a simplified version of what I’ve been attempting to do with Asteroids.  I didn’t even finish the project.  This forced me to go back into the algorithm from the beginning.  And I found my bug… In the first line of the resolution code where the vector of the collision is defined.  My vector math was backwards, and when you’re subtracting vectors - THE ORDER MATTERS.

So, I shelved this project immediately and low and behold, make one simple fix, and then have asteroids bouncing off each other like ping pong balls.  I was SUPER elated, and it made my whole day happy.

The last bit of business for this entry was a simple copy/paste of the same logic for the ship/asteroid collisions.    There was one minor adjustment.  The asteroids had to be much more massive than the spaceship… reason being, I want to prevent the impulse of the ship to send asteroids off like missiles.  The ship’s mass is like 5% of the mass of the smallest asteroid…. This will still need adjusting with play testing and balance adjustments.  This wraps up a successful entry 😊

This currently hosted game site: https://mookie4242.itch.io/asteroids-plus-20

Twitter: @jyoung424242

SlideShare: https://www.slideshare.net/JustinYoung3/next-gen-asteroids

GitHub: https://t.co/utxHxXKdOz

Leave a comment

Log in with itch.io to leave a comment.