PVector zAxis = new PVector();
PVector up = new PVector();
PVector objToCamera = new PVector();
PVector objectToCameraProjection = new PVector();
PVector lookAt = new PVector(0, 0, 1);
public void rotateToCamera(PGraphics _buffer, PVector objectPosition, PVector cameraPosition)
{
objectToCameraProjection.x = cameraPosition.x - objectPosition.x;
objectToCameraProjection.y = 0;
objectToCameraProjection.z = cameraPosition.z - objectPosition.z;
objectToCameraProjection.normalize();
PVector.cross(lookAt, objectToCameraProjection, up);
float 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);
}
PVector.sub(cameraPosition, objectPosition, objToCamera);
objToCamera.normalize();
angle = PVector.dot(objectToCameraProjection, objToCamera);
// parent.println(parent.acos(angle) * 180 / parent.PI);
if ((angle < 0.9999f) && (angle > -0.9999f)) {
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);
}
}
}