PVector zAxis = new PVector();
PVector up = new PVector();
PVector objToCamera = new PVector();
PVector objectToCameraProjection = new PVector();
PVector lookAt = new PVector(0, 0, 1);
float angle;
public void rotateToCamera(PGraphics _buffer, PVector objectPosition, PVector cameraPosition)
{
_buffer.pushMatrix();
objectToCameraProjection.x = cameraPosition.x - objectPosition.x;
objectToCameraProjection.y = 0;
objectToCameraProjection.z = cameraPosition.z - objectPosition.z;
objectToCameraProjection.normalize();
up = lookAt.cross(objectToCameraProjection);
angle = PVector.dot(lookAt, objectToCameraProjection);
if ((angle < 0.9999f) && (angle > -0.9999f)) {
// _buffer.rotate(angle, up.x, up.y, up.z);
_buffer.rotate(parent.acos(angle) * 180 / parent.PI, up.x, up.y, up.z);
}
objToCamera.x = cameraPosition.x - objectPosition.x;
objToCamera.y = cameraPosition.y - objectPosition.y;
objToCamera.z = cameraPosition.z - objectPosition.z;
objToCamera.normalize();
angle = PVector.dot(objectToCameraProjection, objToCamera);
// parent.println(parent.acos(angle) * 180 / parent.PI);
if ((angle < 2f) && (angle > -2f)) {
if (objToCamera.y < 0) {
_buffer.rotate(parent.acos(angle) * 180 / parent.PI, 1, 0, 0);
} else {
_buffer.rotate(parent.acos(angle) * 180 / parent.PI, -1, 0, 0);
}
}
}
Processing Forum