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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 74

Thread: Pong game - Collision detection

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Add {}s after the if and put the println inside the {}s
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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 (x_inc)}; ?

    Did that and I just see 1 when its moving to the right, and -1 when its moving to the left.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    You left the assignment statement outside of the {}s
    if (..)      { 
       x_inc = -1*S ;
       System.out.println ("x_inc=" +x_inc);
    }

    Do you know what makes the value change and how that changes the direction of movement?
    Last edited by Norm; May 13th, 2012 at 11:43 AM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Ermm

    Is it, if x is greater than the width - border offset - hall ball size it changes direction? and gives -1 values to x until it reaches the other side

    So if x > 790 it reverses and gives x values -1?

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Describe the place where x's value is reversed using terms of the shapes, not numeric values,
    Do NOT code 790. Use the values of variables. I'm testing with a window that is 300x400 defined using the W and H variables.

    gives x values -1?
    What variable? x is the location of the ball.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    x >= W - Halfballsize
    I know x_inc gives it a -1, but I don't understand how it does that.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    x_inc gives what a -1?
    Can you explain what variable? How does x_inc give the other variable a -1?

    Think about how the position of the ball changes. What should happen when the ball gets to a side?
    Just work with x values for now. How does the x value change as the ball moves to the right? What will be different when the ball moves to the left?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    When it goes to the right it gets 1 which adds to the x value, while going to the left gives it a -1

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    It's not really 1, its the value of the variable: S. Change S's value and you'll see.

    When does the sign of S change from + to - and from - to +?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Oh is that what S is for....... Yeah I changed S to 5 and when it hit a wall it sped up.

    The S changes depending on the condition that is met I'm guessing.

    As it is -1 or 1 * S.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    The code is poorly written when it comes to meaningful variable names.

    When does the sign of x_inc change?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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 ) -1
    if ( x <= 0+B+HALF_BALL_SIZE ) 1

    When either of these conditions are met.

    Yeah I was given it and had to make a pong game, the only parts I added were the paddles.

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Can you explain in English to show that you understand what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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;

    if x is greater or equal to the width - border offset - halfballsize, it would then call forth x_inc and multiply S by -1. Therefore creating a making the x_inc = -1

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    What visual thing do all those numbers represent?

    Now think in terms of the paddles. When would you want to change the sign of x_inc if the ball is moving to the left towards paddle1?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    When it touches the right side of paddle 1.
    I'm not sure what varibles to put in there, would I be using p1X ?

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Go to the paper and pencil diagram I asked you to draw. Label it with the variables that define the locations of the paddle and the ball. Then look at that drawing to get the variable names you need in the code.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    X variables for paddle is p1X , Y is p1Y.
    Ball position is x and y?

    Also what does this part mean?
    x += x_inc;
    y += y_inc;

    Thanks for the help btw. Might as well learn how to understand it, but short on time, so hope it gets done soon.

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    What does a change to the value of x do?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Hmm? Don't understand that question.

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    What is the variable: x used for in the program? What happens visually when the value of x is changed?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    The ball position

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Does that answer your question asked in post#43?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Pong game - Collision detection

    Was wondering what the += means.

    Also how would I get started on collision detecting the paddle and the ball?

    Is it similar to the layout of the wall collision?

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pong game - Collision detection

    Is it similar to the layout of the wall collision
    Yes, look at the diagram you've drawn on paper and get the variable names from that.

    var += val is shorthand notation for var = var + val.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 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