Beispiel #1
0
        /// <summary>
        /// Berechnet die Position eines Punkts, der um einen angegeben Punkt entlang einer Achse rotiert wird
        /// </summary>
        /// <param name="point">Mittelpunkt der Rotation</param>
        /// <param name="distance">Distanz zum Mittelpunkt</param>
        /// <param name="degrees">Grad der Rotation</param>
        /// <param name="plane">Achse der Rotation (Standard: Y)</param>
        /// <returns>Position des rotierten Punkts</returns>
        public static Vector3 CalculateRotationAroundPointOnAxis(Vector3 point, float distance, float degrees, Plane plane = Plane.Y)
        {
            float radians = MathHelper.DegreesToRadians(degrees % 360);

            Matrix4.CreateTranslation(ref point, out translationPointMatrix);

            if (plane == Plane.X)
            {
                Matrix4.CreateRotationX(radians, out rotationMatrix);
                Matrix4.CreateTranslation(0, 0, distance, out translationMatrix);
            }
            else if (plane == Plane.Y)
            {
                Matrix4.CreateRotationY(radians, out rotationMatrix);
                Matrix4.CreateTranslation(0, 0, distance, out translationMatrix);
            }
            else if (plane == Plane.Z)
            {
                Matrix4.CreateRotationZ(radians, out rotationMatrix);
                Matrix4.CreateTranslation(0, distance, 0, out translationMatrix);
            }
            else if (plane == Plane.Camera)
            {
                if (KWEngine.CurrentWorld != null)
                {
                    Vector3 camLookAt;
                    if (KWEngine.CurrentWorld.IsFirstPersonMode)
                    {
                        camLookAt = KWEngine.CurrentWorld.GetFirstPersonObject().GetLookAtVector();
                    }
                    else
                    {
                        camLookAt = KWEngine.CurrentWorld.GetCameraLookAtVector();
                    }

                    rotationMatrix = HelperMatrix.CreateRotationMatrixForAxisAngle(ref camLookAt, ref radians);
                    Vector3 cross = Vector3.Cross(camLookAt, KWEngine.WorldUp);
                    Matrix4.CreateTranslation(-cross.X, -cross.Y, -cross.Z, out translationMatrix);
                }
                else
                {
                    HelperGeneral.ShowErrorAndQuit("HelperRotation::CalculateRotationAroundPointOnAxis()", "CurrentWorld is not set. Cannot rotate around a camera axis that does not exist.");
                    return(Vector3.Zero);
                }
            }

            Matrix4.Mult(ref translationMatrix, ref rotationMatrix, out tempMatrix);
            Matrix4.Mult(ref tempMatrix, ref translationPointMatrix, out spinMatrix);
            Vector3.TransformPosition(ref zeroVector, ref spinMatrix, out finalTranslationPoint);

            return(finalTranslationPoint);
        }
Beispiel #2
0
        /// <summary>
        /// Berechnet die Position eines Punkts, der um einen angegeben Punkt entlang einer Achse rotiert wird
        /// </summary>
        /// <param name="point">Mittelpunkt der Rotation</param>
        /// <param name="distance">Distanz zum Mittelpunkt</param>
        /// <param name="degrees">Grad der Rotation</param>
        /// <param name="axis">Achse der Rotation (normalisiert!)</param>
        /// <returns>Position des rotierten Punkts</returns>
        public static Vector3 CalculateRotationAroundPointOnAxis(Vector3 point, float distance, float degrees, Vector3 axis)
        {
            float radians = MathHelper.DegreesToRadians(degrees % 360);

            Matrix4.CreateTranslation(ref point, out translationPointMatrix);

            rotationMatrix = HelperMatrix.CreateRotationMatrixForAxisAngle(ref axis, ref radians);
            Vector3 cross = Vector3.Cross(axis, KWEngine.WorldUp);

            Matrix4.CreateTranslation(-cross.X, -cross.Y, -cross.Z, out translationMatrix);

            Matrix4.Mult(ref translationMatrix, ref rotationMatrix, out tempMatrix);
            Matrix4.Mult(ref tempMatrix, ref translationPointMatrix, out spinMatrix);
            Vector3.TransformPosition(ref zeroVector, ref spinMatrix, out finalTranslationPoint);

            return(finalTranslationPoint);
        }