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; } }
public SpringBox( double px, double py, double w, double h, out PhysicsObject[] objs) { // top left p0 = new RectangleParticle(px - w / 2, py - h / 2, 1, 1); // top right p1 = new RectangleParticle(px + w / 2, py - h / 2, 1, 1); // bottom right p2 = new RectangleParticle(px + w / 2, py + h / 2, 1, 1); // bottom left p3 = new RectangleParticle(px - w / 2, py + h / 2, 1, 1); p0.Visible = false; p1.Visible = false; p2.Visible = false; p3.Visible = false; objs = new PhysicsObject[10]; objs[0] = p0; objs[1] = p1; objs[2] = p2; objs[3] = p3; // edges objs[4] = new SpringConstraint(p0, p1); objs[5] = new SpringConstraint(p1, p2); objs[6] = new SpringConstraint(p2, p3); objs[7] = new SpringConstraint(p3, p0); // crossing braces objs[8] = new SpringConstraint(p0, p2); objs[9] = new SpringConstraint(p1, p3); }
public void ResolveRectangleCollision( RectangleParticle p, Engine engine ) { if ( IsRectangleColliding( p ) ) { OnContact(); p.OnContact(); p.ResolveCollision( normal, engine ); } }
public bool IsRectangleColliding( RectangleParticle p ) { p.GetCardYProjection(); double depthY = TestIntervals( p.BMin, p.BMax, minY, maxY ); if ( depthY == 0 ) return false; p.GetCardXProjection(); double depthX = TestIntervals( p.BMin, p.BMax, minX, maxX ); if ( depthX == 0 ) return false; p.GetAxisProjection( sideNormal ); double depthS = TestIntervals( p.BMin, p.BMax, minS, maxS ); if ( depthS == 0 ) return false; p.GetAxisProjection( faceNormal ); double depthF = TestIntervals( p.BMin, p.BMax, minF, maxF ); if ( depthF == 0 ) return false; double absX = Math.Abs( depthX ); double absY = Math.Abs( depthY ); double absS = Math.Abs( depthS ); double absF = Math.Abs( depthF ); if ( absX <= absY && absX <= absS && absX <= absF ) { p.MTD.Set( depthX, 0 ); collNormal.Set( p.MTD.X / absX, 0 ); } else if ( absY <= absX && absY <= absS && absY <= absF ) { p.MTD.Set( 0, depthY ); collNormal.Set( 0, p.MTD.Y / absY ); } else if ( absF <= absX && absF <= absY && absF <= absS ) { p.MTD = faceNormal.MultNew( depthF ); collNormal.Copy( faceNormal ); } else if ( absS <= absX && absS <= absY && absS <= absF ) { p.MTD = sideNormal.MultNew( depthS ); collNormal.Copy( sideNormal ); } return true; }
public virtual void ResolveRectangleCollision( RectangleParticle p, Engine engine, ref CollisionState state ) { }
public ISurface PickSurface(double x, double y, double tx, double ty) { RectangleParticle particle = new RectangleParticle(x - tx, y - ty, tx * 2, ty * 2); foreach (ISurface surface in Surfaces) { if (surface.IsRectangleColliding(particle)) { return surface; } } return null; }
public override void ResolveRectangleCollision( RectangleParticle p, Engine engine, ref CollisionState state ) { throw new Exception( "The method or operation is not implemented." ); }