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
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.
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.
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.
Any good examples?
Last edited by Hokap; May 13th, 2012 at 03:23 PM.
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.
I have no clue about the collision detection code or anything.
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.
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.
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.
Ah I'll try again tomorrow. No clue on what I'm doing lol.
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.
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.
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.
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?
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.
Then the code was not being executed. Move the printlns to where the code is executed.I tried System.out.println in different places but no results get shown in the application window.
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.
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 =/
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.
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.
There are 4 if statements there and no printlns. There won't be anything printed without a println.I still get blank results
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.
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.
What did you expect to be shown with that code?System.out.println(); // Nothing shown
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.
No idea if I'm honest.
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.
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.
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.
Put in where?
if ( x >= W-B-HALF_BALL_SIZE ) Here? x_inc = -1*S;