/// <summary>
        /// Gets the rotation matrix for this object.
        /// </summary>
        public Matrix4x4 GetRotationMatrix()
        {
            switch (m_transformationType)
            {
            case SpacialTransformationType.ScalingTranslationEulerAngles:
            case SpacialTransformationType.TranslationEulerAngles:
                return(Matrix4x4.CreateFromYawPitchRoll(m_rotation.Y, m_rotation.X, m_rotation.Z));

            case SpacialTransformationType.ScalingTranslationQuaternion:
            case SpacialTransformationType.TranslationQuaternion:
                return(Matrix4x4.CreateFromQuaternion(m_rotationQuaternion));

            case SpacialTransformationType.ScalingTranslationDirection:
            case SpacialTransformationType.TranslationDirection:
                return(Matrix4x4Ex.CreateRotationDirection(m_rotationUp, m_rotationForward));

            case SpacialTransformationType.Translation:
            case SpacialTransformationType.None:
            case SpacialTransformationType.CustomTransform:
                return(Matrix4x4.Identity);

            case SpacialTransformationType.TakeFromOtherObject:
                if (m_transformSourceObject != null)
                {
                    return(m_transformSourceObject.GetRotationMatrix());
                }
                else
                {
                    return(Matrix4x4.Identity);
                }

            default:
                throw new SeeingSharpGraphicsException("Unknown transformation type: " + m_transformationType);
            }
        }
 /// <summary>
 /// Calculates the view and projection matrix for this camera.
 /// </summary>
 /// <param name="position">The position of the camera.</param>
 /// <param name="target">The target point of the camera.</param>
 /// <param name="upVector">The current up vector.</param>
 /// <param name="zNear">Distance to the nearest rendered pixel.</param>
 /// <param name="zFar">Distance to the farest rendered pixel.</param>
 /// <param name="screenWidth">The current width of the screen in pixel.</param>
 /// <param name="screenHeight">The current height of the screen in pixel.</param>
 /// <param name="viewMatrix">The calculated view matrix.</param>
 /// <param name="projMatrix">The calculated projection matrix.</param>
 protected override void CalculateViewProjectionMatrices(
     Vector3 position, Vector3 target, Vector3 upVector, float zNear, float zFar, int screenWidth, int screenHeight,
     out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix)
 {
     m_aspectRatio = (float)screenWidth / (float)screenHeight;
     viewMatrix    = Matrix4x4Ex.CreateLookAtLH(
         position,
         target,
         upVector);
     projMatrix = Matrix4x4Ex.CreatePerspectiveFovLH(
         m_fov,
         m_aspectRatio,
         zNear, zFar);
 }
