# Whats a good way to do collision detection in a simple JFrame game?

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im trying to write a simple pong game in java using swing for the GUI. I have a couple of ideas about how I could do the collision detection, but being a noob I'm not sure which is the best (if any). Could anyone help me with this, or at least point me to an article on the subject? Thanks.

##### Share on other sites
Hi I read your post and I think I can help. But first I need to know:

How do you want your game to look like?

Are you aiming for the absolute simplest version(the ball updates 1-5 times a second), or the more modern version(the ball have a fluid motion)?
Is the ball round or square?
What happens if it hits a corner of the paddle?

##### Share on other sites
[quote name='Lars-Kristian' timestamp='1349544294' post='4987449']
Hi I read your post and I think I can help. But first I need to know:

How do you want your game to look like?

Are you aiming for the absolute simplest version(the ball updates 1-5 times a second), or the more modern version(the ball have a fluid motion)?
Is the ball round or square?
What happens if it hits a corner of the paddle?
[/quote]

I'm just doing the absolute simple version for now. Unless the paddle is moving, the ball will act the same no matter where it hits the paddle. The ball is a square.

##### Share on other sites
[url="http://www.metanetsoftware.com/technique/tutorialA.html"]http://www.metanetso.../tutorialA.html[/url]

Under the "calculating the projection vector" section.

You can:
1. Find the the smallest projection vector.
2. Move the ball on the outside of the paddle.(The length of the projection vector)
3. Reverse the acceleration of the ball in the direction of the collision.

Note: It can also be useful too not allow movement of the paddle if the ball hits the top or bottom side of the paddle, so that the ball and paddle does not collide 2 time in a row.

This is not the simplest method too find the collision, but it is very useful when it comes too collision handling.

If this does not make sense, please let me know. xD Edited by Lars-Kristian

##### Share on other sites
[quote name='Lars-Kristian' timestamp='1349552189' post='4987487']
[url="http://www.metanetsoftware.com/technique/tutorialA.html"]http://www.metanetso.../tutorialA.html[/url]

Under the "calculating the projection vector" section.

You can:
1. Find the the smallest projection vector.
2. Move the ball on the outside of the paddle.(The length of the projection vector)
3. Reverse the acceleration of the ball in the direction of the collision.

Note: It can also be useful too not allow movement of the paddle if the ball hits the top or bottom side of the paddle, so that the ball and paddle does not collide 2 time in a row.

This is not the simplest method to find the collision, but it is very useful when it comes to collision handling.

If this does not make sense, please let me know. xD
[/quote]

Great, that helps a lot. Thanks for the help!

##### Share on other sites

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.