Search code examples
c#opentk

Rotate a object around a point


how would I rotate an object around an object, such as rotating an object in circles around another object? Preferably using GL.Rotate and as little math as possible!


Solution

  • GL.Rotate defines a rotation matrix that rotates around (0, 0). If you want to rotate around a pivot (pivotX, pivotY) you have to:

    1. Translate the object so that the pivot point is moved to (0, 0).
    2. Rotate the object.
    3. Move the object so that the pivot point moves in its original position.

    e.g.:

    GL.Translate(pivotX, pivotY, 0);    // 3. move back
    GL.Rotate(angle, 0, 0, 1);         // 2. rotate
    GL.Translate(-pivotX, -pivotY, 0);  // 1. move pivot to (0, 0) 
    

    See also How to use Pivot Point in Transformations