Java Returning a Boolean Error
boolean, collision, java, methods, return
Solution
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);
}
Problem
I am a new-comer to the java language, and I'm confused as to why I am getting an error here. It's a very short piece of code that I seem to have a mental block about. Any suggestions? ``` public class Rigidbody { public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ if(Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)){ return true; } } } ``` Does anyone know what I'm missing here? (It's probably really obvious).