Ich habe jetzt letztens eine Kamera zu meiner eigenen 3D Grafik hinzugefügt, doch wenn ich diese drehen möchte, drehe ich den Raum mit, wodurch ich manchmal an den Objekten bin und manchmal nicht. Hier das wichtigste (Vertex Klasse)
Java:
public double[] getRotationOnXAxis(Camera camera, double y, double z) {
Matrix yAndZ = getRotationOnAxis(y - camera.y, z - camera.z);
yAndZ = yAndZ.multiply(getRotationMatrix(camera.xRotation));
return yAndZ.numbers[0];
}
public double[] getRotationOnYAxis(Camera camera, double x, double z) {
Matrix xAndZ = getRotationOnAxis(x - camera.x, z - camera.z);
xAndZ = xAndZ.multiply(getRotationMatrix(camera.yRotation));
return xAndZ.numbers[0];
}
public double[] getRotationOnZAxis(Camera camera, double x, double y) {
Matrix xAndY = getRotationOnAxis(x - camera.x, y - camera.y);
xAndY = xAndY.multiply(getRotationMatrix(camera.zRotation));
return xAndY.numbers[0];
}
private Matrix getRotationOnAxis(double firstPos, double secondPos) {
Matrix rotationOnAxisMatrix = new Matrix(new double[1][2]);
rotationOnAxisMatrix.numbers[0][0] = firstPos;
rotationOnAxisMatrix.numbers[0][1] = secondPos;
return rotationOnAxisMatrix;
}
private Matrix getRotationMatrix(double theta) {
Matrix rotationMatrix = new Matrix(new double[2][2]);
rotationMatrix.numbers[0][0] = Math.cos(theta);
rotationMatrix.numbers[0][1] = -Math.sin(theta);
rotationMatrix.numbers[1][0] = Math.sin(theta);
rotationMatrix.numbers[1][1] = Math.cos(theta);
return rotationMatrix;
}
public double getProjectedX(Camera camera) {
double x = position.numbers[0][0] + middle.numbers[0][0];
double y = position.numbers[0][1] + middle.numbers[0][1];
double z = position.numbers[0][2] + middle.numbers[0][2];
//rotate
double[] newPos = new double[2];
newPos = getRotationOnXAxis(camera, y, z);
y = newPos[0];
z = newPos[1];
newPos = getRotationOnYAxis(camera, x, z);
x = newPos[0];
z = newPos[1] + camera.z;
newPos = getRotationOnZAxis(camera, x, y);
x = newPos[0] + camera.x;
y = newPos[1] + camera.y;
return translate(x, z, camera.focalLength);
}
public double getProjectedY(Camera camera) {
double x = position.numbers[0][0] + middle.numbers[0][0];
double y = position.numbers[0][1] + middle.numbers[0][1];
double z = position.numbers[0][2] + middle.numbers[0][2];
//rotate
double[] newPos = new double[2];
newPos = getRotationOnXAxis(camera, y, z);
y = newPos[0];
z = newPos[1];
newPos = getRotationOnYAxis(camera, x, z);
x = newPos[0];
z = newPos[1];
newPos = getRotationOnZAxis(camera, x, y);
x = newPos[0];
y = newPos[1];
return translate(y, z, camera.focalLength);
}
@SuppressWarnings("deprecation")
private double translate(double xOrY, double z, double focalLength) {
return new BigDecimal(focalLength*xOrY).divide(
new BigDecimal(focalLength+z), BigDecimal.ROUND_HALF_DOWN).doubleValue();
}