Rotate vertex quad around a custom pivot

This is my code for rotating a vertex quad sized actualSize and centered at x,y:

        vertexData.position(bufPosition * 12);

        float x1=(-actualSize / 2 + x);
        float y1=(-actualSize / 2 + y);

        float x2=(actualSize / 2 + x);
        float y2=(-actualSize / 2 + y);

        float x3=(-actualSize / 2 + x);
        float y3=(actualSize / 2 + y);

        float x4=(actualSize / 2 + x);
        float y4=(actualSize / 2 + y);

        float cos=FastMath.cos(angle);
        float sin=FastMath.sin(angle);

        vertices[0] = x1*cos-y1*sin;
        vertices[1] = x1*sin+y1*cos;
        vertices[2] = 0;
        vertices[3] = x2*cos-y2*sin;
        vertices[4] = x2*sin+y2*cos;
        vertices[5] = 0;
        vertices[6] = x3*cos-y3*sin;
        vertices[7] = x3*sin+y3*cos;
        vertices[8] = 0;
        vertices[9] = x4*cos-y4*sin;
        vertices[10] = x4*sin+y4*cos;
        vertices[11] = 0;
        vertexData.put(vertices, 0, 12);

However, I’d like to rotate around another pivot point (not centered). Any tips? Thanks!

Pseudo code:

Vector3f pivot = whatever

Quaternion rotation = ...your rotation
for each vertex {
    Vector3f v = vertex.subtract(pivot);
    v = rotation.mult(v);
    vertex = v.add(pivot);
}

Even for 2D… just use the quaternion and avoid hand-rolling a matrix mult as you’ve done.

2 Likes

Or if Quaternion seems like overkill for 2D, you could do something similar using com.jme3.math.Vector2f#rotateAroundOrigin().

The trick would still be to translate to the origin, rotate around the origin, and then translate back.

Another approach would be to parent each quad geometry to a node. Put all the translation in the node do the rotation locally on the quad.

1 Like