• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if my balls collide

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> int side = x2-x1; etc

you would need to use Math.abs() to ensure no negative #'s, which would be < 20
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic