Ejemplo n.º 1
0
        public void Update(int X, int Y)
        {
            phi   = -rotateScale * X + (float)Math.PI;
            theta = rotateScale * Y + 0.5f * (float)Math.PI;
            if (theta <= 0.00001f)
            {
                theta = 0.00001f;
                Y     = -(int)((0.5f * Math.PI) / rotateScale);
            }
            else if (theta >= Math.PI)
            {
                theta = (float)Math.PI;
                Y     = (int)((0.5f * (float)Math.PI) / rotateScale);
            }
            forward = new Vec3((float)(Math.Sin(theta) * Math.Sin(phi)), (float)(Math.Cos(theta)), (float)(Math.Sin(theta) * Math.Cos(phi)));
            forward.normalize();
            right = new Vec3(Vec3.cross(new Vec3(forward.x, 0, forward.z), forward));
            right.normalize();

            Quaternion rotationPhi     = new Quaternion(new Vec3(0.0f, 1.0f, 0.0f), phi);
            float      axisTheta       = phi + 0.5f * (float)Math.PI;
            Vec3       rotateThetaAxis = new Vec3((float)Math.Sin(axisTheta), 0.0f, (float)Math.Cos(axisTheta));
            Quaternion rotationTheta   = new Quaternion(rotateThetaAxis, theta);

            rotation = rotationTheta * rotationPhi;
        }
Ejemplo n.º 2
0
        public static Transform rotate(Vec3 axis, float degree)
        {
            axis.normalize();
            Mat4  m1    = new Mat4();
            float cosa  = (float)Math.Cos(degree);
            float sina  = (float)-Math.Sin(degree);
            float ncosa = 1 - cosa;
            float xy    = axis.x * axis.y;
            float xz    = axis.x * axis.z;
            float yz    = axis.y * axis.z;

            m1[0, 0] = cosa + (axis.x * axis.x) * ncosa;
            m1[0, 1] = xy * ncosa - axis.z * sina;
            m1[0, 2] = xz * ncosa + axis.y * sina;
            m1[0, 3] = 0;
            m1[1, 0] = xy * ncosa + axis.z * sina;
            m1[1, 1] = cosa + axis.y * axis.y * ncosa;
            m1[1, 2] = yz * ncosa - axis.x * sina;
            m1[1, 3] = 0;
            m1[2, 0] = xz * ncosa - axis.y * sina;
            m1[2, 1] = yz * ncosa + axis.x * sina;
            m1[2, 2] = axis.z * axis.z * ncosa + cosa;
            m1[2, 3] = 0;
            m1[3, 0] = 0;
            m1[3, 1] = 0;
            m1[3, 2] = 0;
            m1[3, 3] = 1;

            return(new Transform(m1));
        }
Ejemplo n.º 3
0
 public Quaternion(Vec3 axis, float theta)
 {
     axis.normalize();
     imaginaryPart = (float)Math.Sin(0.5f * theta) * axis;
     realPart      = (float)Math.Cos(0.5f * theta);
 }