Reprint to: http://www.cnblogs.com/Kurodo/archive/2012/08/08/2628688.html for a rectangular collision, many people know. But in the face of Polygon graphics, most of the way to use multi-rectangle overlay. But I don't really like this way, I'm using a classic algorithm: SAT a fast detection of irregular convex polygon collision of the algorithm to give two convex polygons, if we can find an axis, so that the two objects on this axis projection does not overlap, there is no collision between the two objects, This axis is called separating axis (red axis).
For 2D, the red line is the axis perpendicular to the polygon edge.
Therefore, if we want to check whether the two polygons collide, we check whether the projections of the two polygons overlap on each of the possible axes.
<summary>
Detects if 2 rectangles collide
</summary>
<returns></returns>
public static bool Isintersect (vector2[] A, vector2[] B)
{
Vector2 AX, AY, BX, by;
AX = new Vector2 ();
AY = new Vector2 ();
BX = new Vector2 ();
by = new Vector2 ();
Ax. X = a[0]. X-A[1]. X
Ax. Y = a[0]. Y-A[1]. Y
AY. X = a[0]. X-A[3]. X
AY. Y = a[0]. Y-A[3]. Y
Bx. X = b[0]. X-B[1]. X
Bx. Y = b[0]. Y-B[1]. Y
By. X = b[0]. X-B[3]. X
By. Y = b[0]. Y-B[3]. Y
For AX on:
if (TMP (AX, A, B)) return false;
if (TMP (AY, A, B)) return false;
if (TMP (BX, A, B)) return false;
if (Tmp (by, A, B)) return false;
return true;
}
private static bool Tmp (Vector2 is,vector2[] a,vector2[] B)
{
float[] v = new FLOAT[4];
for (int i = 0; i < 4; i++)
{
float TMP = (IS. X * A[i]. X + is. Y * A[i]. Y)/(IS. X * is. X + is. Y * is. Y);
V[I] = tmp * is. X * is. X + tmp * is. Y * is. Y
}
float[] vv = new FLOAT[4];
for (int i = 0; i < 4; i++)
{
float TMP = (IS. X * B[i]. X + is. Y * B[i]. Y)/(IS. X * is. X + is. Y * is. Y);
VV[I] = tmp * is. X * is. X + tmp * is. Y * is. Y
}
if (Math.max (Math.max (v[0], v[1]), Math.max (V[2],v[3])) >math.min (Math.min (vv[0],vv[1)), Math.min (Vv[2],vv[3])) && math.min (Math.min (v[0],v[1]), Math.min (V[2],v[3])) < Math.max (Math.max (vv[0],vv[1)), Math.max (vv[2), VV[3])) {
return false;
}//said it was not known whether the collision
else return true;//indicates that no collisions are known
}
H5 Game Development Polygon Collision Detection (Java code)