Esempio n. 1
0
        public override CollisionData CheckCollision(Collider collider)
        {
            //these values will be returned if there isn't a collision
            bool    didCollide     = false;
            float   collisionDepth = 0;
            Vector2 collisionAngle = new Vector2();

            if (collider is CircleCollider)
            {
                CircleCollider circleCollider = (CircleCollider)collider;

                //check for collision
                Vector2 r = circleCollider.Body.Pos + circleCollider.Pos - (Body.Pos + Pos);
                collisionDepth = (radius + circleCollider.Radius) - r.Length();
                if (collisionDepth > 0)
                {
                    didCollide = true;

                    //collision angle
                    r.Normalize();
                    collisionAngle = r;
                }
            }
            else if (collider is RectangleCollider)
            {
                RectangleCollider rectangleCollider = (RectangleCollider)collider;

                Vector2 myPos    = Body.Pos + Pos;
                Vector2 otherPos = rectangleCollider.Body.Pos + rectangleCollider.Pos;

                //top left region
                if (myPos.X < otherPos.X && myPos.Y < otherPos.Y)
                {
                    Vector2 r = otherPos - myPos;
                    collisionDepth = radius - r.Length();
                    if (collisionDepth > 0)
                    {
                        didCollide = true;
                        r.Normalize();
                        collisionAngle = r;
                    }
                }
                //top right region
                else if (myPos.X > otherPos.X + rectangleCollider.Dimensions.X && myPos.Y < otherPos.Y)
                {
                    Vector2 r = otherPos + new Vector2(rectangleCollider.Dimensions.X, 0) - myPos;
                    collisionDepth = radius - r.Length();
                    if (collisionDepth > 0)
                    {
                        didCollide = true;
                        r.Normalize();
                        collisionAngle = r;
                    }
                }
                //bottom right region
                else if (myPos.X > otherPos.X + rectangleCollider.Dimensions.X && myPos.Y > otherPos.Y + rectangleCollider.Dimensions.Y)
                {
                    Vector2 r = otherPos + rectangleCollider.Dimensions - myPos;
                    collisionDepth = radius - r.Length();
                    if (collisionDepth > 0)
                    {
                        didCollide = true;
                        r.Normalize();
                        collisionAngle = r;
                    }
                }
                //bottom left region
                else if (myPos.X < otherPos.X && myPos.Y > otherPos.Y + rectangleCollider.Dimensions.Y)
                {
                    Vector2 r = otherPos + new Vector2(0, rectangleCollider.Dimensions.Y) - myPos;
                    collisionDepth = radius - r.Length();
                    if (collisionDepth > 0)
                    {
                        didCollide = true;
                        r.Normalize();
                        collisionAngle = r;
                    }
                }
                //if it isn't in any of those regions, it's basically a square
                else
                {
                    Collider      rectangularHull = new RectangleCollider(Body, CollisionGroup, new Vector2(Pos.X - Radius, pos.Y - Radius), new Vector2(radius * 2, radius * 2), OnCollision);
                    CollisionData collisionData   = rectangularHull.CheckCollision(rectangleCollider);
                    didCollide     = collisionData.DidCollide;
                    collisionDepth = collisionData.CollisionDepth;
                    collisionAngle = collisionData.CollisionAngle;
                }
            }

            return(new CollisionData(didCollide, collisionDepth, collisionAngle, this, collider));
        }
Esempio n. 2
0
        public override CollisionData CheckCollision(Collider collider)
        {
            //these values will be returned if there isn't a collision
            bool    didCollide     = false;
            float   collisionDepth = 0;
            Vector2 collisionAngle = new Vector2();

            if (collider is RectangleCollider)
            {
                RectangleCollider rectangleCollider = (RectangleCollider)collider;
                Vector2           myPos             = Body.Pos + Pos;
                Vector2           otherPos          = rectangleCollider.Body.Pos + rectangleCollider.Pos;

                //check for collision
                if (myPos.Y < otherPos.Y + rectangleCollider.Dimensions.Y && myPos.Y + Dimensions.Y > otherPos.Y && myPos.X < otherPos.X + rectangleCollider.Dimensions.X && myPos.X + Dimensions.X > otherPos.X)
                {
                    didCollide = true;

                    //check if it's a vertical collision
                    Vector2 r = otherPos - myPos;
                    float   collisionAngleConstant = (Dimensions.Y + rectangleCollider.Dimensions.Y) / (Dimensions.X + rectangleCollider.Dimensions.X);
                    if (Math.Abs(r.Y) >= collisionAngleConstant * Math.Abs(r.X))
                    {
                        if (myPos.Y < otherPos.Y)
                        {
                            collisionAngle = new Vector2(0, 1);
                            //collision depth
                            collisionDepth = myPos.Y + Dimensions.Y - otherPos.Y;
                        }
                        else
                        {
                            collisionAngle = new Vector2(0, -1);
                            //collision depth
                            collisionDepth = otherPos.Y + rectangleCollider.Dimensions.Y - myPos.Y;
                        }
                    }
                    //if it's a horizontal collision
                    else
                    {
                        if (myPos.X < otherPos.X)
                        {
                            collisionAngle = new Vector2(1, 0);
                            //collision depth
                            collisionDepth = myPos.X + Dimensions.X - otherPos.X;
                        }
                        else
                        {
                            collisionAngle = new Vector2(-1, 0);
                            //collision depth
                            collisionDepth = otherPos.X + rectangleCollider.Dimensions.X - myPos.X;
                        }
                    }
                }
            }
            else if (collider is CircleCollider)
            {
                CollisionData collisionData = collider.CheckCollision(this);
                didCollide     = collisionData.DidCollide;
                collisionDepth = collisionData.CollisionDepth;
                collisionAngle = -collisionData.CollisionAngle;
            }

            return(new CollisionData(didCollide, collisionDepth, collisionAngle, this, collider));
        }