Thread: 2d Collision Detection in openGL

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    2d Collision Detection in openGL

    As some of you may know, I'm currently undertaking a simple pong game, it will be my first game, and the road block I am at right now is making the paddles work, I can't figure out a good way to detect collision between the ball and the paddle.....

    I've tried the force methods....

    if blank = blank blank blank blank then make the ball go right direction, but i just cant make anything work, any suggestions anyone?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is a very easy way to get you started.

    Make a bounding box around your ball. So then you have a bounding box (ball) and a bounding rectangle (paddle). You should be able to test if the ball is *about* to move inside of the paddle. Before you update the ball's position, you check the new points of your box to see if any of them will be inside the paddle, if they are then you know you have a collision. A simple way to respond to this collision detection is to reflect the velocity vector about the surface of the paddle, which is probably vertical. Then that is your new velocity vector for the ball and then update as normal.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    typedef struct{
      int x;
      int y;
      int x2;
      int y2;
    }PADDLE;
    x = starting position of paddle
    y = ...
    x2 = x + size of paddle image
    y2 = ...

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Lesse

    Make a loop to check the XY coordinates of the translatef function, if the xy coordinates are inside ----(paddle inequality)------ then a collision has occured, and from there i invert the x speed to make it go back the opposite direction.......

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    i see a potential problem that my paddles have, they are drawn like this

    Left Top Right
    Left Top Right
    Left Center Right
    Left Bottom Right
    Left Bottom Right


    I need to make it so my drawing point for my paddle is at the bottom left, not the very center.....

    another to the to do list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection in OpenGL
    By Krak in forum Game Programming
    Replies: 13
    Last Post: 03-02-2009, 10:09 AM
  2. 2D pixel perfect collision detection
    By Warlax in forum Game Programming
    Replies: 0
    Last Post: 06-22-2006, 07:39 AM
  3. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  4. 2D sidescrollers and collision detection
    By funkydude9 in forum Game Programming
    Replies: 16
    Last Post: 03-26-2003, 09:22 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM