Added rotate function to Vector3 and cleaned up WorldObject

pull/35/head
James Ball 2020-02-11 22:11:14 +00:00
rodzic 0346c6eb16
commit dd35221625
2 zmienionych plików z 30 dodań i 25 usunięć

Wyświetl plik

@ -31,49 +31,57 @@ public class Vector3 {
public Vector3 add(Vector3 other) {
return new Vector3(
this.getX() + other.getX(),
this.getY() + other.getY(),
this.getZ() + other.getZ()
getX() + other.getX(),
getY() + other.getY(),
getZ() + other.getZ()
);
}
public Vector3 scale(double scalar) {
public Vector3 scale(double factor) {
return new Vector3(
this.getX() * scalar,
this.getY() * scalar,
this.getZ() * scalar
getX() * factor,
getY() * factor,
getZ() * factor
);
}
public Vector3 rotateX(double delta) {
public Vector3 rotate(Vector3 rotation) {
return rotateX(rotation.getX())
.rotateY(rotation.getY())
.rotateZ(rotation.getZ());
}
public Vector3 rotateX(double theta) {
return new Vector3(
getX(),
Math.cos(delta) * getY() - Math.sin(delta) * getZ(),
Math.sin(delta) * getY() + Math.cos(delta) * getZ()
Math.cos(theta) * getY() - Math.sin(theta) * getZ(),
Math.sin(theta) * getY() + Math.cos(theta) * getZ()
);
}
public Vector3 rotateY(double delta) {
public Vector3 rotateY(double theta) {
return new Vector3(
Math.cos(delta) * getX() + Math.sin(delta) * getZ(),
Math.cos(theta) * getX() + Math.sin(theta) * getZ(),
getY(),
-Math.sin(delta) * getX() + Math.cos(delta) * getZ()
-Math.sin(theta) * getX() + Math.cos(theta) * getZ()
);
}
public Vector3 rotateZ(double delta) {
public Vector3 rotateZ(double theta) {
return new Vector3(
Math.cos(delta) * getX() - Math.sin(delta) * getY(),
Math.sin(delta) * getX() + Math.cos(delta) * getY(),
Math.cos(theta) * getX() - Math.sin(theta) * getY(),
Math.sin(theta) * getX() + Math.cos(theta) * getY(),
getZ()
);
}
public static Vector3 meanPoint(List<Vector3> points) {
Vector3 mean = new Vector3();
for(Vector3 point : points) {
for (Vector3 point : points) {
mean = mean.add(point);
}
return mean.scale(1f / (points.size()));
}
}

Wyświetl plik

@ -16,19 +16,16 @@ public class WorldObject {
}
public void rotate(Vector3 theta) {
this.rotation = this.rotation.add(theta);
rotation = rotation.add(theta);
}
public List<Vector3> getVertices() {
List<Vector3> vertices = new ArrayList<>();
for(Vector3 vertex : mesh.getVertices()) {
vertices.add(
vertex
.rotateX(this.rotation.getX())
.rotateY(this.rotation.getY())
.rotateZ(this.rotation.getY())
.add(this.position));
for (Vector3 vertex : mesh.getVertices()) {
vertices.add(vertex.rotate(rotation).add(position));
}
return vertices;
}