This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Checking if my balls collide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Checking if my balls collide" Watch "Checking if my balls collide" New topic
Author

Checking if my balls collide

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I am trying to check if the balls in my animation collide, if they do then i want to reverse their direction. Currently i have the code below. All of my balls are added to a linkedlist when they are added to my JPanel. They all have a diameter of 20 and currently just bounce off the outer bounding box.
Currently i am getting the centre point of each ball and checking to see if the next ball is within a diameter of the other ball. If it is then they will touch and the speed of each will be reversed and the ball will travel in the opposite direction. It sort of works ish but not really ha. When i add two balls they stay almost static, switching direction back and forth. If i add a few more balls some of them move and some keep switching their direction. Occasionally balls will come until it looks like they will hit but then they move away (although the distance between them seems greater than i expected). Other times the balls dont hit.
So it is very buggy and not great at the moment. I have added this method into the timer which repaints the panel if that helps.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you need to make a triangle from x1,y1 to x2,y2
and from those you'll get width and height
calculate the hypotenuse from width, height
and if < diameter (or radius of both, if different sized balls) they collide
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Michael Dunn wrote:you need to make a triangle from x1,y1 to x2,y2
and from those you'll get width and height
calculate the hypotenuse from width, height
and if < diameter (or radius of both, if different sized balls) they collide


I am not sure if this is what you mean but this is what i have just tried.
It still ends up with unpredictable movements and not what i had intended.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Ok i have decided to try my old idea again. I have a box surrounding my ball, i update its co ordinates as i update my balls co ordinates. I then check if the two intersect.
Again this sort of works. It works a lot better than the idea above. The only problem is when they collide they get stuck together for a bit and then free themselves. Any idea why this might be?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> int side = x2-x1; etc

you would need to use Math.abs() to ensure no negative #'s, which would be < 20
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

First of all your collision check is wrong. The distance between 2 points is sqrt((x1-x2)^2+(y1-y2)^2)

Second, the way you are reversing directions is wrong. Balls don't move that way. Nothing moves that way. You might want to google conservation of momentum to understand theory socks behind ball collision

Third, I hope you are updating the ball positions in the same thread as ball collision. If the ball collision check is called multiple times before the ball positions are updated, they will reverse direction multiple times. This will result in your balls being weird
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Jayesh A Lalwani wrote:First of all your collision check is wrong. The distance between 2 points is sqrt((x1-x2)^2+(y1-y2)^2)

Second, the way you are reversing directions is wrong. Balls don't move that way. Nothing moves that way. You might want to google conservation of momentum to understand theory socks behind ball collision

Third, I hope you are updating the ball positions in the same thread as ball collision. If the ball collision check is called multiple times before the ball positions are updated, they will reverse direction multiple times. This will result in your balls being weird


The ball collision method is called in my ball thread which also handles the ball movement yes. I just wanted to create a simple animation that worked before i went to deeply into the physics and the angles which the balls were hit etc. I will check my collision formula though, thanks.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

Yeah, that makes sense, but you never know whether your balls are weird because the implementation of the ball physics is wrong, or there is something else wrong outside the physics calculations.

For debugging purposes. you might want to put something in your loop that will pause the loop at every step, or maybe put a very long sleep in the loop to slow down the animation. This will help you check if your balls move right when you take each step.
 
Consider Paul's rocket mass heater.
 
subject: Checking if my balls collide
 
Similar Threads
Drawing a Diamond Shape (like Rectangle, Circle)
Calculating the distance between two points problem
how do i draw a dotted straight line on an applet.
Ball Physics implementation.
drawRect() issues