Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 3 123 LastLast
Results 1 to 25 of 74

Thread: Pong game - Collision detection

  1. #1
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Pong game - Collision detection

    Hello, I'm just wondering how would I programme a collision detection for a pong 2 paddle java game?

    If its required I will copy and paste my code when asked!

    Thanks.

    Edit : I'm using BlueJ
    Last edited by Hokap; May 12th, 2012 at 03:02 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Depends some on how the shapes are defined. Some classes have methods that you could use, otherwise you'd have to write your own code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    Any good examples?
    Last edited by Hokap; May 13th, 2012 at 03:23 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Where is the collision detection code?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    I have no clue about the collision detection code or anything.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    If you define a collusion when the ball shape and the paddle shape touch or overlap, then you need code where you change the position of either to test for that condition.

    For drawing, you show not override the JFrame class's paint method. Create a class that extends the JPanel class, add it to the GUI, override its paintComponent method and do your drawing there.

    The ball seems to have good collision detection with the borders of the playing area. You have the logic there.

    The run() method should be on its own thread, not on the one used when main() is called.

    The formatting of the code in the run() method is confusing. There are no {} with many of the if statements. The code following the if statements is indented as if it is inside of the if statement.

    I'd suggest always using {}s with loops and if statements to avoid this confusion.
    Last edited by Norm; May 12th, 2012 at 07:03 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    Well it wasn't my code, I just had to add a paddle in and do collision detection, I read an example for the paddle and did something similar. But I don't understand the collision detection it showed, so looking for help.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Then study it to see how it detects the ball's collisions with the sides.
    Add some printlns to show the values of the variables when a collision is detected.

    A suggestion for testing: Make the frame smaller so the collisions happen sooner: change values of H and W to 1/2 current values.
    Last edited by Norm; May 12th, 2012 at 07:20 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    Ah I'll try again tomorrow. No clue on what I'm doing lol.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    If you add printlns to the code where it changes the values of the locations for the ball, you will see what the code does to dectect a collision and react to it. Print out the values of all the variables and expressions used in the run method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    I'm wondering how I would start a collision.

    I don't know what varibles I stick in to this code

    {
    if ( p1X <= HALF_BALL_SIZE ) ?? ;

    Or something along those lines.
    Last edited by Hokap; May 13th, 2012 at 09:47 AM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Did you analyze the ball's collision with the sides?

    What does the code you posted test for? p1X <= HALF_BALL_SIZE
    What does it mean if the expression
    is true?
    if its false?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    I tried System.out.Println in different places but no results get shown in the application window.

    And I don't know why I posted that, just tried copying the half ball size part.

    Was actually expecting to use

    p1X.intersects(HALF_BALL_SIZE)); or something along those lines, but it doesn't compile saying something.
    Any fixes on that?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    You need to think about the two objects in a two dimension area. Take a piece of paper and draw the paddle and label the positions of its four corners. Then draw the ball as it collides with one of the sides of the paddle. Label its position. Now look at the x,y values that you have made as labels. Notice the difference between of the x,y locations before a collision and at a collision.

    Don't try writing any code until you understand the relationships of the positions of the paddle and the ball.

    I tried System.out.println in different places but no results get shown in the application window.
    Then the code was not being executed. Move the printlns to where the code is executed.
    You need to print out the values of the variables used in the run method and understand how the ball bounces off the sides of the area it is in. You are wasting your time writing code before you understand what the code needs to do.

    Post the code that shows where your println statements are and what they are printing.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    I know I'm wasting time, but I've got a few hours before I have to hand in it. Otherwise I'll just hand it in like this =/

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Did you analyze how the ball's collisions with the side works?
    Adding printlns to the code will help.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    Adding the codes where? I tried adding in between the lines of this

    if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S;
    if ( x <= 0+B+HALF_BALL_SIZE ) x_inc = 1*S;
    if ( y >= H-B-HALF_BALL_SIZE ) y_inc = -1*S;
    if ( y <= 0+M+HALF_BALL_SIZE ) y_inc = 1*S;

    And I still get blank results.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    I still get blank results
    There are 4 if statements there and no printlns. There won't be anything printed without a println.

    Take the statements one at a time. What is the first one testing? What are the values of all the variables used in that if statement?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    if ( x >= W-B-HALF_BALL_SIZE ) x_inc = -1*S;
    System.out.println(); // Nothing shown
    if ( x <= 0+B+HALF_BALL_SIZE ) x_inc = 1*S;
    System.out.println(); // Nothing shown
    if ( y >= H-B-HALF_BALL_SIZE ) y_inc = -1*S;
    System.out.println(); // Nothing shown
    if ( y <= 0+M+HALF_BALL_SIZE ) y_inc = 1*S;
    System.out.println(); // Nothing shown

    Every time theres nothing shown, and it just keeps refreshing.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    System.out.println(); // Nothing shown
    What did you expect to be shown with that code?

    Did you expect the compiler or the java program to find something useful and print it? Where did you tell the computer what you wanted printed?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    No idea if I'm honest.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    Do a Search here on the forum for code samples that use println
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    I used
    System.out.println (x);

    A load of numbers just popped up, but its no difference from the application I was given as it already shows the x and y positions of the ball at all times.
    So I'm still lost on how to accomplish this task concerning the paddle and the ball.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong game - Collision detection

    The purpose of the println is to show you the values of variables as the code executes. How are the values of the variables used to control the logic of the execution? When is the if statement true?
    What happens to the value of x_inc ?

    Put the println inside of the true condition for the if() {<HERE>} to show you when and how the variables values change.
    If you understand what those 4 if statements do, you don't need to see. If you do not understand. then seeing will help you understand.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    May 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pong game - Collision detection

    Put in where?

    if ( x >= W-B-HALF_BALL_SIZE ) Here? x_inc = -1*S;

Page 1 of 3 123 LastLast

Similar Threads

  1. Noob, Collision Detection and Score
    By gkovr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 24th, 2012, 07:53 PM
  2. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM