1
votes

I'm trying to implement transform.Rotate on the y axis in Unity using rotation matrix, is it possible? I wrote this function but it doesn't seems to work, pos is the position of the object to rotate and theta is the angle

public static MyQuaternion Rotate(Vector3 pos, float theta)
 {
         pos = pos.normalized;
         pos -= pos;
         float[,] m = new float[3, 3];

         m[0, 0] = Mathf.Cos(theta); 
         m[0, 1] = 0; 
         m[0, 2] = Mathf.Sin(theta);
         m[1, 0] = 0; 
         m[1, 1] = 1; 
         m[1, 2] = 0;
         m[2, 0] = -Mathf.Sin(theta); 
         m[2, 1] = 0; 
         m[2, 2] = Mathf.Cos(theta);

         Vector3 newPos = new Vector3();
         float temp;

         for (int i = 0; i < 3; ++i)
         {
             temp = 0;
             for (int j = 0; j < 3; ++j)
             {
                 temp += m[i, j] * pos[j];
             }
             newPos[i] = temp;
         }

         newPos += pos;

         return new MyQuaternion(newPos.x, newPos.y, newPos.z, 0);
 }

Do you have any idea? Thanks

1
but it doesn't seems to work - in what way? Is the end result incorrect? Where did you get this equation from? And are you trying to rotate the point pos itself, or create a matrix which rotates objects around pos? - meowgoesthedog
you seem to be rotating the vector sort of all right, but then at the end you take the rotated vector and shove it into a quaternion - this is clrearly wrong. Your method returns a Vector3, not a rotation. if you need a rotation there's already a few methods that generate a quaternion from direction built into Quaternion class - zambari
@meowgoesthedog I want to rotate the object itself of 90 degrees so i take its coordinates and use it to translate to the origin and apply the basic rotation matrix for the y axis, then i convert the position of the object to a Quaternion (not sure about the conversion) that will be applied to the object. The problem is that the object doesn't rotate, it just remains stationary. - Keyr
For this you need to return a 4x4 (not 3x3) transformation matrix, i.e. an affine transformation in 3-D. Simply returning a quaternion isn't enough, not to mention that you are constructing it in the wrong way as mentioned by @zambari. - meowgoesthedog
Why are you doing this: pos -= pos; ? - Ruzihm

1 Answers

0
votes

If you want to get deeply into rotating a vector by hand, you are sort of doing it all right, however I am not sure about the type MyQuaternion you are returning.

If you just want to rotate a vector by an arbitrary angle you can just use Unity's build in Quaternion class. The mutliplication by Vector3 is defined as rotation of that vector so you can just do

Quaternion q=Quaternion.Euler(theta,0,0); // or however you want to map it
Vector3 rotatedVector=q*inputVector;
return rotatedVector;

Rotation in unity is generally done via Quaternion multiplication. To apply it to a gameobject, simply multiply its transform by a rotation you want to perform.

transform.rotation*=Quaternion.Euler(0,0,theta);

Position of an object does not have any play in rotation.