Esempio n. 1
0
        private bool IsCircleColliding( CircleParticle 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 ) < p.Radius;
            bool isInVertexY = Math.Abs( depthY ) < p.Radius;

            if ( isInVertexX && isInVertexY )
            {
                // get the closest vertex
                double vx = center.X + Sign( p.Curr.X - center.X ) * ( rectWidth / 2 );
                double vy = center.Y + Sign( p.Curr.Y - center.Y ) * ( rectHeight / 2 );

                // get the distance from the vertex to circle center
                double dx = p.Curr.X - vx;
                double dy = p.Curr.Y - vy;
                double mag = Math.Sqrt( dx * dx + dy * dy );
                double pen = p.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;
            }
        }
Esempio n. 2
0
        private bool IsCircleColliding( CircleParticle 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;

            double dx = center.X - p.Curr.X;
            double dy = center.Y - p.Curr.Y;
            double len = Math.Sqrt( dx * dx + dy * dy );
            double pen = ( p.Radius + radius ) - len;

            if ( pen > 0 )
            {
                dx /= len;
                dy /= len;
                p.MTD.Set( -dx * pen, -dy * pen );
                normal.Set( -dx, -dy );
                return true;
            }

            return false;
        }