public bool IsRectangleColliding( RectangleParticle p ) { p.GetCardXProjection(); double depthX = TestIntervals( p.BMin, p.BMax, minX, maxX ); if ( depthX == 0 ) return false; p.GetCardYProjection(); double depthY = TestIntervals( p.BMin, p.BMax, minY, maxY ); if ( depthY == 0 ) return false; p.SetXYMTD( depthX, depthY ); normal.Set( p.MTD.X / Math.Abs( depthX ), p.MTD.Y / Math.Abs( depthY ) ); return true; }
// TBD: This method is basically identical to the isCircleColliding of the // RectangleSurface class. Need some type of CollisionResolver class to handle // all collisions and move responsibility away from the Surface classes. public bool IsRectangleColliding( RectangleParticle p ) { p.GetCardXProjection(); double depthX = TestIntervals( p.BMin, p.BMax, minX, maxX ); if ( depthX == 0 ) return false; p.GetCardYProjection(); double depthY = TestIntervals( p.BMin, p.BMax, minY, maxY ); if ( depthY == 0 ) return false; // determine if the circle's center is in a vertex voronoi region bool isInVertexX = Math.Abs( depthX ) < radius; bool isInVertexY = Math.Abs( depthY ) < radius; if ( isInVertexX && isInVertexY ) { // get the closest vertex double vx = p.Curr.X + Sign( center.X - p.Curr.X ) * ( p.Width / 2 ); double vy = p.Curr.Y + Sign( center.Y - p.Curr.Y ) * ( p.Height / 2 ); p.Vertex.Set( vx, vy ); // get the distance from the vertex to circle center double dx = p.Vertex.X - center.X; double dy = p.Vertex.Y - center.Y; double mag = Math.Sqrt( dx * dx + dy * dy ); double pen = radius - mag; // if there is a collision in one of the vertex regions if ( pen > 0 ) { dx /= mag; dy /= mag; p.MTD.Set( dx * pen, dy * pen ); normal.Set( dx, dy ); return true; } return false; } else { // collision on one of the 4 edges p.SetXYMTD( depthX, depthY ); normal.Set( p.MTD.X / Math.Abs( depthX ), p.MTD.Y / Math.Abs( depthY ) ); return true; } }