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).
Well, firstly, you forgot to have an
elseclause:Someone else already pointed out this can be shortened as follows:
(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 wantMath.pow()?Or, you can also just use
Math.hypotrather than rolling your own!