Results 1 to 14 of 14
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Pong - Y-Axis Collision Detection

    I have made a "Pong" game and I have no idea how to check if the ball has hit the top or the bottom of a paddle, however I do know how to check if it has hit the left or right side:

    Java Code:
    if((BALL.getX() >= LEFT_BAT.getX() && BALL.getX() <= LEFT_BAT.getX() + LEFT_BAT.getWidth() 
    	&& BALL.getY() >= LEFT_BAT.getY() && BALL.getY() <= LEFT_BAT.getY() + LEFT_BAT.getHeight())
    	&& (BALL.getX() >= RIGHT_BAT.getX() && BALL.getX() <= RIGHT_BAT.getX() + RIGHT_BAT.getWidth()
    	&& BALL.getY() >= RIGHT_BAT.getY() && BALL.getY() <= RIGHT_BAT.getY() + RIGHT_BAT.getHeight())){
    		BALL.increaseDX();
    		BALL.setDX(-BALL.getDX());
    }
    It seems as though I would use the same code for checking the top or bottom, but that doesn't make sense. Can you please help me with figuring it out or give me a hint?

    Thanks in advance! :)
    Last edited by Zelaine; 01-28-2014 at 05:35 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Pong - Y-Axis Collision Detection

    Shameless self-promotion: the link in my signature to StaticVoidGames has a tutorial going over Pong in Java, underneath the "Learn" tab.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    Your ball can't hit the top or bottom of the bats, however mine can. That is why I want the ball to bounce when hitting the top or bottom of the bats, because at the moment it doesn't.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Pong - Y-Axis Collision Detection

    Well, more shameless self-promotion: the same tutorials contain a section on collision detection, which goes over detecting collision detection between arbitrary hit boxes.

    But the gist is this: check the top and bottom of the paddle first, and then in a separate if statement, check the left and right sides. That way you know which side to bounce the ball off of.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    Okay, I'll check out your tutorial. But to check if the ball has hit the paddles you have to check if it is in the coordinates of the paddle, so how do I differentiate the ball hitting the top and bottom from the ball hitting the left and right sides?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Pong - Y-Axis Collision Detection

    The tutorials explain it better than I can here (that's the point of them, hehe), but here is the general idea:

    Java Code:
    if(ball + deltaY is inside square){
       move ball to top/bottom hit
       reverse deltaY;
    }
    if(ball + deltaX is inside square){
       move ball to left/right it will hit
       reverse deltaX;
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    Won't both those statements always be through though if the ball is inside the square?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Pong - Y-Axis Collision Detection

    Quote Originally Posted by Zelaine View Post
    Won't both those statements always be through though if the ball is inside the square?
    The trick is to not let it get inside the square. You check where the ball will be *before you move it*, that's what I meant by deltaX and deltaY. This is all explained in more detail in the tutorials though.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    Yeah, I know what deltaX and deltaY are. This is what I did after having read your tutorial, why doesn't it work?

    Java Code:
    if(Rectangle.intersects(LEFT_BAT.getLeft(), LEFT_BAT.getRight(), LEFT_BAT.getTop(), LEFT_BAT.getBottom(), BALL.getLeft(), BALL.getRight(), BALL.getTop() + BALL.getDY(), BALL.getBottom() + BALL.getDY()))
    	BALL.setDX(-BALL.getDY());
    else if(Rectangle.intersects(RIGHT_BAT.getLeft(), RIGHT_BAT.getRight(), RIGHT_BAT.getTop(), RIGHT_BAT.getBottom(), BALL.getLeft(), BALL.getRight(), BALL.getTop() + BALL.getDY(), BALL.getBottom() + BALL.getDY()))
    	BALL.setDY(-BALL.getDY());
    else if(BALL.getY() + BALL.getDY() < DISPLAY.getSpanning() || BALL.getY() + BALL.getHeight() + BALL.getDY() > DISPLAY.getHeight() - DISPLAY.getSpanning())
    	BALL.setDY(-BALL.getDY());
    		
    if(Rectangle.intersects(LEFT_BAT.getLeft(), LEFT_BAT.getRight(), LEFT_BAT.getTop(), LEFT_BAT.getBottom(), BALL.getLeft() + BALL.getDX(), BALL.getRight() + BALL.getDX(), BALL.getTop(), BALL.getBottom()))
    	BALL.setDX(-BALL.getDX());
    else if(Rectangle.intersects(RIGHT_BAT.getLeft(), RIGHT_BAT.getRight(), RIGHT_BAT.getTop(), RIGHT_BAT.getBottom(), BALL.getLeft() + BALL.getDX(), BALL.getRight() + BALL.getDX(), BALL.getTop(), BALL.getBottom()))
    	BALL.setDX(-BALL.getDX());
    else if(BALL.getX() + BALL.getDX() < DISPLAY.getSpanning() || BALL.getX() + BALL.getWidth() + BALL.getDX() > DISPLAY.getWidth() - DISPLAY.getSpanning())
    	BALL.setDX(-BALL.getDX());

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Pong - Y-Axis Collision Detection

    I dunno, what does it do instead? Can you post an SSCCE?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: Pong - Y-Axis Collision Detection

    Because that code is so verbose that it becomes unreadable and you can only make a mistake in it which you then cannot spot.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    I dunno, what does it do instead?
    It just goes through the platforms like they were not there, however it bounces when hitting the walls.

    Can you post an SSCCE?
    Sure, that would mean a lot of code though.
    Last edited by Zelaine; 01-30-2014 at 10:47 PM.

  13. #13
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    I fixed it! Forgot to update the left, right, top and bottom values regularly xD

  14. #14
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Pong - Y-Axis Collision Detection

    Never mind... It bounces on the left and right sides but not on the top or bottom...

Similar Threads

  1. Pong--paddle collision algorithm help
    By sora628 in forum New To Java
    Replies: 5
    Last Post: 02-06-2013, 03:35 PM
  2. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 03:29 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-07-2011, 11:40 PM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 04:55 PM
  5. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •