# Pong game - Collision detection

Show 40 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• May 13th, 2012, 11:30 AM
Norm
Re: Pong game - Collision detection
Add {}s after the if and put the println inside the {}s
• May 13th, 2012, 11:34 AM
Hokap
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.
• May 13th, 2012, 11:40 AM
Norm
Re: Pong game - Collision detection
You left the assignment statement outside of the {}s
Code :

```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?
• May 13th, 2012, 11:46 AM
Hokap
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?
• May 13th, 2012, 11:52 AM
Norm
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.

Quote:

gives x values -1?
What variable? x is the location of the ball.
• May 13th, 2012, 11:58 AM
Hokap
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.
• May 13th, 2012, 12:05 PM
Norm
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?
• May 13th, 2012, 12:07 PM
Hokap
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
• May 13th, 2012, 12:22 PM
Norm
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 +?
• May 13th, 2012, 12:27 PM
Hokap
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.
• May 13th, 2012, 12:30 PM
Norm
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?
• May 13th, 2012, 12:33 PM
Hokap
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.
• May 13th, 2012, 12:45 PM
Norm
Re: Pong game - Collision detection
Can you explain in English to show that you understand what the code is doing.
• May 13th, 2012, 12:48 PM
Hokap
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
• May 13th, 2012, 12:53 PM
Norm
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?
• May 13th, 2012, 12:58 PM
Hokap
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 ?
• May 13th, 2012, 01:01 PM
Norm
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.
• May 13th, 2012, 01:06 PM
Hokap
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.
• May 13th, 2012, 01:09 PM
Norm
Re: Pong game - Collision detection
What does a change to the value of x do?
• May 13th, 2012, 01:11 PM
Hokap
Re: Pong game - Collision detection
Hmm? Don't understand that question.
• May 13th, 2012, 01:14 PM
Norm
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?
• May 13th, 2012, 01:16 PM
Hokap
Re: Pong game - Collision detection
The ball position
• May 13th, 2012, 01:20 PM
Norm
Re: Pong game - Collision detection
• May 13th, 2012, 01:22 PM
Hokap
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?
• May 13th, 2012, 01:25 PM
Norm
Re: Pong game - Collision detection
Quote:

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.
Show 40 post(s) from this thread on one page
Page 2 of 3 First 123 Last