Hallöchen Loits!
Ich würde gerne Objekte bewegen. Und zwar so, dass "Vorwärts" bedeutet, dass sich das Objekt von seiner eigenen Rotation aus nach vorne bewegt. Das habe ich auch schon so weit hinbekommen. Das funktioniert auch alles soweit gut, solange man das Objekt NICHT auf der Z Achse rotiert. Wenn man das tut, dann verhält sich das Objekt etwas merkwürdig (bewegt sich nur ungefähr in die Richtung in die es soll, dennoch falsch).
Was genau mache ich falsch? Irgendwo muss ein Denkfehler sein.
Hier ist der Code:
Ich würde gerne Objekte bewegen. Und zwar so, dass "Vorwärts" bedeutet, dass sich das Objekt von seiner eigenen Rotation aus nach vorne bewegt. Das habe ich auch schon so weit hinbekommen. Das funktioniert auch alles soweit gut, solange man das Objekt NICHT auf der Z Achse rotiert. Wenn man das tut, dann verhält sich das Objekt etwas merkwürdig (bewegt sich nur ungefähr in die Richtung in die es soll, dennoch falsch).
Was genau mache ich falsch? Irgendwo muss ein Denkfehler sein.
Hier ist der Code:
Java:
package de.ralleytn.engine.lunatic;
import javax.vecmath.Vector3f;
public interface Movable extends Translatable, Rotatable {
public default void move(Direction direction, float units) {
float nUnits = -units;
Vector3f rotation = this.getRotation();
if(direction == Direction.LEFT) {
float rY = (float)Math.toRadians(rotation.y);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.cos(rY) * nUnits;
float y = (float)Math.sin(rZ) * units;
float z = (float)Math.sin(rY) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.RIGHT) {
float rY = (float)Math.toRadians(rotation.y);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.cos(rY) * units;
float y = (float)Math.sin(rZ) * nUnits;
float z = (float)Math.sin(rY) * units;
this.translate(x, y, z);
} else if(direction == Direction.FORWARD) {
float rX = (float)Math.toRadians(rotation.x);
float rY = (float)Math.toRadians(rotation.y - 90.0F);
float x = (float)Math.cos(rY) * units;
float y = (float)Math.sin(rX) * nUnits;
float z = (float)Math.sin(rY) * units;
this.translate(x, y, z);
} else if(direction == Direction.BACKWARD) {
float rX = (float)Math.toRadians(rotation.x);
float rY = (float)Math.toRadians(rotation.y - 90.0F);
float x = (float)Math.cos(rY) * nUnits;
float y = (float)Math.sin(rX) * units;
float z = (float)Math.sin(rY) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.UP) {
float rX = (float)Math.toRadians(rotation.x);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.sin(rZ) * nUnits;
float y = (float)Math.cos(rZ) * units;
float z = (float)Math.sin(rX) * nUnits;
this.translate(x, y, z);
} else if(direction == Direction.DOWN) {
float rX = (float)Math.toRadians(rotation.x);
float rZ = (float)Math.toRadians(rotation.z);
float x = (float)Math.sin(rZ) * units;
float y = (float)Math.cos(rZ) * nUnits;
float z = (float)Math.sin(rX) * units;
this.translate(x, y, z);
}
}
}