Example #1
0
        public virtual Matrix3D Rotate(Axis axis, double deltaAngle, bool isFromSaved)
        {
            Quaternion3D rotationQ = Quaternion3D.Identity;

            switch (axis)
            {
            case Axis.X:
                rotationQ *= Quaternion3D.CreateFromAxisAngle(_unitX, deltaAngle);
                break;

            case Axis.Y:
                rotationQ *= Quaternion3D.CreateFromAxisAngle(_unitY, deltaAngle);
                break;

            case Axis.Z:
                rotationQ *= Quaternion3D.CreateFromAxisAngle(_unitZ, deltaAngle);
                break;
            }
            Matrix3D matrix = Ext3D.CreateFromQuaternion(rotationQ);

            if (isFromSaved)
            {
                _transform = matrix * _savedTransform;
            }
            else
            {
                _transform = matrix * _transform;
            }

            return(matrix);
        }
Example #2
0
        public override void DoTransform(double deltaAngle)
        {
            //  Debug.WriteLine("start DoTransform");
            foreach (CubicleFace face in AffectedFaces)
            {
                Cubie     cubie     = face.CubieFace.Cubie;
                CubieFace cubieFace = face.CubieFace;
                //cubicle.Cubie.Rotate(BasicOp.Axis, deltaAngle, false);
                Vector3D axis = face.CubieFace.Cubie.UnitX;

                if (Axis == Axis.X)
                {
                    axis = face.CubieFace.Cubie.UnitX;
                }
                else if (Axis == Axis.Y)
                {
                    axis = face.CubieFace.Cubie.UnitY;
                }
                else if (Axis == Axis.Z)
                {
                    axis = face.CubieFace.Cubie.UnitZ;
                }

                Quaternion3D rot       = Quaternion3D.CreateFromAxisAngle(axis, deltaAngle);
                Matrix3D     rotMatrix = Ext3D.CreateFromQuaternion(rot);
                Matrix3D     matrix    = cubie.Transform;// Matrix.Invert(cubie.Transform);
                matrix.Invert();


                Matrix3D rotation = Ext3D.CreateTranslation(Ext3D.Transform(-AxisTranslationFromOrigin, matrix)) * rotMatrix * Ext3D.CreateTranslation(Ext3D.Transform(AxisTranslationFromOrigin, matrix));
                if (IsAxisMoving)
                {
                    Vector3D v1;
                    if (!_axisTranslation.ContainsKey(cubieFace))
                    {
                        Matrix3D m = (cubieFace as ITransform).Transform; m.Invert();
                        v1 = Ext3D.Transform(Axis2TranslationFromOrigin, m);
                        _axisTranslation.Add(cubieFace, v1);
                    }
                    v1 = _axisTranslation[cubieFace];
                    Matrix3D r = rotation; r.Invert();
                    v1 = Ext3D.Transform(v1, r);// Matrix.Invert(rotation));

                    Matrix3D rotationAxis = Ext3D.CreateTranslation(-v1) * rotMatrix * Ext3D.CreateTranslation(v1);
                    rotation = rotationAxis * rotation;

                    _axisTranslation[cubieFace] = v1;
                }
                (cubieFace as ITransform).DoTransform(rotation, false);
            }
        }
Example #3
0
        public static RotateCameraResult RotateCamera(this PerspectiveCamera camera, Vector3D axis, double angle)
        {
            axis.Normalize();
            Quaternion3D q = Quaternion3D.CreateFromAxisAngle(axis, angle);
            Matrix3D     m = Ext3D.CreateFromQuaternion(q);

            Vector3D p   = new Vector3D(camera.Position.X, camera.Position.Y, camera.Position.Z);
            Vector3D pos = Ext3D.Transform(p, m);

            camera.Position = new Point3D(pos.X, pos.Y, pos.Z);

            Vector3D d = new Vector3D(camera.LookDirection.X, camera.LookDirection.Y, camera.LookDirection.Z);

            camera.LookDirection = Ext3D.Transform(d, m);
            Vector3D up = new Vector3D(camera.UpDirection.X, camera.UpDirection.Y, camera.UpDirection.Z);

            camera.UpDirection = Ext3D.Transform(up, m);
            return(new RotateCameraResult
            {
                RotateMatrix = m,
                ViewMatrix = CalculateViewMatrix(camera.Position, camera.LookDirection, camera.UpDirection)
            });
        }