Example #1
0
        public override void Collision(CircleCollider other)
        {
            if (other.entity.Equals(this.entity))
            {
                return;
            }
            // ABVector is the vector between this object and the other object.
            Vector2 ABVector = other.center - this.center;
            // Distance Between Centers is the distance between the 2 collider centers.
            float distanceBetweenCenters = ABVector.Length();

            // If this is greater than the sum of the radii, a collision has occured.
            if (distanceBetweenCenters <= this.radius + other.radius)
            {
                // The collision normal is the ABVector, converted into a Unit vector (or normalized)
                Vector2 normal = ABVector;
                normal.Normalize();
                // We correct interpenetration BEFORE physics has a chance to occur.
                // The distanceIntersecting is the distance between the centers, minus the sum of the radii.
                float distanceIntersecting = (distanceBetweenCenters - (radius + other.radius));
                // We push each object along the collision normal as far as they have interpenetrated.
                entity.transform.SetPosition(entity.transform.position + (normal * distanceIntersecting));
                // Now that we have corrected interpenetration, we call OnCollision on the entity,
                // which will perform physics behaviours if it has a PhysicsBody component.
                ICollisionListener collisionEntity = entity as ICollisionListener;
                collisionEntity.OnCollision(other, normal);
            }
        }
Example #2
0
        public override void Collision(BoxCollider other)
        {
            if (other.entity.Equals(this.entity))
            {
                return;
            }
            // If the distance between the two collider's center is greater than the combined halfSize's, then a collision has occured.
            Vector2 centers = new Vector2(Math.Abs(this.center.X - other.center.X), Math.Abs(this.center.Y - other.center.Y));
            Vector2 halves  = (this.size + other.size) / 2;

            if ((centers.X < halves.X) && (centers.Y < halves.Y))
            {
                // Inform the Entity that they have been collided with.
                ICollisionListener collisionEntity = entity as ICollisionListener;
                collisionEntity.OnCollision(other);
            }
        }
Example #3
0
        public override void Collision(PlaneCollider other)
        {
            // Create a new Vector2, with position equal to the Plane's position.
            Vector2 testVector = other.position;
            // TestVariable is what the distance is checked against.
            float testVariable;

            if (other.horizontal)
            {
                if (this.position.X < other.position.X || this.position.X > other.position.X + other.width)
                {
                    return;
                }
                // If the Plane is horizontal
                // Shift the TestVector along the X-Axis so it is equal to this colliders center.
                testVector.X = this.center.X;
                testVariable = this.size.Y / 2;
            }
            else
            {
                // If the plane is Vertical
                // Shift the Testvector along the Y-Axis so it is equal to this colliders center.
                testVector.Y = this.center.Y;
                testVariable = this.size.X / 2;
            }

            if (Vector2.Distance(this.center, testVector) <= testVariable)
            {
                // Collision normal
                Vector2 normal;
                if (other.horizontal)
                {
                    if (testVector.Y >= this.center.Y)
                    {
                        // If we're above the plane, the normal is directly up.
                        normal = -Vector2.UnitY;
                        // Correct interpenetration.
                        entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.center.Y + testVariable), testVector)));
                    }
                    else
                    {
                        // If we're below the plane, the normal is directly down.
                        normal = Vector2.UnitY;
                        // Correct interpenetration.
                        entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.position.Y), testVector)));
                    }
                }
                else
                {
                    if (testVector.X <= testVariable)
                    {
                        // If we're to the right of the plane
                        normal = Vector2.UnitX;
                        // Correct interpenetration.
                        entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.position.Y), testVector)));
                    }
                    else
                    {
                        normal = -Vector2.UnitX;
                        entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X + testVariable, this.center.Y), testVector)));
                    }
                }
                // Resolve the collision.
                ICollisionListener collisionEntity = entity as ICollisionListener;
                collisionEntity.OnCollision(other, normal);
            }
        }