SGDE.Physics.PhysicsBaby.AddBounce C# (CSharp) Method

AddBounce() public method

Add a bounce between two CollisionUnits based on the velocity.
public AddBounce ( CollisionUnit unit, CollisionUnit other ) : void
unit SGDE.Physics.Collision.CollisionUnit The main collision unit involved in the "bounce".
other SGDE.Physics.Collision.CollisionUnit The secondary collision unit involved in the "bounce".
return void
        public void AddBounce(CollisionUnit unit, CollisionUnit other)
        {
            Vector2 unitCircleCenter;
            Vector2 otherCircleCenter;
            int unitRadius;
            int otherRadius;
            Vector2 oldVelocity;

            Vector2 norm;
            Vector2 unitNorm;
            Vector2 unitTan = new Vector2();
            Vector2 oldVelocityOther = new Vector2(0, 0);
            float velocityNorm;
            float velocityTan;
            float velocityNormOther;
            float velocityTanOther;
            float newVelocityScalar;
            float newVelocityScalarOther;
            Vector2 newVelocityNorm;
            Vector2 newVelocityNormOther;
            Vector2 newVelocityTan;
            Vector2 newVelocityTanOther;
            Vector2 newVelocity;
            Vector2 newVelocityOther;
            float mass = 1;
            float massOther = 10000;
            Vector2 velocityDiff;

            if (unit.GetCollisionType() == CollisionUnit.CollisionType.COLLISION_CIRCLE && other.GetCollisionType() == CollisionUnit.CollisionType.COLLISION_CIRCLE)
            {
                unitCircleCenter = unit.GetCircleCenter();
                unitRadius = unit.GetCircleRadius();

                otherCircleCenter = other.GetCircleCenter();
                otherRadius = other.GetCircleRadius();

                oldVelocity = mVelocity;

                ////////////////

                norm = otherCircleCenter - unitCircleCenter;
                unitNorm = norm / ((float)Math.Sqrt(norm.X * norm.X + norm.Y * norm.Y));
                unitTan.X = unitNorm.Y * -1;
                unitTan.Y = unitNorm.X;

                velocityNorm = Vector2.Dot(unitNorm, oldVelocity);
                velocityTan = Vector2.Dot(unitTan, oldVelocity);
                velocityNormOther = Vector2.Dot(unitNorm, oldVelocityOther);
                velocityTanOther = Vector2.Dot(unitTan, oldVelocityOther);

                newVelocityScalar = (velocityNorm * (mass - massOther) + 2 * massOther * velocityNormOther) / (mass + massOther);
                newVelocityScalarOther = (velocityNormOther * (massOther - mass) + 2 * mass * velocityNorm) / (mass + massOther);

                newVelocityNorm = newVelocityScalar * unitNorm;
                newVelocityNormOther = newVelocityScalarOther * unitNorm;

                newVelocityTan = velocityTan * unitTan;
                newVelocityTanOther = velocityTanOther * unitTan;

                newVelocity = newVelocityNorm + newVelocityTan;
                newVelocityOther = newVelocityNormOther + newVelocityTanOther;

                newVelocity *= 0.99f;

                velocityDiff = newVelocity - mVelocity;
                mVelocity = newVelocity;

                // some fudge
                //if (velocityDiff.X > 1 || velocityDiff.X < -1 || velocityDiff.Y > 1 || velocityDiff.Y < -1)
                //{
                    mOwner.Translate(0.04f * (unitCircleCenter.X - otherCircleCenter.X), 0.04f * (unitCircleCenter.Y - otherCircleCenter.Y));
                //}
            }
            else
            {
                newVelocity = mVelocity * -0.8f;
                velocityDiff = newVelocity - mVelocity;
                mVelocity = newVelocity;
                unitCircleCenter = unit.GetCircleCenter();
                otherCircleCenter = unit.GetCollisionPoint(other);

                //if (velocityDiff.X > 1 || velocityDiff.X < -1 || velocityDiff.Y > 1 || velocityDiff.Y < -1)
                //{
                    mOwner.Translate(0.04f * (unitCircleCenter.X - otherCircleCenter.X), 0.04f * (unitCircleCenter.Y - otherCircleCenter.Y));
                //}
            }
        }