Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Sphere.cs
/// <summary>
/// Performs a bounce collision (a collision which modifies velocity and acceleration), and separates the objects if so.
/// </summary>
/// <param name="sphere">The Sphere to perform collision against.</param>
/// <param name="thisMass">The mass of this instance.</param>
/// <param name="otherMass">Th e mass of the argument cube.</param>
/// <param name="elasticity">The ratio of velocity to preserve.</param>
/// <returns>Whether a collision occurred.</returns>
Performs a bounce collision (a collision which modifies velocity and acceleration), and separates the objects if so.
Performs a bounce collision ( a collision which modifies velocity and acceleration ) and separates the objects if so .
public bool CollideAgainstBounce(Sphere sphere, float thisMass, float otherMass, float elasticity)
        {
#if DEBUG
            if (thisMass == 0 && otherMass == 0)
            {
                throw new ArgumentException("Both masses cannot be 0.  For equal masses pick a non-zero value");
            }
#endif
            if (CollideAgainstMove(sphere, thisMass, otherMass))
            {
                Vector3 relativeVelocity = new Vector3(
                    this.TopParent.XVelocity - sphere.TopParent.XVelocity,
                    this.TopParent.YVelocity - sphere.TopParent.YVelocity,
                    this.TopParent.ZVelocity - sphere.TopParent.ZVelocity);
#if FRB_MDX
                float velocityNormalDotResult = Vector3.Dot( relativeVelocity, LastMoveCollisionReposition);
#else
                float velocityNormalDotResult;
                Vector3.Dot(ref relativeVelocity, ref LastMoveCollisionReposition, out velocityNormalDotResult);
#endif
                if (velocityNormalDotResult >= 0)
                {
                    return true;
                }
#if FRB_MDX
                Vector3 reverseNormal = -Vector3.Normalize(LastMoveCollisionReposition);
                float length = Vector3.Dot(relativeVelocity, reverseNormal);
                Vector3 velocityOnNormal = Vector3.Multiply(reverseNormal, length);
#else
                Vector3 reverseNormal = -LastMoveCollisionReposition;
                reverseNormal.Normalize();
                float length = Vector3.Dot(relativeVelocity, reverseNormal);
                Vector3 velocityOnNormal;
                Vector3.Multiply(ref reverseNormal, length, out velocityOnNormal);
#endif
                sphere.TopParent.Velocity.X += (1 + elasticity) * thisMass / (thisMass + otherMass) * velocityOnNormal.X;
                sphere.TopParent.Velocity.Y += (1 + elasticity) * thisMass / (thisMass + otherMass) * velocityOnNormal.Y;
                sphere.TopParent.Velocity.Z += (1 + elasticity) * thisMass / (thisMass + otherMass) * velocityOnNormal.Z;
                this.TopParent.Velocity.X -= (1 + elasticity) * otherMass / (thisMass + otherMass) * velocityOnNormal.X;
                this.TopParent.Velocity.Y -= (1 + elasticity) * otherMass / (thisMass + otherMass) * velocityOnNormal.Y;
                this.TopParent.Velocity.Z -= (1 + elasticity) * otherMass / (thisMass + otherMass) * velocityOnNormal.Z;
                return true;
            }
            return false;
        }
public bool CollideAgainstBounce ( Sphere sphere , float thisMass , float otherMass , float elasticity ) { if DEBUG if ( thisMass == 0 && otherMass == 0 ) { throw new ArgumentException ( " " ) ; } endif if ( CollideAgainstMove ( sphere , thisMass , otherMass ) ) { Vector3 relativeVelocity = new Vector3 ( this . TopParent . XVelocity - sphere . TopParent . XVelocity , this . TopParent . YVelocity - sphere . TopParent . YVelocity , this . TopParent . ZVelocity - sphere . TopParent . ZVelocity ) ; if FRB_MDX float velocityNormalDotResult = Vector3 . Dot ( relativeVelocity , LastMoveCollisionReposition ) ; else float velocityNormalDotResult ; Vector3 . Dot ( ref relativeVelocity , ref LastMoveCollisionReposition , out velocityNormalDotResult ) ; endif if ( velocityNormalDotResult >= 0 ) { return true ; } if FRB_MDX Vector3 reverseNormal = - Vector3 . Normalize ( LastMoveCollisionReposition ) ; float length = Vector3 . Dot ( relativeVelocity , reverseNormal ) ; Vector3 velocityOnNormal = Vector3 . Multiply ( reverseNormal , length ) ; else Vector3 reverseNormal = - LastMoveCollisionReposition ; reverseNormal . Normalize ( ) ; float length = Vector3 . Dot ( relativeVelocity , reverseNormal ) ; Vector3 velocityOnNormal ; Vector3 . Multiply ( ref reverseNormal , length , out velocityOnNormal ) ; endif sphere . TopParent . Velocity . X += ( 1 + elasticity ) * thisMass / ( thisMass + otherMass ) * velocityOnNormal . X ; sphere . TopParent . Velocity . Y += ( 1 + elasticity ) * thisMass / ( thisMass + otherMass ) * velocityOnNormal . Y ; sphere . TopParent . Velocity . Z += ( 1 + elasticity ) * thisMass / ( thisMass + otherMass ) * velocityOnNormal . Z ; this . TopParent . Velocity . X -= ( 1 + elasticity ) * otherMass / ( thisMass + otherMass ) * velocityOnNormal . X ; this . TopParent . Velocity . Y -= ( 1 + elasticity ) * otherMass / ( thisMass + otherMass ) * velocityOnNormal . Y ; this . TopParent . Velocity . Z -= ( 1 + elasticity ) * otherMass / ( thisMass + otherMass ) * velocityOnNormal . Z ; return true ; } return false ; }
Performs a bounce collision (a collision which modifies velocity and acceleration), and separates the objects if so.
Performs a bounce collision ( a collision which modifies velocity and acceleration ) and separates the objects if so .
// Get the relative velocity of this circle to the argument circle: //Vector2 tangentVector = new Vector2((float)mLastCollisionTangent.X, (float)mLastCollisionTangent.Y); // Perform the bounce if the relative velocity and the tangent are the opposite direction.
Whether a collision occurred.
Whether a collision occurred .
sphere
Sphere
The Sphere to perform collision against.
The Sphere to perform collision against .
thisMass
float
The mass of this instance.
The mass of this instance .
otherMass
float
Th e mass of the argument cube.
Th e mass of the argument cube .
elasticity
float
The ratio of velocity to preserve.
The ratio of velocity to preserve .