C++ collision detection

This is a discussion on C++ collision detection within the Game Programming forums, part of the General Programming Boards category; hello I have been writing a side scrolling shooter in visual studio with opengl. I draw the level with GL_QUADSTRIP ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    C++ collision detection

    hello 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
    20
    # level speed (used for development/testing)
    1.2
    # ground data
    10
    0.0 0.0
    1.0 0.25
    2.0 0.0
    #ceiling data
    10
    0.0 1.0
    1.0 0.75
    2.0 1.0
    #end
    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
    Code:
    for(int i= 0;i<=level.groundLength; i++){
    		if (ship.position.x < level.ground[i].x){
    			gameState = GAME_QUIT;
    		}
    }
    This isnt 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 collidiing with the level and ending the game.

    Any help is appreciated
    Thanking You
    Last edited by ForeverAndOne; 10-09-2011 at 03:55 AM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You want to read the data from the file to find a collision? I'm a bit lost. You can use bounding boxes to compute collisions in the code but why would you want to read the data from the .dat to find a collision?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Quote Originally Posted by ForeverAndOne View Post
    # level length
    20
    # level speed (used for development/testing)
    1.2
    # ground data
    10
    0.0 0.0
    1.0 0.25
    2.0 0.0
    #ceiling data
    10
    0.0 1.0
    1.0 0.75
    2.0 1.0
    #end
    If this file remains constant, it's as easy as reading certain lines while discarding the rest.
    But if this is a general form of a more complex format, ( e.g comments, assignment, arbitrary data position ), you need to implement a parser that will retrieve the data for you.

    Quote Originally Posted by ForeverAndOne View Post
    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
    Code:
    for(int i= 0;i<=level.groundLength; i++){
    		if (ship.position.x < level.ground[i].x){
    			gameState = GAME_QUIT;
    		}
    }
    Since we're talking about ground, I think you messed the meaning of "x" in your mind.
    Say (shipX, shipY) are the coords of the ship, (gndX, gndY) are the coords of a piece of ground, also "shipLen" and "groundLen" the lenghts of the ship and ground piece respectively:
    This is a way to check for collision properly:
    Code:
    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";
    }
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision detection
    By jack_carver in forum C Programming
    Replies: 5
    Last Post: 02-08-2010, 05:52 AM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. Collision Detection
    By cboard_member in forum Game Programming
    Replies: 2
    Last Post: 08-06-2005, 12:14 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Need collision detection!(HELP!)
    By SyntaxBubble in forum Game Programming
    Replies: 1
    Last Post: 02-27-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21