Search code examples
c#math3drotation2d

Calculate rotation of rect angle by new position and distortion?


I have a game project which is based on augmented reality but I don't want to use any of these library's and plugins out there because they can't do what I want to do after this...etc..

So my question is it is possible to calculate the rotation of a 2D rect in a 3D world? If yes how? If no what should I do?

Illustration images for what I'm thinking of:

The 0 rotation on every axis (x,y,z axis in 2 dimension??)

The 0 rotation on every axis (x,y,z axis in 2 dimension??)

The 0 rotation in the 3D world

The 0 rotation in the 3D world

Some rotation in 2D world (which is in 3D --> x70, y10, z30)

rotation 70,10,30 in 2D

The rotated rect in 2D

rotation 70,10,30 in 3D

enter image description here

So how to calculate the 3D rotation from 2D? Like the AR Games do. How they do this?


Solution

  • As an important pre-requisite, you must know which edges on the unrotated rect correspond to which ones on the rotated one.

    1. Calculate the normals of each rect (A and B) respectively, and compute the quaternion required to rotate from A to B => Q. (This is probably available in Eigen or another game math library)

    2. Calculate the following:

      • The midpoint of the top edge of the unrotated rect => C
      • The corresponding point on the rotated rect => D
      • The center of the rotated rect => E
    3. Rotate vector C by Q to obtain a vector F. Rotation by quaternion is another common operation available in most 3D math libraries.

    4. Calculate the quaternion to rotate from F to D - E => P

    5. Calculate the final quaternion R = P * Q, and convert to axis-angles if you like: Wikipedia