Need help in C++ collision detection?

Asked By 10 points N/A Posted on -


I have been writing a side scrolling shooter in visual studio with opengl. I draw the level with GL_QUADSTRIP that reads its points in from a .DAT file.

The DAT file looks like this
# level length
# level speed (used for development/testing)
# ground data
0.0 0.0
1.0 0.25
2.0 0.0
#ceiling data
0.0 1.0
1.0 0.75
2.0 1.0

I am trying to use this to try and implement collision detection between the ship(which is just a basic triangle drawn with opengl) and the floor and ceiling data
for(int i = 0, i < enemyLength, ++i){
if(ship.position.x < level.enemies[i].position.x){

This isn't working obviously so my question is can anyone help me work out how to read the data form the .DAT to check if the ship is colliding with the level and ending the game.

Any help is appreciated
Thanking You

Best Answer by Tony clever
Best Answer
Best Answer
Answered By 20 points N/A #90099

Need help in C++ collision detection?


Hi friend,

Here is the original code for side scrolling shooter in visual studio. Just add or edit it with your one.

And the floor and ceiling data is

for(into= 0;i<=level.groundLength; i++){

        if(ship.position.x < level.ground[i].x){

            gameState = GAME_QUIT;




If here there is any error then this is the way to check collision detection. For this just add these codes with your program

if(shipY <= gndY)


    if((shipX >= gndX && shipX < gndX+groundLen) ||

        (shipX+shipLen >= gndX && shipX+shipLen < gndX+groundLen) ||

        (shipX < gndX && shipX+shipLen > gndX+groundLen))

        cout << "There was a collision!n";


Answered By 0 points N/A #90100

Need help in C++ collision detection?


There are two methods for it, the first is to check when the sprite is being drawn, whether it is writing over the same area as the bomb. The second is to use a little trigonometry, and calculate the radius of the bomb, and the radius of the other sprite, and work out the hypotenuse between the two.

If the hypotenuse is less than the two radii added together, then you have a collision. You can find code on the net, If you want an understanding of collision detection methods a good place to start is here.
If you have a basic understanding of the theories involved in it puts you in a better position for creating more flexible game engines. You can just thought.

Login/Register to Answer

Related Questions