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..
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.
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:
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.
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
Post a Comment