WOuld it be effective to use the distance formula to test for collision detection between two moving images (characters)? or is there some other more efficient method. I figured this wouldnt be too hard to implement
Assuming your image is a square, no. You could use the AABB method I guess but if its 2 squares you can do it like this:
public boolean collides(Rect a, Rect b) {
if(a.x < b.x + b.width) return true;
if(a.y < b.y + b.height) return true;
if(a.x + a.width > b.x) return true;
if(a.y + a.height > b.y) return true;
return false;
}
However, if your characters are circles, then yes. You can use the distance formula and check if the distance returned is less than the radius of both circles added together. Then there is a collision.
Well the game i had in mind was a circle colliding with a square image. What do you think would be best for that?
Yeah, use AABB: http://www.java-gaming.org/topics/basic-collision-detection/27326/view.html
He explains it in the best possible way
thanks for the help!
A circle to circle test isn’t expensive either…
public boolean collides(Circle a, Circle b) {
float dx = a.x - b.x, dy = a.y - b.y;
float dist = a.radius + b.radius;
return dx*dx + dy*dy < dist*dist;
}