Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Sphere.cs
/// <summary>
/// Returns whether this instance overlaps the argument cube, and separates the two instances according to their relative masses.
/// </summary>
/// <param name="cube">The cube to perform collision against.</param>
/// <param name="thisMass">The mass of this instance.</param>
/// <param name="otherMass">The mass of the cube.</param>
/// <returns>Whether the objects were overlapping before the reposition.</returns>
Returns whether this instance overlaps the argument cube, and separates the two instances according to their relative masses.
Returns whether this instance overlaps the argument cube and separates the two instances according to their relative masses .
public bool CollideAgainstMove(AxisAlignedCube cube, float thisMass, float otherMass)
        {
#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 (LastDependencyUpdate != TimeManager.CurrentTime)
            {
                UpdateDependencies(TimeManager.CurrentTime);
            }
            if (cube.LastDependencyUpdate != TimeManager.CurrentTime)
            {
                cube.UpdateDependencies(TimeManager.CurrentTime);
            }
            Vector3 relativeCenter = Position - cube.Position;
            if (System.Math.Abs(relativeCenter.X) - Radius > cube.ScaleX ||
                System.Math.Abs(relativeCenter.Y) - Radius > cube.ScaleY ||
                System.Math.Abs(relativeCenter.Z) - Radius > cube.ScaleZ)
            {
                return false;
            }
            Vector3 nearest = GetNearestPoint(cube, relativeCenter);
            float distance = (nearest - relativeCenter).LengthSquared();
            if (distance > Radius * Radius) return false;
            double distanceToMove = Radius - System.Math.Sqrt((double)distance);
            Vector3 moveVector = relativeCenter - nearest;
            moveVector.Normalize();
            float amountToMoveThis = otherMass / (thisMass + otherMass);
            LastMoveCollisionReposition = moveVector * (float)(distanceToMove * amountToMoveThis);
            Vector3 cubeReposition = moveVector * (float)(-distanceToMove * (1 - amountToMoveThis));
            cube.mLastMoveCollisionReposition = cubeReposition;
            if (mParent != null)
            {
                TopParent.Position += LastMoveCollisionReposition;
                ForceUpdateDependencies();
            }
            else
            {
                Position += LastMoveCollisionReposition;
            }
            if (cube.mParent != null)
            {
                cube.TopParent.Position += cube.LastMoveCollisionReposition;
                ForceUpdateDependencies();
            }
            else
            {
                cube.Position += cube.LastMoveCollisionReposition;
            }
            return true;
        }
public bool CollideAgainstMove ( AxisAlignedCube cube , float thisMass , float otherMass ) { if DEBUG if ( thisMass == 0 && otherMass == 0 ) { throw new ArgumentException ( " " ) ; } endif if ( LastDependencyUpdate != TimeManager . CurrentTime ) { UpdateDependencies ( TimeManager . CurrentTime ) ; } if ( cube . LastDependencyUpdate != TimeManager . CurrentTime ) { cube . UpdateDependencies ( TimeManager . CurrentTime ) ; } Vector3 relativeCenter = Position - cube . Position ; if ( System . Math . Abs ( relativeCenter . X ) - Radius > cube . ScaleX || System . Math . Abs ( relativeCenter . Y ) - Radius > cube . ScaleY || System . Math . Abs ( relativeCenter . Z ) - Radius > cube . ScaleZ ) { return false ; } Vector3 nearest = GetNearestPoint ( cube , relativeCenter ) ; float distance = ( nearest - relativeCenter ) . LengthSquared ( ) ; if ( distance > Radius * Radius ) return false ; double distanceToMove = Radius - System . Math . Sqrt ( ( double ) distance ) ; Vector3 moveVector = relativeCenter - nearest ; moveVector . Normalize ( ) ; float amountToMoveThis = otherMass / ( thisMass + otherMass ) ; LastMoveCollisionReposition = moveVector * ( float ) ( distanceToMove * amountToMoveThis ) ; Vector3 cubeReposition = moveVector * ( float ) ( - distanceToMove * ( 1 - amountToMoveThis ) ) ; cube . mLastMoveCollisionReposition = cubeReposition ; if ( mParent != null ) { TopParent . Position += LastMoveCollisionReposition ; ForceUpdateDependencies ( ) ; } else { Position += LastMoveCollisionReposition ; } if ( cube . mParent != null ) { cube . TopParent . Position += cube . LastMoveCollisionReposition ; ForceUpdateDependencies ( ) ; } else { cube . Position += cube . LastMoveCollisionReposition ; } return true ; }
Returns whether this instance overlaps the argument cube, and separates the two instances according to their relative masses.
Returns whether this instance overlaps the argument cube and separates the two instances according to their relative masses .
Whether the objects were overlapping before the reposition.
Whether the objects were overlapping before the reposition .
cube
AxisAlignedCube
The cube to perform collision against.
The cube to perform collision against .
thisMass
float
The mass of this instance.
The mass of this instance .
otherMass
float
The mass of the cube.
The mass of the cube .