Articles, Tutorials, and other things. => Allegro Game Programming => Topic started by: Richard Marks on March 30, 2010, 05:31:48 PM

Title: ASK Tutorial 08 - More on Collision
Post by: Richard Marks on March 30, 2010, 05:31:48 PM
ASK Tutorial 08 - More on Collision

Yep! Time for yet another Allegro Starter Kit tutorial! 8)

The concept: Let the ball's shot's kill the player.

It is assumed that you are using Visual C++ 2008 Express (MSVC9)
If you're not using MSVC9, then you will need to adjust some steps to suit your IDE.

This tutorial builds off the previous tutorial's code (, so don't forget to add the code from the previous tutorial.

Step #1 - Create a new Empty Win32 Project (not a Console Application) in your IDE.

File -> New Project
Choose Win32 Project
Enter "ASKMoreOnCollisionDemo" for the Name
Click OK
Click Application Settings
Check the Empty Project check-box
Click Finish

Step #2 - Save the Empty Project

File -> Save All

Step #3 - Copy the code files from Tutorial 07 into your project's folder.

Copy all the *.cpp and *.h files from the Tutorial 07 ( project into the folder that holds the ASKMoreOnCollisionDemo.vcproj file.

Step #4 - Add the files to the Project

Project -> Add Existing Item
Choose all the .h and .cpp files you just copied and click Add.

Step #5 - Add the code to the PrimaryWindow files to implement the demo concept.

Sorry, I am not going to even attempt to explain the following.. ::) Read the comments.
We need to add the code to the end of the UpdateBallShot() function in PrimaryWindow.cpp
// handle collision against the player
We are going to use a point/triangle intersection test this time.

Warning: The following code is convoluted and difficult to understand.
I didn't write the algorithm. I found it somewhere a long time ago.
I do not remember the source, so I credit it as (C) Original Author.

Just use it, and know that if the inside boolean is true after the code
executes, that there has been a collision.

The input variables are: x,y, vx1, vy1, vx2, vy2, vx3, vy3

x - the x coordinate of the point to test
y - the y coordinate of the point to test
vxN - the x coordinate for the vertice N of the triangle
vyN - the y coordinate for the vertice N of the triangle

// the player's triangle
int playerBottom = playerY_ + playerHeight_;
float vx1 = (float)playerX_;
float vy1 = (float)playerBottom;
float vx2 = (float)(playerX_ + (playerWidth_ / 2));
float vy2 = (float)playerY_;
float vx3 = (float)(playerX_ + playerWidth_);
float vy3 = vy1;

// the point is the shot's center (its position)
float& x = ballShotX_;
float& y = ballShotY_;

// test if the point is within the triangle
bool inside = false;
if (inside)
// kill the shot
ballShotAlive_ = false;

// reset the position to the ball
ballShotX_ = ballX_;
ballShotY_ = ballY_;

// kill the player! Oh Noooo
playerAlive_ = false;

// decrease the number of lives the player has

OK, we're done with this tutorial.

When you compile and run the project, you should get:
1 - A window with a black background
2 - The bouncing blue ball from the first tutorial
3 - The green triangle from the second tutorial that you can move left and right using the arrow keys.
4 - A single reloading cyan bullet that you can fire using the space bar.
5 - The ball should disappear if the bullet collides with it.
6 - The ball should re-appear (re-spawn) after roughly 1 second has passed after it dies.
7 - The ball should attack the player when it is moving downwards, and above the Y center line.
8 - Three second delay before you can move the player as it is in a "re-spawning" state.
9 - The player should flash on and off while it is in the re-spawning state.
10 - There is now a heads up display at the bottom of the screen showing the number of lives the player has.
11 - Now the player will die when hit by the ball's shots, and this will cause the player to lose lives.

Thanks for reading! See you in the next tutorial!
Let me know if you were able to follow this tutorial without any trouble.

(Updated April 2, 2010 - Changed usage of underscores in regards to Redslash's notice.)