Collision Detection Implementation

前端 未结 2 871
温柔的废话
温柔的废话 2021-01-03 15:09

I have a collision detection class that works by finding the distance between the centres and whether that distance is small enough to be a collision (see Collision Detectio

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 15:43

    I had the same problem recently. Circle overlap is easy to determine. With ellipses it's trickier, but not that bad. You play around with the ellipse equation for a while, and the result comes up:

    //Returns true if the pixel is inside the ellipse
    public bool CollisionCheckPixelInEllipse(Coords pixel, Coords center, UInt16 radiusX, UInt16 radiusY)
    {
       Int32 asquare = radiusX * radiusX;
       Int32 bsquare = radiusY * radiusY;
       return ((pixel.X-center.X)*(pixel.X-center.X)*bsquare + (pixel.Y-center.Y)*(pixel.Y-center.Y)*asquare) < (asquare*bsquare);
    }
    
    // returns true if the two ellipses overlap
    private bool CollisionCheckEllipses(Coords center1, UInt16 radius1X, UInt16 radius1Y, Coords center2, UInt16 radius2X, UInt16 radius2Y)
    {
        UInt16 radiusSumX = (UInt16) (radius1X + radius2X);
        UInt16 radiusSumY = (UInt16) (radius1Y + radius2Y);
    
        return CollisionCheckPixelInEllipse(center1, center2, radiusSumX, radiusSumY);
    }
    

提交回复
热议问题