Beispiel #3
0
        internal void PreStep(float dt)
        {
            Matrix4x4Ex.CreateFromAxisAngle(ref suspension.localDirection, shape.steeringAngle, out shape.steeringTransform);
            Matrix4x4Ex.TransformNormal(ref localForwardDirection, ref shape.steeringTransform, out worldForwardDirection);
            Matrix3x3.Transform(ref worldForwardDirection, ref Vehicle.Body.orientationMatrix, out worldForwardDirection);
            if (HasSupport)
            {
                Vector3Ex.Subtract(ref supportLocation, ref Vehicle.Body.position, out ra);
                if (supportingEntity != null)
                {
                    Vector3Ex.Subtract(ref supportLocation, ref SupportingEntity.position, out rb);
                }


                //Mind the order of updating!  sliding friction must come before driving force or rolling friction
                //because it computes the sliding direction.

                suspension.isActive = true;
                suspension.numIterationsAtZeroImpulse       = 0;
                suspension.solverSettings.currentIterations = 0;
                slidingFriction.isActive = true;
                slidingFriction.numIterationsAtZeroImpulse       = 0;
                slidingFriction.solverSettings.currentIterations = 0;
                drivingMotor.isActive = true;
                drivingMotor.numIterationsAtZeroImpulse       = 0;
                drivingMotor.solverSettings.currentIterations = 0;
                brake.isActive = true;
                brake.numIterationsAtZeroImpulse       = 0;
                brake.solverSettings.currentIterations = 0;

                suspension.PreStep(dt);
                slidingFriction.PreStep(dt);
                drivingMotor.PreStep(dt);
                brake.PreStep(dt);
            }
            else
            {
                //No support, don't need any solving.
                suspension.isActive      = false;
                slidingFriction.isActive = false;
                drivingMotor.isActive    = false;
                brake.isActive           = false;

                suspension.accumulatedImpulse      = 0;
                slidingFriction.accumulatedImpulse = 0;
                drivingMotor.accumulatedImpulse    = 0;
                brake.accumulatedImpulse           = 0;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Calculates the view and projection matrix for this camera.
        /// </summary>
        /// <param name="position">The position of the camera.</param>
        /// <param name="target">The target point of the camera.</param>
        /// <param name="upVector">The current up vector.</param>
        /// <param name="zNear">Distance to the nearest rendered pixel.</param>
        /// <param name="zFar">Distance to the farest rendered pixel.</param>
        /// <param name="screenWidth">The current width of the screen in pixel.</param>
        /// <param name="screenHeight">The current height of the screen in pixel.</param>
        /// <param name="viewMatrix">The calculated view matrix.</param>
        /// <param name="projMatrix">The calculated projection matrix.</param>
        protected override void CalculateViewProjectionMatrices(
            Vector3 position, Vector3 target, Vector3 upVector, float zNear, float zFar, int screenWidth, int screenHeight,
            out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix)
        {
            if (m_zoomFactor <= ZOOM_FACTOR_MIN)
            {
                m_zoomFactor = ZOOM_FACTOR_MIN;
            }

            projMatrix = Matrix4x4Ex.CreateOrthoLH(
                (float)ScreenWidth / m_zoomFactor, (float)screenHeight / m_zoomFactor,
                -Math.Abs(Math.Max(zNear, zFar)),
                Math.Abs(Math.Max(zNear, zFar)));
            viewMatrix = Matrix4x4Ex.CreateLookAtLH(
                position,
                target,
                upVector);
        }
Beispiel #5
0
        /// <summary>
        /// Updates the wheel's world transform for graphics.
        /// Called automatically by the owning wheel at the end of each frame.
        /// If the engine is updating asynchronously, you can call this inside of a space read buffer lock
        /// and update the wheel transforms safely.
        /// </summary>
        public override void UpdateWorldTransform()
        {
#if !WINDOWS
            System.Numerics.Vector3 newPosition = new System.Numerics.Vector3();
#else
            System.Numerics.Vector3 newPosition;
#endif
            System.Numerics.Vector3 worldAttachmentPoint;
            System.Numerics.Vector3 localAttach;
            Vector3Ex.Add(ref wheel.suspension.localAttachmentPoint, ref wheel.vehicle.Body.CollisionInformation.localPosition, out localAttach);
            worldTransform = Matrix3x3.ToMatrix4X4(wheel.vehicle.Body.BufferedStates.InterpolatedStates.OrientationMatrix);

            Matrix4x4Ex.TransformNormal(ref localAttach, ref worldTransform, out worldAttachmentPoint);
            worldAttachmentPoint += wheel.vehicle.Body.BufferedStates.InterpolatedStates.Position;

            System.Numerics.Vector3 worldDirection;
            Matrix4x4Ex.Transform(ref wheel.suspension.localDirection, ref worldTransform, out worldDirection);

            float length = wheel.suspension.currentLength - graphicalRadius;
            newPosition.X = worldAttachmentPoint.X + worldDirection.X * length;
            newPosition.Y = worldAttachmentPoint.Y + worldDirection.Y * length;
            newPosition.Z = worldAttachmentPoint.Z + worldDirection.Z * length;

            System.Numerics.Matrix4x4 spinTransform;

            System.Numerics.Vector3 localSpinAxis;
            Vector3Ex.Cross(ref wheel.localForwardDirection, ref wheel.suspension.localDirection, out localSpinAxis);
            Matrix4x4Ex.CreateFromAxisAngle(ref localSpinAxis, spinAngle, out spinTransform);


            System.Numerics.Matrix4x4 localTurnTransform;
            Matrix4x4Ex.Multiply(ref localGraphicTransform, ref spinTransform, out localTurnTransform);
            Matrix4x4Ex.Multiply(ref localTurnTransform, ref steeringTransform, out localTurnTransform);
            //System.Numerics.Matrix4x4.Multiply(ref localTurnTransform, ref spinTransform, out localTurnTransform);
            Matrix4x4Ex.Multiply(ref localTurnTransform, ref worldTransform, out worldTransform);
            worldTransform.Translation += newPosition;
        }
        /// <summary>
        /// Updates the object.
        /// </summary>
        /// <param name="updateState">Current update state.</param>
        protected override void UpdateInternal(SceneRelatedUpdateState updateState)
        {
            //Calculates local transform matrix (transforms local space to world space)
            bool doRecreateShaderParameters = false;

            base.TransormationChanged =
                m_transformParamsChanged || updateState.ForceTransformUpdatesOnChilds;

            // Update local transform matrix if transform values have changed
            if (m_transformParamsChanged || updateState.ForceTransformUpdatesOnChilds)
            {
                m_transformParamsChanged       = false;
                m_forceTransformUpdateOnChilds = this.HasChilds;
                doRecreateShaderParameters     = true;

                // Calculate new transformation matrix
                switch (m_transformationType)
                {
                case SpacialTransformationType.ScalingTranslationEulerAngles:
                    m_transform =
                        Matrix4x4.CreateScale(m_scaling) *
                        Matrix4x4.CreateFromYawPitchRoll(m_rotation.Y, m_rotation.X, m_rotation.Z) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.ScalingTranslationQuaternion:
                    m_transform =
                        Matrix4x4.CreateScale(m_scaling) *
                        Matrix4x4.CreateFromQuaternion(m_rotationQuaternion) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.ScalingTranslationDirection:
                    m_transform =
                        Matrix4x4.CreateScale(m_scaling) *
                        Matrix4x4Ex.CreateRotationDirection(m_rotationUp, m_rotationForward) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.ScalingTranslation:
                    m_transform =
                        Matrix4x4.CreateScale(m_scaling) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.TranslationEulerAngles:
                    m_transform =
                        Matrix4x4.CreateFromYawPitchRoll(m_rotation.Y, m_rotation.X, m_rotation.Z) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.TranslationQuaternion:
                    m_transform =
                        Matrix4x4.CreateFromQuaternion(m_rotationQuaternion) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.TranslationDirection:
                    m_transform =
                        Matrix4x4Ex.CreateRotationDirection(m_rotationUp, m_rotationForward) *
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.Translation:
                    m_transform =
                        Matrix4x4.CreateTranslation(m_position) *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.CustomTransform:
                    m_transform =
                        m_customTransform *
                        updateState.World.Top;
                    break;

                case SpacialTransformationType.TakeFromOtherObject:
                    if (m_transformSourceObject != null)
                    {
                        m_transform = m_transformSourceObject.m_transform;
                    }
                    else
                    {
                        m_transform = updateState.World.Top;
                    }
                    break;

                case SpacialTransformationType.None:
                    m_transform = updateState.World.Top;
                    break;
                }
            }

            // Trigger recreation of shader parameters
            if (doRecreateShaderParameters)
            {
                TriggerRecreateOfParameters();
            }
        }