Game Development Reference
In-Depth Information
// x+w did not overflow and x+w is smaller than X+W
if (w >= x && W > w) {
return false;
}
}
h += y;
H += Y;
if (H <= Y) {
if (h >= y || H > h) {
return false;
}
} else {
if (h >= y && H > h) {
return false;
}
}
return true;
}
public boolean contains(int x, int y) {
return inside(x, y);
}
public boolean inside(int X, int Y) {
int w = this.width;
int h = this.height;
if ((w | h) < 0) {
// At least one of the dimensions is negative...
return false;
}
// Note: if either dimension is zero, tests below must return false...
int x = this.x;
int y = this.y;
if (X < x || Y < y) {
return false;
}
w += x;
h += y;
// overflow || intersect
return ((w < x || w > X) && (h < y || h > Y));
}
}
Creating a Polygon Class for Asteroids
The standard J2SE API has the neat class
Polygon
that we can reuse for Asteroids. It is hard to understand
why Google has not included such useful classes from
java.awt
into the Android API. Listing 4-2 shows
the modified
Polygon
class (it is the same as the Java SE
Polygon
stripped for running in Android and
Asteroids). Some of the most interesting methods of this class follow: