Example #1
0
        private void DistributeCollision()
        {
            if (Body1.OnContact.IsNotNull())
            {
                Body1.OnContact(Body2);
            }
            if (Body2.OnContact.IsNotNull())
            {
                Body2.OnContact(Body1);
            }

            if (Body1.IsTrigger || Body2.IsTrigger)
            {
                return;
            }

            switch (LeCollisionType)
            {
            case CollisionType.Circle_Circle:
                DistX = Body1._position.x - Body2._position.x;
                DistY = Body1._position.y - Body2._position.y;
                dist  = FixedMath.Sqrt((DistX * DistX + DistY * DistY) >> FixedMath.SHIFT_AMOUNT);

                if (dist == 0)
                {
                    const int randomMax = (int)((long)int.MaxValue % (FixedMath.One / 64));
                    Body1._position.x    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body1._position.y    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body1.PositionChanged = true;
                    Body2._position.x    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body2._position.y    += LSUtility.GetRandom(randomMax) - randomMax / 2;
                    Body2.PositionChanged = true;
                    return;
                }


                depth = (Body1.Radius + Body2.Radius - dist);

                if (depth <= 0)
                {
                    return;
                }
                DistX = (DistX * depth / dist) / 2L;
                DistY = (DistY * depth / dist) / 2L;

                const bool applyVelocity = false;
                //Resolving collision
                if (Body1.Immovable && Body1.isActiveAndEnabled || (Body2.Immovable == false && Body1.Priority > Body2.Priority))
                {
                    Body2._position.x    -= DistX;
                    Body2._position.y    -= DistY;
                    Body2.PositionChanged = true;
                    if (applyVelocity)
                    {
                        Body2._velocity.x    -= DistX;
                        Body2.VelocityChanged = true;
                    }
                }
                else if (Body2.Immovable || Body2.Priority > Body1.Priority)
                {
                    Body1._position.x    += DistX;
                    Body1._position.y    += DistY;
                    Body1.PositionChanged = true;
                    if (applyVelocity)
                    {
                        Body1._velocity.x    += DistX;
                        Body1._velocity.y    += DistY;
                        Body1.VelocityChanged = true;
                    }
                }
                else
                {
                    DistX /= 2;
                    DistY /= 2;

                    Body1._position.x += DistX;
                    Body1._position.y += DistY;
                    Body2._position.x -= DistX;
                    Body2._position.y -= DistY;

                    Body1.PositionChanged = true;
                    Body2.PositionChanged = true;
                    if (applyVelocity)
                    {
                        DistX                /= 8;
                        DistY                /= 8;
                        Body1._velocity.x    += DistX;
                        Body1._velocity.y    += DistY;
                        Body1.VelocityChanged = true;

                        Body2._velocity.x    -= DistX;
                        Body2._velocity.y    -= DistY;
                        Body2.VelocityChanged = true;
                    }
                }
                break;

            case CollisionType.Circle_AABox:
                if (Body1.Shape == ColliderType.AABox)
                {
                    DistributeCircle_Box(Body1, Body2);
                }
                else
                {
                    DistributeCircle_Box(Body2, Body1);
                }
                break;

            case CollisionType.Circle_Polygon:
                if (Body1.Shape == ColliderType.Circle)
                {
                    this.DistributeCircle_Poly(Body1, Body2);
                }
                else
                {
                    this.DistributeCircle_Poly(Body2, Body1);
                }
                break;
            }
        }
        public void DistributeCollision()
        {
            if (!Active)
            {
                return;
            }

            if (IsColliding)
            {
                if (Body1.OnContact != null)
                {
                    Body1.OnContact(Body2);
                }
                if (Body2.OnContact != null)
                {
                    Body2.OnContact(Body1);
                }

                if (DoPhysics)
                {
                    switch (LeCollisionType)
                    {
                    case CollisionType.Circle_Circle:
                        DistX = Body1.Position.x - Body2.Position.x;
                        DistY = Body1.Position.y - Body2.Position.y;
                        dist  = FixedMath.Sqrt((DistX * DistX + DistY * DistY) >> FixedMath.SHIFT_AMOUNT);

                        if (dist == 0)
                        {
                            Body1.Position.x     += (LSUtility.GetRandom(1 << (FixedMath.SHIFT_AMOUNT - 2)) + 1);
                            Body1.Position.y     -= (LSUtility.GetRandom(1 << (FixedMath.SHIFT_AMOUNT - 2)) + 1);
                            Body1.PositionChanged = true;
                            return;
                        }

                        depth = (Body1.Radius + Body2.Radius - dist);

                        if (depth < 0)
                        {
                            return;
                        }

                        DistX = (DistX * depth / dist);
                        DistY = (DistY * depth / dist);

                        //Resolving collision
                        if (Body1.Immovable)
                        {
                            Body2.Position.x -= DistX;
                            Body2.Position.y -= DistY;

                            Body2.PositionChanged = true;
                        }
                        else if (Body2.Immovable)
                        {
                            Body1.Position.x     += DistX;
                            Body1.Position.y     += DistY;
                            Body1.PositionChanged = true;
                        }
                        else
                        {
                            DistX /= 4;
                            DistY /= 4;
                            if (Body1.Velocity.Dot(Body2.Velocity.x, Body2.Velocity.y) < 0)
                            {
                                if (Body1.Mover != null)
                                {
                                    Body1.Velocity.x     += DistX;
                                    Body1.Velocity.y     += DistY;
                                    Body1.VelocityChanged = true;
                                }
                                else
                                {
                                    Body1.Position.x     += DistX;
                                    Body1.Position.y     += DistY;
                                    Body1.PositionChanged = true;
                                }
                                if (Body2.Mover != null)
                                {
                                    Body2.Velocity.x     -= DistX;
                                    Body2.Velocity.y     -= DistY;
                                    Body2.VelocityChanged = true;
                                }
                                else
                                {
                                    Body2.Position.x     -= DistX;
                                    Body2.Position.y     -= DistY;
                                    Body2.PositionChanged = true;
                                }
                            }
                            else
                            {
                                Body1.Position.x     += DistX;
                                Body1.Position.y     += DistY;
                                Body1.PositionChanged = true;
                                Body2.Position.x     -= DistX;
                                Body2.Position.y     -= DistY;
                                Body2.PositionChanged = true;
                            }
                        }
                        break;

                    case CollisionType.Circle_AABox:
                        if (Body1.Shape == ColliderType.AABox)
                        {
                            DistributeCircle_Box(Body1, Body2);
                        }
                        else
                        {
                            DistributeCircle_Box(Body2, Body1);
                        }
                        break;

                    case CollisionType.Circle_Polygon:

                        break;
                    }
                }
            }
        }
Example #3
0
        private void DistributeCollision()
        {
            if (Body1.OnContact.IsNotNull())
            {
                Body1.OnContact(Body2);
            }
            if (Body2.OnContact.IsNotNull())
            {
                Body2.OnContact(Body1);
            }

            if (DoPhysics && Body1.HasParent == false && Body2.HasParent == false)
            {
                switch (LeCollisionType)
                {
                case CollisionType.Circle_Circle:
                    DistX = Body1.Position.x - Body2.Position.x;
                    DistY = Body1.Position.y - Body2.Position.y;
                    dist  = FixedMath.Sqrt((DistX * DistX + DistY * DistY) >> FixedMath.SHIFT_AMOUNT);

                    if (dist == 0)
                    {
                        const int randomMax = 1000;
                        Body1.Position.x     += LSUtility.GetRandom(randomMax);
                        Body1.Position.y     += LSUtility.GetRandom(randomMax);
                        Body1.PositionChanged = true;
                        Body2.Position.x     += LSUtility.GetRandom(randomMax);
                        Body2.Position.y     += LSUtility.GetRandom(randomMax);
                        Body2.PositionChanged = true;
                        return;
                    }


                    depth = (Body1.Radius + Body2.Radius - dist);

                    if (depth <= 0)
                    {
                        return;
                    }
                    DistX = (DistX * depth / dist) / 2L;
                    DistY = (DistY * depth / dist) / 2L;

                    const bool applyVelocity = true;
                    //Resolving collision
                    if (Body1.Immovable || (Body2.Immovable == false && Body1.Priority > Body2.Priority))
                    {
                        Body2.Position.x     -= DistX;
                        Body2.Position.y     -= DistY;
                        Body2.PositionChanged = true;
                        if (applyVelocity)
                        {
                            Body2._velocity.x    -= DistX;
                            Body2._velocity.y    -= DistY;
                            Body2.VelocityChanged = true;
                        }
                    }
                    else if (Body2.Immovable || Body2.Priority > Body1.Priority)
                    {
                        Body1.Position.x     += DistX;
                        Body1.Position.y     += DistY;
                        Body1.PositionChanged = true;
                        if (applyVelocity)
                        {
                            Body1._velocity.x    += DistX;
                            Body1._velocity.y    += DistY;
                            Body1.VelocityChanged = true;
                        }
                    }
                    else
                    {
                        DistX /= 2;
                        DistY /= 2;

                        Body1.Position.x += DistX;
                        Body1.Position.y += DistY;
                        Body2.Position.x -= DistX;
                        Body2.Position.y -= DistY;

                        Body1.PositionChanged = true;
                        Body2.PositionChanged = true;
                        if (applyVelocity)
                        {
                            DistX                /= 8;
                            DistY                /= 8;
                            Body1._velocity.x    += DistX;
                            Body1._velocity.y    += DistY;
                            Body1.VelocityChanged = true;

                            Body2._velocity.x    -= DistX;
                            Body2._velocity.y    -= DistY;
                            Body2.VelocityChanged = true;
                        }
                    }
                    break;

                case CollisionType.Circle_AABox:
                    if (Body1.Shape == ColliderType.AABox)
                    {
                        DistributeCircle_Box(Body1, Body2);
                    }
                    else
                    {
                        DistributeCircle_Box(Body2, Body1);
                    }
                    break;

                case CollisionType.Circle_Polygon:

                    break;
                }
            }
        }
Example #4
0
        private void DistributeCollision()
        {
            if (Body1.OnContact != null)
            {
                Body1.OnContact(Body2);
            }
            if (Body2.OnContact != null)
            {
                Body2.OnContact(Body1);
            }

            if (DoPhysics && Body1.HasParent == false && Body2.HasParent == false)
            {
                switch (LeCollisionType)
                {
                case CollisionType.Circle_Circle:
                    DistX = Body1.Position.x - Body2.Position.x;
                    DistY = Body1.Position.y - Body2.Position.y;
                    dist  = FixedMath.Sqrt((DistX * DistX + DistY * DistY) >> FixedMath.SHIFT_AMOUNT);

                    if (dist == 0)
                    {
                        Body1.Position.x += (LSUtility.GetRandom(1 << (FixedMath.SHIFT_AMOUNT - 2)) + 1);
                        Body1.Position.y -= (LSUtility.GetRandom(1 << (FixedMath.SHIFT_AMOUNT - 2)) + 1);
                        return;
                    }

                    depth = (Body1.Radius + Body2.Radius - dist);

                    if (depth < 0)
                    {
                        return;
                    }

                    DistX = (DistX * depth / dist);
                    DistY = (DistY * depth / dist);

                    //Resolving collision
                    if (physicsFavor == PhysicsFavor.Favor1)
                    {
                        Body2.Position.x     -= DistX;
                        Body2.Position.y     -= DistY;
                        Body2.PositionChanged = true;
                    }
                    else if (physicsFavor == PhysicsFavor.Favor2)
                    {
                        Body1.Position.x     += DistX;
                        Body1.Position.y     += DistY;
                        Body1.PositionChanged = true;
                    }
                    else
                    {
                        DistX /= 4;
                        DistY /= 4;
                        if (Body1.Velocity.Dot(Body2.Velocity.x, Body2.Velocity.y) <= 0)
                        {
                            Body1.Velocity.x     += DistX;                        //FixedMath.Mul(DistX, Body1.VelocityMagnitude);
                            Body1.Velocity.y     += DistY;                        //FixedMath.Mul(DistY, Body1.VelocityMagnitude);
                            Body1.VelocityChanged = true;

                            Body2.Velocity.x     -= DistX;                        //FixedMath.Mul(DistX, Body2.VelocityMagnitude);
                            Body2.Velocity.y     -= DistY;                        //FixedMath.Mul(DistY, Body2.VelocityMagnitude);
                            Body2.VelocityChanged = true;

                            Body1.Position.x += DistX;
                            Body1.Position.y += DistY;
                            Body2.Position.x -= DistX;
                            Body2.Position.y -= DistY;
                        }
                        else
                        {
                            Body1.Position.x += DistX;
                            Body1.Position.y += DistY;
                            Body2.Position.x -= DistX;
                            Body2.Position.y -= DistY;
                        }
                        Body1.PositionChanged = true;
                        Body2.PositionChanged = true;
                    }
                    break;

                case CollisionType.Circle_AABox:
                    if (Body1.Shape == ColliderType.AABox)
                    {
                        DistributeCircle_Box(Body1, Body2);
                    }
                    else
                    {
                        DistributeCircle_Box(Body2, Body1);
                    }
                    break;

                case CollisionType.Circle_Polygon:

                    break;
                }
            }
        }