Esempio n. 1
0
        /// <summary>
        /// Returns whether this instance overlaps the argument AxisAlignedCube
        /// </summary>
        /// <param name="cube">The instance AxisAlignedCube to test against.</param>
        /// <returns>Whether collision has occurred</returns>
        public bool CollideAgainst(AxisAlignedCube cube)
        {
            UpdateDependencies(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();

            return(distance <= Radius * Radius);
        }
Esempio n. 2
0
        /// <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>
        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);
        }