Example #1
0
        public IntersectData IntersectsAABB(AABB _other)
        {
            IntersectData IntersectData = new IntersectData();
            IntersectData.collider = _other;

            return IntersectData;
        }
Example #2
0
        public IntersectData IntersectsPoint(Point _other)
        {
            IntersectData IntersectData = new IntersectData();
            IntersectData.collider = _other;

            float deltaX = _other.center.X - center.X;
            float pointX = dimensions.X / 2 - Math.Abs(deltaX);

            if (pointX <= 0)
            {
                IntersectData.collision = false;
                return IntersectData;
            }

            float deltaY = _other.center.Y - center.Y;
            float pointY = dimensions.Y / 2 - Math.Abs(deltaY);

            if (pointY <= 0)
            {
                IntersectData.collision = false;
                return IntersectData;
            }

            if (pointX < pointY)
            {
                float sx = Math.Sign(deltaX);
                IntersectData.Delta = new Vector2(pointX * sx, 0f);
                IntersectData.Normal = new Vector2(sx, 0);
                IntersectData.Point = new Vector2(center.X + ((dimensions.X / 2) * sx), _other.center.Y);
            }
            else
            {
                float sy = Math.Sign(deltaY);
                IntersectData.Delta = new Vector2(0f, pointY * sy);
                IntersectData.Normal = new Vector2(0f, sy);
                IntersectData.Point = new Vector2(_other.center.X, center.Y + ((dimensions.X / 2) * sy));
            }

            IntersectData.collision = true;
            return IntersectData;
        }
Example #3
0
        public IntersectData IntersectsPoint(Point _other)
        {
            IntersectData IntersectData = new IntersectData();
            IntersectData.collider = _other;

            float distance = Vector2.Distance(_other.center, center);

            if (distance > radius)
            {
                IntersectData.collision = false;
                return IntersectData;
            }

            Vector2 direction = _other.center - center;
            direction.Normalize();

            IntersectData.Point = direction * m_radius;
            IntersectData.Normal = direction;

            return IntersectData;
        }
