Well, firstly, you forgot to have an else
clause:
public boolean checkCircleCollision(float x1, float y1, float r1,
float x2, float y2, float r2) {
if (Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2)){
return true;
} else {
return false;
}
}
Someone else already pointed out this can be shortened as follows:
public boolean checkCircleCollision(float x1, float y1, float r1,
float x2, float y2, float r2) {
return Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2);
}
(make sure to upvote them for pointing those out :-)
However, your code is still wrong.
As stated here, Java's ^
operator is for exclusive bitwise OR, not exponentiation. Perhaps you want Math.pow()
?
Returns the value of the first argument raised to the power of the second argument.
public boolean checkCircleCollision(float x1, float y1, float r1,
float x2, float y2, float r2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) <= (r1 + r2);
}
Or, you can also just use Math.hypot
rather than rolling your own!
Returns sqrt(x^2 + y^2) without intermediate overflow or underflow.
public boolean checkCircleCollision(float x1, float y1, float r1,
float x2, float y2, float r2) {
return Math.hypot(x2 - x1, y2 - y1) <= (r1 + r2);
}