Skip to content

Commit 809a0eb

Browse files
committed
Get JOML'd
- Add more rotate methods to Affine and Rotate - Add optimized implementations in TransformedInstance
1 parent 10d3923 commit 809a0eb

File tree

3 files changed

+161
-45
lines changed

3 files changed

+161
-45
lines changed

common/src/lib/java/dev/engine_room/flywheel/lib/instance/TransformedInstance.java

+78-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.engine_room.flywheel.lib.instance;
22

3+
import org.joml.AxisAngle4f;
34
import org.joml.Matrix4f;
45
import org.joml.Matrix4fc;
56
import org.joml.Quaternionfc;
@@ -9,6 +10,7 @@
910
import dev.engine_room.flywheel.api.instance.InstanceHandle;
1011
import dev.engine_room.flywheel.api.instance.InstanceType;
1112
import dev.engine_room.flywheel.lib.transform.Affine;
13+
import net.minecraft.core.Direction;
1214

1315
public class TransformedInstance extends ColoredLitInstance implements Affine<TransformedInstance> {
1416
public final Matrix4f pose = new Matrix4f();
@@ -17,12 +19,6 @@ public TransformedInstance(InstanceType<? extends TransformedInstance> type, Ins
1719
super(type, handle);
1820
}
1921

20-
@Override
21-
public TransformedInstance rotateAround(Quaternionfc quaternion, float x, float y, float z) {
22-
pose.rotateAround(quaternion, x, y, z);
23-
return this;
24-
}
25-
2622
@Override
2723
public TransformedInstance translate(float x, float y, float z) {
2824
pose.translate(x, y, z);
@@ -84,4 +80,80 @@ public TransformedInstance setZeroTransform() {
8480
pose.zero();
8581
return this;
8682
}
83+
84+
@Override
85+
public TransformedInstance rotateAround(Quaternionfc quaternion, float x, float y, float z) {
86+
pose.rotateAround(quaternion, x, y, z);
87+
return this;
88+
}
89+
90+
@Override
91+
public TransformedInstance rotateCentered(float radians, float axisX, float axisY, float axisZ) {
92+
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
93+
.rotate(radians, axisX, axisY, axisZ)
94+
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
95+
return this;
96+
}
97+
98+
@Override
99+
public TransformedInstance rotateXCentered(float radians) {
100+
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
101+
.rotateX(radians)
102+
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
103+
return this;
104+
}
105+
106+
@Override
107+
public TransformedInstance rotateYCentered(float radians) {
108+
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
109+
.rotateY(radians)
110+
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
111+
return this;
112+
}
113+
114+
@Override
115+
public TransformedInstance rotateZCentered(float radians) {
116+
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
117+
.rotateZ(radians)
118+
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
119+
return this;
120+
}
121+
122+
@Override
123+
public TransformedInstance rotate(float radians, float axisX, float axisY, float axisZ) {
124+
pose.rotate(radians, axisX, axisY, axisZ);
125+
return this;
126+
}
127+
128+
@Override
129+
public TransformedInstance rotate(AxisAngle4f axisAngle) {
130+
pose.rotate(axisAngle);
131+
return this;
132+
}
133+
134+
@Override
135+
public TransformedInstance rotateX(float radians) {
136+
pose.rotateX(radians);
137+
return this;
138+
}
139+
140+
@Override
141+
public TransformedInstance rotateY(float radians) {
142+
pose.rotateY(radians);
143+
return this;
144+
}
145+
146+
@Override
147+
public TransformedInstance rotateZ(float radians) {
148+
pose.rotateZ(radians);
149+
return this;
150+
}
151+
152+
@Override
153+
public TransformedInstance rotateToFace(Direction facing) {
154+
// Need to invert the step because the super default method rotates from North (-Z),
155+
// but rotateTowards rotates from South (+Z)
156+
pose.rotateTowards(-facing.getStepX(), -facing.getStepY(), -facing.getStepZ(), 0, 1, 0);
157+
return this;
158+
}
87159
}

common/src/lib/java/dev/engine_room/flywheel/lib/transform/Affine.java

+57-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.mojang.math.Axis;
88

99
import net.minecraft.core.Direction;
10+
import net.minecraft.util.Mth;
1011

1112
public interface Affine<Self extends Affine<Self>> extends Translate<Self>, Rotate<Self>, Scale<Self> {
1213
default Self rotateAround(Quaternionfc quaternion, float x, float y, float z) {
@@ -15,19 +16,18 @@ default Self rotateAround(Quaternionfc quaternion, float x, float y, float z) {
1516
}
1617

1718
default Self rotateAround(Quaternionfc quaternion, Vector3fc vec) {
18-
return translate(vec.x(), vec.y(), vec.z()).rotate(quaternion)
19-
.translateBack(vec.x(), vec.y(), vec.z());
19+
return rotateAround(quaternion, vec.x(), vec.y(), vec.z());
2020
}
2121

2222
default Self rotateCentered(Quaternionfc q) {
2323
return rotateAround(q, CENTER, CENTER, CENTER);
2424
}
2525

26-
default Self rotateCentered(float radians, Vector3fc axis) {
26+
default Self rotateCentered(float radians, float axisX, float axisY, float axisZ) {
2727
if (radians == 0) {
2828
return self();
2929
}
30-
return rotateCentered(new Quaternionf().setAngleAxis(radians, axis.x(), axis.y(), axis.z()));
30+
return rotateCentered(new Quaternionf().setAngleAxis(radians, axisX, axisY, axisZ));
3131
}
3232

3333
default Self rotateCentered(float radians, Axis axis) {
@@ -37,10 +37,59 @@ default Self rotateCentered(float radians, Axis axis) {
3737
return rotateCentered(axis.rotation(radians));
3838
}
3939

40+
default Self rotateCentered(float radians, Vector3fc axis) {
41+
return rotateCentered(radians, axis.x(), axis.y(), axis.z());
42+
}
43+
44+
default Self rotateCentered(float radians, Direction.Axis axis) {
45+
return rotateCentered(radians, Direction.fromAxisAndDirection(axis, Direction.AxisDirection.POSITIVE));
46+
}
47+
4048
default Self rotateCentered(float radians, Direction axis) {
41-
if (radians == 0) {
42-
return self();
43-
}
44-
return rotateCentered(radians, axis.step());
49+
return rotateCentered(radians, axis.getStepX(), axis.getStepY(), axis.getStepZ());
50+
}
51+
52+
default Self rotateCenteredDegrees(float degrees, float axisX, float axisY, float axisZ) {
53+
return rotateCentered(Mth.DEG_TO_RAD * degrees, axisX, axisY, axisZ);
54+
}
55+
56+
default Self rotateCenteredDegrees(float degrees, Axis axis) {
57+
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
58+
}
59+
60+
default Self rotateCenteredDegrees(float degrees, Vector3fc axis) {
61+
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
62+
}
63+
64+
default Self rotateCenteredDegrees(float degrees, Direction axis) {
65+
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
66+
}
67+
68+
default Self rotateCenteredDegrees(float degrees, Direction.Axis axis) {
69+
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
70+
}
71+
72+
default Self rotateXCentered(float radians) {
73+
return rotateCentered(radians, Axis.XP);
74+
}
75+
76+
default Self rotateYCentered(float radians) {
77+
return rotateCentered(radians, Axis.YP);
78+
}
79+
80+
default Self rotateZCentered(float radians) {
81+
return rotateCentered(radians, Axis.ZP);
82+
}
83+
84+
default Self rotateXCenteredDegrees(float degrees) {
85+
return rotateXCentered(Mth.DEG_TO_RAD * degrees);
86+
}
87+
88+
default Self rotateYCenteredDegrees(float degrees) {
89+
return rotateYCentered(Mth.DEG_TO_RAD * degrees);
90+
}
91+
92+
default Self rotateZCenteredDegrees(float degrees) {
93+
return rotateZCentered(Mth.DEG_TO_RAD * degrees);
4594
}
4695
}

common/src/lib/java/dev/engine_room/flywheel/lib/transform/Rotate.java

+26-31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mojang.math.Axis;
99

1010
import net.minecraft.core.Direction;
11+
import net.minecraft.util.Mth;
1112

1213
public interface Rotate<Self extends Rotate<Self>> {
1314
Self rotate(Quaternionfc quaternion);
@@ -16,11 +17,12 @@ default Self rotate(AxisAngle4f axisAngle) {
1617
return rotate(new Quaternionf(axisAngle));
1718
}
1819

19-
default Self rotate(float radians, Vector3fc axis) {
20+
default Self rotate(float radians, float axisX, float axisY, float axisZ) {
2021
if (radians == 0) {
2122
return self();
2223
}
23-
return rotate(new Quaternionf().setAngleAxis(radians, axis.x(), axis.y(), axis.z()));
24+
return rotate(new Quaternionf().setAngleAxis(radians, axisX, axisY, axisZ));
25+
2426
}
2527

2628
default Self rotate(float radians, Axis axis) {
@@ -30,47 +32,36 @@ default Self rotate(float radians, Axis axis) {
3032
return rotate(axis.rotation(radians));
3133
}
3234

35+
default Self rotate(float radians, Vector3fc axis) {
36+
return rotate(radians, axis.x(), axis.y(), axis.z());
37+
}
38+
3339
default Self rotate(float radians, Direction axis) {
34-
if (radians == 0) {
35-
return self();
36-
}
37-
return rotate(radians, axis.step());
40+
return rotate(radians, axis.getStepX(), axis.getStepY(), axis.getStepZ());
3841
}
3942

4043
default Self rotate(float radians, Direction.Axis axis) {
41-
return switch (axis) {
42-
case X -> rotateX(radians);
43-
case Y -> rotateY(radians);
44-
case Z -> rotateZ(radians);
45-
};
44+
return rotate(radians, Direction.fromAxisAndDirection(axis, Direction.AxisDirection.POSITIVE));
4645
}
4746

48-
default Self rotateDegrees(float degrees, Vector3fc axis) {
49-
if (degrees == 0) {
50-
return self();
51-
}
52-
return rotate((float) Math.toRadians(degrees), axis);
47+
default Self rotateDegrees(float degrees, float axisX, float axisY, float axisZ) {
48+
return rotate(Mth.DEG_TO_RAD * degrees, axisX, axisY, axisZ);
5349
}
5450

5551
default Self rotateDegrees(float degrees, Axis axis) {
56-
if (degrees == 0) {
57-
return self();
58-
}
59-
return rotate(axis.rotationDegrees(degrees));
52+
return rotate(Mth.DEG_TO_RAD * degrees, axis);
53+
}
54+
55+
default Self rotateDegrees(float degrees, Vector3fc axis) {
56+
return rotate(Mth.DEG_TO_RAD * degrees, axis);
6057
}
6158

6259
default Self rotateDegrees(float degrees, Direction axis) {
63-
if (degrees == 0) {
64-
return self();
65-
}
66-
return rotate((float) Math.toRadians(degrees), axis);
60+
return rotate(Mth.DEG_TO_RAD * degrees, axis);
6761
}
6862

6963
default Self rotateDegrees(float degrees, Direction.Axis axis) {
70-
if (degrees == 0) {
71-
return self();
72-
}
73-
return rotate((float) Math.toRadians(degrees), axis);
64+
return rotate(Mth.DEG_TO_RAD * degrees, axis);
7465
}
7566

7667
default Self rotateX(float radians) {
@@ -86,15 +77,15 @@ default Self rotateZ(float radians) {
8677
}
8778

8879
default Self rotateXDegrees(float degrees) {
89-
return rotateDegrees(degrees, Axis.XP);
80+
return rotateX(Mth.DEG_TO_RAD * degrees);
9081
}
9182

9283
default Self rotateYDegrees(float degrees) {
93-
return rotateDegrees(degrees, Axis.YP);
84+
return rotateY(Mth.DEG_TO_RAD * degrees);
9485
}
9586

9687
default Self rotateZDegrees(float degrees) {
97-
return rotateDegrees(degrees, Axis.ZP);
88+
return rotateZ(Mth.DEG_TO_RAD * degrees);
9889
}
9990

10091
default Self rotateToFace(Direction facing) {
@@ -108,6 +99,10 @@ default Self rotateToFace(Direction facing) {
10899
};
109100
}
110101

102+
default Self rotateTo(Vector3fc from, Vector3fc to) {
103+
return rotate(new Quaternionf().rotateTo(from, to));
104+
}
105+
111106
@SuppressWarnings("unchecked")
112107
default Self self() {
113108
return (Self) this;

0 commit comments

Comments
 (0)