/** Check if the ball collides with any twinkles and convert into a score */ public void CheckBallCollisions(Ball b) { if (b != null) { var qb = new nGQuad(5f).Offset(b.Position); foreach (var t in Twinkles) { var qc = new nGQuad(t.Size).Offset(t.Position); if (qc.Intersects(qb)) { t.Die(); CreateScore(t); } } } }
/** If another quad intersects, non-rotated */ public bool Intersects(nGQuad q) { var rectA = Rect; var rectB = q.Rect; // Logging for debug // nLog.Debug("{0},{1} -> {2},{3} vs. {4},{5} -> {6},{7}", xMin, yMin, xMax, yMax, q.xMin, q.yMin, q.xMax, q.yMax); // For rotated test, see: // http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles var rtn = ((Math.Abs(rectA.x - rectB.x) < (Math.Abs(rectA.width + rectB.width) / 2)) && (Math.Abs(rectA.y - rectB.y) < (Math.Abs(rectA.height + rectB.height) / 2))); return rtn; }
public nGQuad(nGQuad parent) { Init(parent.xMin, parent.yMin, parent.xMax, parent.yMax); }
/** Check if two sprites intersect */ public static bool Intersects(nSprite a, nSprite b) { var q1 = new nGQuad(); q1.Points = a.Points.Raw; q1.Offset(a.Position[0], a.Position[1]); var q2 = new nGQuad(); q2.Points = b.Points.Raw; q2.Offset(b.Position[0], b.Position[1]); return q1.Intersects(q2); }