Skip to main content

Feel The Pain

This time I'm going to add collision to the game. Perfect collision detection is super important in bullet hell games, so I'll be making sure I get it right. I'll split this into two steps - first I'll add the code to support collisions and put in some coarse "box" collision. That's not going to be good enough, but it works well as a first pass. Once the box collision has decided that two objects are potentially colliding, then we'll switch to pixel perfect collision, which I'll add next time.


First, let's talk about that simple box collision..

Think Inside The Box


Box collision is pretty much exactly what you might think it is - define a box that fits coarsely around the object, and then test whether the boxes of two different objects overlap. The box position comes from the object's xPos and yPos, and the size comes from the width and height of the object's image.

Performing box collision is very simple. For two objects, call them A and B, you can do this in four steps: first check if the right edge of A is more than the left edge of B, then check if the left edge of A is less than the right edge of B, then do the same two tests but on the Y axis. If all four tests pass - you have a collision. Don't worry too much if that doesn't immediately make sense - try drawing it out on paper. The animation to the left might also help.




This box collision is codified in a new isColliding() function:

Next I made a function inside the object manager to collide two groups of objcets. I'll call this from the main loop to collide bullets against the payer.


Response

The second part of collision detection is collision response. You may have noticed the call to onCollision() inside the collide() function - I call this on both objects that are involved in the collision. This function exists in the base Object class and is over-ridden in both the player and bullet classes. When the player has collided, I just teleport it to another part of the screen - it's not much, but for now it's enough to show that collision has happened:


The bullet response is slightly more involved. I want the bullet to disappear after the collision, but I can't just remove it from the object manager - it gets quite messy if you want to remove an item from a list that you are currently iterating over. The easiest solution is to set a flag in the bullet, and have that flag used the next time isAlive() is called. See the full code for the bullet class below:


Wrapping Up

In this post I defined box collision and added some functions to make this happen. I also talked about collision response, and added some very simple responses to the bullet and the player objects.

Code is in the repo, and the online version is online here.

Next Time.. Collision Part 2

In the next post I will be adding pixel perfect collision. In the meantime, you could do a lot worse than read up on Hitbox Dissonance, which explains why accurate collision detection is so important.

Comments

Popular posts from this blog

Set The Controls For The Heart Of The Sun

When we last met, I had just started work on a level editor. I'd got as far as adding the ability to select an object and move it around, and then I added an undo/redo system using the command pattern. That was a good start, but dragging items around the level is only fun for so long. What I really want to be doing is.. ..editing the properties of the bullet spawners! And I want to be doing this in realtime as the game plays so that I can immediately see what effect my changes have made to the gameplay of the level.

World In Motion

In the last post I touched on how easy it is to score in this game - you can just camp out on the score bullet spawner and watch the points rack up. I'm going to add a little delay to the score bullets so that they start out in a deactivated and uncollectable state, and then change to active and collectable after a short time - say, half a second. To make it clear what state they are in I'm going to add code to let me change the look of Objects and allow them to be animated.

The Long And Winding Road

I started this blog back in November of last year. My aim was to document the development of a Bullet Hell shoot-em-up, written in JavaScript. Here we are, six months later, and lots has happened. With 20 posts already written, I thought that now would be a good time to take stock of what I've already done, and to look to the future and see what's to come.