Defining a circular area for bounds in an Android game
I want to make a simple lunar lander varient with asteroids. I've got asteroids, but I'm stumped on how to make them defined areas with bounds that if my ship crosses causes the player to lose. Any tips about defining a circle pixel area? If it helps, I know the exact coordinates of my asteroids.
Thanks for your advice.
(Looking for collision-detection advice)
Edit: My persona开发者_StackOverflowl solution, slightly more verbose but no more helpful than the solution below :)
public boolean collisionDetection(double xa, double ya, double ra, double xb, double yb, double rb) {
double distance = Math.sqrt(Math.pow(xa-xb, 2) + Math.pow(ya-yb, 2));
if (distance < ra+rb)
return true;
else
return false;
}
This assumes you know the asteroids and ships co-ords and size:
float x = Asteroid.X - Ship.X;
float y = Asteroid.Y - Ship.Y;
float radius = Asteroid.Radius - Ship.Radius;
x *= x;
y *= y;
radius *= radius;
if((x+y) < radius)
{
//collision
}
EDIT: my function was wrong sorry, use this one
Thanks LarsH
精彩评论