Collision Detection.

This is a discussion on Collision Detection. within the C++ Programming forums, part of the General Programming Boards category; First of all sorry about the double post. In my pong game I need to of course have the paddle ...

  1. #1
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Collision Detection.

    First of all sorry about the double post. In my pong game I need to of course have the paddle reflect the ball. My ball moves by having its velocity in both the x and y axis added onto its current position.

    Code:
    if (pongBall.xpos<=ppaddlex+5 && pongBall.ypos-5>=ppaddley-40 && pongBall.ypos-5<=ppaddley){
            pongBall.vely=pongBall.vely*-1;
        };
    In this piece of code I am trying to test:
    1st: if the x cordinate of pong ball = the front part of the paddle.

    2nd: If the middle of the pong ball is greater than the lowest part of the paddle.

    3rd: If the middle of the pong ball is less than the hightest part of the paddle.

    pongBall is a class with its xpos, and ypos and there accompaning velocities name vely and vel x. ppaddle x and y are the upper left corner of the paddle.

    Does anyone know why this collision detection wont work. Ive been staring at it for about 2 hours and I just cant figure it out. Sorry if I didn't give enough info please tell me and I will put up more.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,654
    So create a few more (temporary) variables which give meaningful names to some of the values involved.
    Code:
    int paddle_top = ppaddley-40;
    A few more of those, and your if statement will be a lot more readable.

    It helps to draw a diagram on paper showing the geometry of the problem to be solved.

  3. #3
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    ARRRRRRRRGHHH. I cant do it. can somebody please help me or give me a hint?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,654
    What?
    You can't draw a couple of shapes on a piece of paper and put some coordinates on there?

  5. #5
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    ya I can and did that, But it still seems like it should work. I have all the coordinates correct as far as I can tell.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  6. #6
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    Never mind I did it. I thought that the graph was
    ---------------------<---- x
    |
    |
    |
    |
    |<---- -y

    when y is realy positive
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM

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