Skip to content

Rotate a plane

Notice: all GL states are being handled implicitly.

java
int x = 10, y = 10, width = 20, height = 20;

Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);
bufferbuilder.pos(x, y, 0).endVertex();
bufferbuilder.pos(x, y + height, 0).endVertex();
bufferbuilder.pos(x + width, y + height, 0).endVertex();
bufferbuilder.pos(x + width, y, 0).endVertex();

GlStateManager.pushMatrix();
GlStateManager.rotate(45f, 1, 1, 0);
tessellator.draw();
GlStateManager.popMatrix();

Snipaste_2025-01-18_22-54-53

  • A transformed plane is being rendered

When it comes to quaternion, it's actually equivalent to do so

java
GlStateManager.rotate(new Quaternion(
                (float)Math.sin(Math.PI / 4d / 2d) / (float)Math.sqrt(2),
                (float)Math.sin(Math.PI / 4d / 2d) / (float)Math.sqrt(2),
                0f,
                (float)Math.cos(Math.PI / 4d / 4d)));

Intro to Quaternion

Quaternion q = [w, u] where w ∈ ℝ and u ∈ ℝ^3 which is a vector

equivalently, we write

q = w + a i + b j + c k where w, a, b, c ∈ ℝ, and i, j, k are imaginary numbers
where

i00
0j0
00k
*
ijk
ijk
ijk
=
-1k-j
-k-1i
j-i-1

and yeah u = (a, b, c)^T

When u is our rotation axis and θ is our rotation angle (in radian), we write that quaternion q = [cos θ/2, normalized u * sin θ/2]

How It Works

Let's say we have a point p = (x0, y0, z0)^T and define q = [cos θ/2, normalized u * sin θ/2]

Consider P = [0, p]

Then P' = [0,p'] = q P q^-1 = q P q* where p' is the rotated point

= [cos θ/2, normalized u sin θ/2] [0, p] [cos θ/2, - normalized u sin θ/2]

We can treat them as imaginary numbers and do the multiplication, or use Hamilton product.

Contributors

© 2024 CleanroomMC. All Rights Reserved.