This post is a portion of Part 5 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 5.0 – Our First Game
- 5.1 – Our First Game: The Player
- 5.2 – Our First Game: Timing and Movement
- 5.3 – Our First Game: The Projectiles
- 5.4 – Our First Game: The Enemies
- 5.5 – Our First Game: Collision Detection
- 5.6 – Our First Game: Game Logic
- 5.7 – Our First Game: Conclusion
In this video, we look at making our game interactive by adding collision detection. We don’t do anything too extreme though, just basic bounding box magic.
Full source can be found here.
Notice that the source code is slightly different from the video? Here is why.
Thanks Mike!
Would it be possible for you to make a tutorial on an autonomous enemy tracking algorithm?
Could you be more specific? Do you mean a system that knows where game objects are, or like “heat seeking missiles”?
Yes – almost like “heat seeking missiles.” Although I am referring more to enemies tracking the player. For example, comets autonomously tracking the ship as they move, instead of just flying straight ahead.
Something like that is actually very easy to achieve (depending on how you want it to work). Basically, every update cycle, the comets could adjust their bearing to the current position of the player. Of course, you would probably want to make it sloppy (so the player has a chance) which can be done by adjusting the bearing only every 5 cycles or so.
If you wish to have external forces acting upon the projectile (comet, missile, …) you can give the object momentum. It may be heading down, but when the player’s ship is to the left, apply a force toward the player. The momentum will be strong enough that the projectile will “lean” toward the player without actually homing in on them directly. It gives the projectile a natural curve without any jerky course alterations.
Thank you very much, you are an amazing teacher.
Im just wondering if there is a better way to do this in Allegro 5? I know in Allegro 4 there was a way to grab Pixels from a bitmap and if your in an area with that certain pixel. It trigger a collision statement. Wich I had set up.
You can certainly do that, but I would not consider it better. Get pixel operations are costly as far as efficiency goes. Also it is worth noting, that in this example we aren’t using bitmaps. Therefore, we would have to draw to the screen and then pull back from the screen. Bounding box collision is much more efficient.
Thankyou. I was thinking of doing it that way for my Pong game that I thought I might make. I will have to work on Bounding Box then.
I tend to get Comets too close to each other after every little while(not fixed), two comets tend to be drawn over each other in a way that they have 30-40% shared circle!
I hope i am able to convey it correctly!
It should be that the comets are initialized randomly. You will have some spawning on each other, but that shouldn’t happen all of the time.
It does, each time i run the game, after every few seconds, i do get some spawning! The game might look mediocre because of it. So, some rand() mod should do it ?
i love your tutorials and learning this, u are a awesome teacher i cant wait to see more
Hey!
I must just thank for the great toturial you have made. I am just starting to get interessed in programming and i have soon watched all parts you made.
Otherwise i got a ide of an game, and soon i am also able to start on it. But there is still much to learn for me as i have only been programming in about 3 weeks on my sparetime.
so far i have made your sideshooter game, i have also trained to add bitmaps, all is great.
But now i am trying to figure out a new angle of the game, their your ship aims at the mouse cursor and shoots strait to the cursor, i still can’t figure out how to do that, also i am interessed to find out how to make my astroids aim at the ship.
would appriciate some assistance with that.
//Mattias
This is a very common question. Look up an algorithm for angular velocity. That should steer you in the right direction
Hey!
I have looked at to many sites so far and it doesn’t help at all. I have started with my own project but as i don’t get this to work makes me having a hard time, also my hair is starting to become less.. I found alot of suggestions but i can’t just add it into and apply it into my game..
would be glad if i could get some assistance.
/Mattias
Since both the comets and the bullets are circles, we could have just done circle-to-circle collision detection. Its as simple as bounding box but in this case more accurate.
One thing I don’t understand about the bounding logic: wouldn’t the entire bounding box of the comet have to be within the bounding box of the ship to register true? Don’t we want this to be true even if only part of the boxes overlap?
It is true if only a part overlaps.
hey i watch your tutorials every day ,your help us so much ! thanks you
just i want to ask you a question about prototype if we want to Initialize another ship (like if we want to make the game for 2 players) we use the same prototype or we have to make a new one if the first case please tell me how and already I make a second ship but I create a new prototype for it like that :
void InitShip(SpaceShip &ship1);
void DrawShip(SpaceShip &ship1);
void MoveShipUp(SpaceShip &ship1);
void MoveShipDown(SpaceShip &ship1);
void MoveShipLeft(SpaceShip &ship1);
void MoveShipRight(SpaceShip &ship1);
void InitShip2(SpaceShip &ship2);
void DrawShip2(SpaceShip &ship2);
void MoveShipUp2(SpaceShip &ship2);
void MoveShipDown2(SpaceShip &ship2);
void MoveShipLeft2(SpaceShip &ship2);
void MoveShipRight2(SpaceShip &ship2);
Don’t duplicate functions. Instead:
void InitShip(SpaceShip &ship2);
void DrawShip(SpaceShip &ship2);
void MoveShipUp(SpaceShip &ship2);
void MoveShipDown(SpaceShip &ship2);
void MoveShipLeft(SpaceShip &ship2);
void MoveShipRight(SpaceShip &ship2);
(comets[j].x – comets[j].boundx)
(comets[j].x + comets[j].boundx)
could you maybe explain this better to me? ; what exactly happens with – and + ?
ahmmm never mind I got it 😀 its comet[].x is just 1 pixel and it goes + colision box , 😛 got it
Hi, I’m having some problems with the boundary collide, it seems that sometimes the bullet just shots throught the comet, without colliding…Don’t know why that, but my best guess would be that, it’s taking to long to compare the objects against each other…any help on that?
Its hard to say without seeing code. Chances are there is something missing in your collision check.
i’m having the same problem! it seems my code is just like his, but it doesnt work :S
Man, I’m here just to thank you SO MUCH for doing these tutorials. They’re REALLY good and i’m learning a lot with them. Thank you my friend. (:
Was looking to learn graphics using Allegro 5 after studying C++ for a couple of years. What a terrific tutorial! Thanks so much, Mike. 🙂
Hello, I have a problem with the comets. The comets appearing on the screen usually reach max(10) and I don’t have enough time to shoot them all. In your video, I see the comets appearing on the screen usually reach 4 and sometime there’re even non of them on the screen. In my case, 10 comets pour out almost at the same time. You can see in here: https://40.media.tumblr.com/1b0f42ffae14c8075f7457c69488a055/tumblr_o5zdi5WSIz1sbe06zo1_540.png and https://40.media.tumblr.com/5ec194a5d7f79c7e26c1401e1c075b4b/tumblr_o5zdi5WSIz1sbe06zo2_r1_540.png
How can I fix this?
I have:
srand(time(NULL));
as well as
if (rand() % 500 == 0)
ah, I found it!
Great tutorial by the way!