Beispiel #1
0
        public Vector3 ComputeCollision(Vector3 absTranslation)  // Takes the wanted translation and returns the possible one
        {
            bool wasHCollision = this.WasHorizontalCollision;

            this.WasHorizontalCollision = false;
            absTranslation.x            = MathHelper.clamp(absTranslation.x, -Cst.CUBE_SIDE, Cst.CUBE_SIDE);
            absTranslation.y            = MathHelper.clamp(absTranslation.y, -Cst.CUBE_SIDE, Cst.CUBE_SIDE);
            absTranslation.z            = MathHelper.clamp(absTranslation.z, -Cst.CUBE_SIDE, Cst.CUBE_SIDE);

            Vector3[] hitPoints      = this.GetHitPoints();
            Vector3   actTranslation = absTranslation;

            Vector3[] axes = new Vector3[] { Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z };

            foreach (Vector3 axis in axes)
            {
                Vector3 delta = absTranslation * axis;
                if (delta.IsZeroLength)
                {
                    continue;
                }                                                            // Ignore the axis if there is no translation on it

                Vector3[] actHitPoints    = this.AddToAll(hitPoints, delta); // The hitPoints after the wanted translation
                Vector3[] hitPointsToTest = this.GetPointsToTest(actHitPoints, absTranslation, axis);
                for (int i = 0; i < hitPointsToTest.Length; i++)
                {
                    Vector3 relHitPoint = MainWorld.AbsToRelative(hitPointsToTest[i]);
                    if (this.mWorld.HasPointCollision(hitPointsToTest[i]))
                    {
                        /* Compute the possible translation */
                        float val = 0;
                        if (axis == Vector3.UNIT_Y)
                        {
                            float blockHeight = Cst.CUBE_SIDE;
                            if (mWorld.getIsland().getBlock(relHitPoint, false) is SnowEighthBlock)
                            {
                                blockHeight /= 8;
                            }
                            if (this.GetNonNullValue(absTranslation * axis) < 0)
                            {
                                val = (relHitPoint.y * Cst.CUBE_SIDE + blockHeight) - this.mCharac.FeetPosition.y;
                            }
                        }
                        else
                        {
                            this.WasHorizontalCollision = true;
                        }

                        actTranslation = (actTranslation * (Vector3.UNIT_SCALE - axis)) + val * axis;    // Update the actTranslation
                        break;
                    }
                }
            }

            this.HasHorizontalCollisionEnded = wasHCollision && !this.WasHorizontalCollision;
            return(actTranslation);
        }