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 3 of 3 FirstFirst 123
Results 51 to 74 of 74

Thread: Pong game - Collision detection

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

    Default Re: Pong game - Collision detection

    Alright so I got

    if ( x >= 1PX + 25 ) - Not sure what else to put.

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Do NOT use 25. Use the variable that defines the value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    That means I have to change the

    g.setPaint( Color.black );
    g.fillRect( p1X, p1Y, 25, 60);

    values?

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    It allows you to easily change the way the code works. Say you want a longer, thinner paddle. Change the values of two variables and the new version works.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Still can't get around how to collide with the ball and paddle.

    I've changed the rect to
    g.setPaint( Color.black );
    g.fillRect( p1X, p1Y, p1Width, p1Height);
    Not sure how the format for the if statement goes, because the paddle moves.

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    The paddle's location is defined by the values of variables. Look at the diagram you drew on paper.

    detect one collision at a time: The ball moving to the left towards paddle1

    Don't write any code until you can describe what the conditions are for a collision.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Well I know it has to hit the right side of the left paddle.
    This would require p1X + p1Width since that would equate to the top right side of it.
    Do I have to also include the pPU? Since it is moving......I'm so confused

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Forget about the paddle's moving for now.
    You x value for the paddle's right side looks reasonable. Now you need to see if the paddle's y values overlap where the ball is moving. The ball could go above or below the paddle.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Then the y value would be
    p1Y + p1Height

    Would I need to write seperate things for the x and y values? Or could they be incased in the same ()?

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Can you describe the tests to be made for the ball at the same y values as the paddle?
    What is the purpose of the expression you posted? It gives 1 y value. The paddle has a top and a bottom.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    So the code should look something like

    if ( x >= p1X+p1Width+HALF_BALL_SIZE ) x_inc = -1*S;
    if ( y >= p1Y+p1Height+p1Width+HALF_BALL_SIZE ) y_inc = -1*S;
    if ( y >= p1Y+HALF_BALL_SIZE ) y_inc = -1*S;

    But if the ball hits the top if stays up there, and the border is locked at the width so its stuck in a small rectangle box.

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Can you explain in English what you are trying to write the code to test for?

    Your 3 independent if tests do not test the condition correctly.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    I'm trying to make it so when the ball hits the paddle, it bounces off it. I read about the intersect code but I'm not sure how that works.

    So the whole of the paddle should make it bounce back.
    So if the ball hit between the top right to the bottom right of the paddle, it will bounce off.

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    if the ball hit between the top right to the bottom right of the paddle
    The tests for the location of the ball must all be true. Your 3 if tests were independent of each other.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    How would I go about coding it?

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Use the AND operator (&&) to connect all the conditions into a single condition.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    No clue on how to use that. My knowledge of java programming is very low.

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Then you are trying to run before learning to walk.

    Look in the code for &&. It is used in several places.
    Last edited by Norm; May 13th, 2012 at 02:55 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Hokap (May 13th, 2012)

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

    Default Re: Pong game - Collision detection

    It was just a code I found that created a paddle.
    Oh well, guess I'll send the code like this, only have two hours left before submitting time.

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

    Default Re: Pong game - Collision detection

    Guess its closed.

    Thanks for the help Norm.

    Even though I didn't get anything accomplished.

  22. #71
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    Good luck learning java. You started a little late with so much to learn to get this done so soon.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Yeah I'm slacked a bit.

    But I solved it
    {
                 if ( x <= p1X+p1Width && x >=p1X)
          if (y >=p1Y && y <= p1Y+p1Height ) {
          x_inc = 1*S;
          y_inc = 1*S;
       }
    }

    Just figuring out how to make it turn direction on how it hits the box.

  24. #73
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Pong game - Collision detection

    How do you know you want to change the y_inc value?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Because whenever it hits the paddle box, it goes down every time. I'm wondering if theres a way that the ball hits the paddle makes it go up or down. But if it requires a bit more work I guess I'll leave it.

Page 3 of 3 FirstFirst 123

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