Skip to content

Commit 8f4c989

Browse files
committed
Added getForward getRight Fixed tranform order
1 parent 10e25cf commit 8f4c989

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/main/java/engine/components/Transform.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public Transform() {
5757
* @param g The graphics context to which this transformation is applied.
5858
*/
5959
public void apply(Graphics g) {
60-
g.translate(position.x, position.y, position.z);
61-
g.rotateX(rotation.x);
60+
g.scale(scale.x, scale.y, scale.z); // Scale first
61+
g.rotateX(rotation.x); // Then rotate
6262
g.rotateY(rotation.y);
6363
g.rotateZ(rotation.z);
64-
g.scale(scale.x, scale.y, scale.z);
64+
g.translate(position.x, position.y, position.z); // Translate last
6565
}
6666

6767
/**
@@ -88,6 +88,17 @@ public void rotate(Vector3f delta) {
8888
this.rotation.addLocal(delta);
8989
}
9090

91+
/**
92+
* Rotates this transformation by the given delta values for each axis (in radians).
93+
*
94+
* @param x The change in rotation around the X-axis (in radians).
95+
* @param y The change in rotation around the Y-axis (in radians).
96+
* @param z The change in rotation around the Z-axis (in radians).
97+
*/
98+
public void rotate(float x, float y, float z) {
99+
this.rotation.addLocal(x, y, z);
100+
}
101+
91102
/**
92103
* Scales this transformation by the provided scaling factors.
93104
*
@@ -208,6 +219,38 @@ public void setScale(float sx, float sy, float sz) {
208219
this.scale.set(sx, sy, sz);
209220
}
210221

222+
/**
223+
* Retrieves the forward direction of the transform, based on its current rotation.
224+
*
225+
* <p>The forward direction is calculated using the rotation values and represents the vector that
226+
* points in the direction the object is facing.
227+
*
228+
* @return A normalized {@link Vector3f} representing the forward direction of the object.
229+
*/
230+
public Vector3f getForward() {
231+
float cosY = (float) Math.cos(rotation.y);
232+
float sinY = (float) Math.sin(rotation.y);
233+
float cosX = (float) Math.cos(rotation.x);
234+
float sinX = (float) Math.sin(rotation.x);
235+
236+
return new Vector3f(cosY * cosX, sinX, sinY * cosX).normalizeLocal();
237+
}
238+
239+
/**
240+
* Retrieves the right direction of the transform, based on its current rotation.
241+
*
242+
* <p>The right direction is calculated using the rotation values and represents the vector that
243+
* points to the right of the object.
244+
*
245+
* @return A normalized {@link Vector3f} representing the right direction of the object.
246+
*/
247+
public Vector3f getRight() {
248+
float cosY = (float) Math.cos(rotation.y);
249+
float sinY = (float) Math.sin(rotation.y);
250+
251+
return new Vector3f(-sinY, 0, cosY).normalizeLocal();
252+
}
253+
211254
@Override
212255
public void update(float tpf) {}
213256

src/main/java/workspace/GraphicsPImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ public void rotateZ(float angle) {
340340
g.rotate(angle);
341341
}
342342

343+
@Override
344+
public void rotate(float rx, float ry, float rz) {
345+
g.rotateX(rx);
346+
g.rotateY(ry);
347+
g.rotateZ(rz);
348+
}
349+
343350
public void camera() {
344351
g.camera();
345352
}
@@ -453,7 +460,7 @@ public void applyCamera(Camera camera) {
453460
if (camera == null) {
454461
throw new IllegalArgumentException("Camera instance cannot be null.");
455462
}
456-
463+
457464
float fov = camera.getFieldOfView();
458465
float aspect = camera.getAspectRatio();
459466
float near = camera.getNearPlane();
@@ -462,6 +469,6 @@ public void applyCamera(Camera camera) {
462469

463470
Vector3f target = camera.getTarget();
464471
Vector3f eye = camera.getTransform().getPosition();
465-
g.camera(eye.x, eye.y, eye.z, target.x, target.y, target.z, 0, -1, 0);
472+
g.camera(eye.x, eye.y, eye.z, target.x, target.y, target.z, 0, 1, 0);
466473
}
467474
}

0 commit comments

Comments
 (0)