What you are posting is basic framing / conditional code for detecting a collision without the collision detection code. You haven't tried to write it yet.
Again the info you seek is found in the articles I mentioned. When two circles or even spheres collide all you have to do is see if the distance between the center points is less than the sum of the radii of the circles. Read this article again and you'll see this info in "figure 1" about a 1/4 of the way down the page; there is a graphic image that clearly shows what you need to do:http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/
So do study those articles and as I mentioned it's probably worth buying the full source code so you can simply translate what you need to Java, etc.
Because there are additional shortcuts / efficiency calculations I'll be nice and give you some pseudocode..
- Code: Select all
float combinedRadius = ball1.radius + ball2.radius
// Get the distance between the balls and check if there is a collision; note this is a short cut insofar
// that we are removing a square root calculation which is very expensive and are checking with the
// distances squared.
float dX, dY;
dX = ball1.x - ball2.x;
dY = ball1.y - ball2.y;
if (combinedRadius * combinedRadius > dX * dX + dY * dY)