Example #4
0
        public void ResolvePhysics()
        {
            List<PhysicsObject> copyList = new List<PhysicsObject>(m_physicsObjects);

            foreach (PhysicsObject that in m_physicsObjects)
            {
                copyList.Remove(that);
                foreach (PhysicsObject other in copyList)
                {
                    Collider thatc = that.GetPhysicsComponent<Collider>();
                    Collider otherc = other.GetPhysicsComponent<Collider>();
                    if (thatc == null || otherc == null)
                        break;

                    IntersectData IntersectData = new IntersectData();
                    if (thatc.colliderType == ColliderType.Point && otherc.colliderType == ColliderType.Point)
                    {
                        IntersectData = ((Point)thatc).IntersectsPoint((Point)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }
                    else if (thatc.colliderType == ColliderType.AABB && otherc.colliderType == ColliderType.Point)
                    {
                        IntersectData = ((AABB)thatc).IntersectsPoint((Point)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }
                    else if (thatc.colliderType == ColliderType.AABB && otherc.colliderType == ColliderType.AABB)
                    {
                        IntersectData = ((AABB)thatc).IntersectsAABB((AABB)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }
                    /*
                    else if (thatc.colliderType == ColliderType.AABB && otherc.colliderType == ColliderType.Circle)
                    {
                        IntersectData = ((AABB)thatc).IntersectsCircle((Circle)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }*/
                    else if (thatc.colliderType == ColliderType.Circle && otherc.colliderType == ColliderType.Point)
                    {
                        IntersectData = ((Circle)thatc).IntersectsPoint((Point)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }
            /*
                    else if (thatc.colliderType == ColliderType.Circle && otherc.colliderType == ColliderType.Circle)
                    {
                        IntersectData = ((Circle)thatc).IntersectsCircle((Circle)otherc);
                        if (IntersectData.collision)
                            CalculateResolution(that, other, IntersectData);
                    }
                    */
                    if (IntersectData.collision)
                    {
                        object[] para = new object[1];
                        para[0] = IntersectData;
                        thatc.physicsObject.gameObject.InvokeMethod("Collision", para);
                        IntersectData.collider = thatc;
                        otherc.physicsObject.gameObject.InvokeMethod("Collision", para);
                    }
                }
            }
        }
Example #5
0
        private void CalculateResolution(PhysicsObject that, PhysicsObject other, IntersectData _IntersectData)
        {
            if (that.massType == Physics_MassType.DontInteract || other.massType == Physics_MassType.DontInteract)
                return;

            //We want that 'that' is always the one moving towards 'other'

            if (that.v.LengthSquared() < other.v.LengthSquared())
            {
                PhysicsObject tmp = that;
                that = other;
                other = tmp;
            }

            Vector2 thatov = that.v;
            Vector2 otherov = that.v;
            float k = (that.bouncyness + other.bouncyness) / 2f;

            if (Vector2.Dot(that.v, _IntersectData.Normal) != 0f)
            {
                if (that.massType == Physics_MassType.Zero || other.massType == Physics_MassType.Infitive)
                {
                    that.v = (that.v - new Vector2(that.v.X * Math.Abs(_IntersectData.Normal.X), that.v.Y * Math.Abs(_IntersectData.Normal.Y)));
                    //Vector2 friction = Vector2.Normalize((float)Math.Sin(that.v.X) * new Vector2(_IntersectData.Normal.Y , _IntersectData.Normal.X));
                    //that.AddForce(friction);
                    //Console.WriteLine(friction.ToString());
                }
                else if (that.massType == Physics_MassType.Infitive || other.massType == Physics_MassType.Zero)
                {
                    other.v = -other.v * k;
                }
                else
                {
                    that.v = (that.m * that.v + other.m * other.v - other.m * (that.v - other.v) * k) / (that.m + other.m);
                    other.v = (other.m * other.v + that.m * thatov - that.m * (other.v - thatov) * k) / (that.m + other.m);
                }
            }

            that.gameObject.transform.position -= _IntersectData.Delta;
            /*
            if (that.massType == other.massType)
            {

                that.v = other.v * that.bouncyness + thatov * (1 - that.bouncyness);

                other.v = that.v * other.bouncyness + otherov * (1 - other.bouncyness);

                that.gameObject.transform.position += _IntersectData.Delta * _IntersectData.Normal;
                other.gameObject.transform.position -= _IntersectData.Delta * _IntersectData.Normal;

            }else if (that.massType == Physics_MassType.Zero || other.massType == Physics_MassType.Infitive)
            {
                that.v = (-that.v + (other.v - that.v)) * that.bouncyness;

                that.gameObject.transform.position -= _IntersectData.Delta * _IntersectData.Normal;
            }
            else if (other.massType == Physics_MassType.Zero || that.massType == Physics_MassType.Infitive)
            {
                other.v = (-other.v + (that.v - other.v)) * that.bouncyness;

                other.gameObject.transform.position -= _IntersectData.Delta * _IntersectData.Normal;
            }else
            {
            }*/
        }
Example #6
0
        public IntersectData IntersectsRayCast(RayCast _other)
        {
            IntersectData IntersectData = new IntersectData();
            IntersectData.collider = _other;

            float scaleX = 1.0f / (_other.direction.X * _other.distance);
            float scaleY = 1.0f / (_other.direction.Y * _other.distance);

            float signX = Math.Sign(scaleX);
            float signY = Math.Sign(scaleY);

            float nearTimeX = (center.X - signX * (dimensions.X / 2) - _other.center.X) * scaleX;
            float nearTimeY = (center.Y - signY * (dimensions.Y / 2) - _other.center.Y) * scaleY;

            float farTimeX = (center.X + signX * (dimensions.X / 2) - _other.center.X) * scaleX;
            float farTimeY = (center.Y + signY * (dimensions.Y / 2) - _other.center.Y) * scaleY;

            if (nearTimeX > farTimeY || nearTimeY > farTimeY)
            {
                IntersectData.collision = false;
                return IntersectData;
            }

            float nearTime = nearTimeX > nearTimeY ? nearTimeX : nearTimeY;
            float farTime = farTimeX > farTimeY ? farTimeX : farTimeY;

            float time = nearTime < 0 ? 0 : nearTime > 1 ? 1 : nearTime;

            if (nearTime >= 1 | farTime <= 0)
            {
                IntersectData.collision = false;
                return IntersectData;
            }

            if (nearTimeX > nearTimeY)
                IntersectData.Normal = new Vector2(-signX, 0);
            else
                IntersectData.Normal = new Vector2(0, -signY);
            IntersectData.Delta = new Vector2(time * (_other.direction.X * _other.distance), time * (_other.direction.Y * _other.distance));

            IntersectData.collision = true;
            return IntersectData;
        }