Situation: From a first person perspective (camera position) I want to be able to grab an object and have it fixed to the center center of the screen while looking up-down (pitch) and looking left-right (yaw). The yaw works fine. The pitch only works facing the object head on when camera position z > object z. Turning 180 degrees the pitch rotation is opposite. How do I compensate for this. This is what have:
double yawRadians = Math.toRadians(yaw - oldYaw);
double pitchRadians = Math.toRadians(pitch - oldPitch);
//center position of object, minus camera position
double x = grabbedObject.x0 - position.x;
double y = grabbedObject.y0 - position.y;
double z = grabbedObject.z0 - position.z;
double sinX = Math.sin(pitchRadians);
double cosX = Math.cos(pitchRadians);
double sinY = Math.sin(yawRadians);
double cosY = Math.cos(yawRadians);
double sinXY = sinX * y;
double cosXZ = cosX * z;
double newX = cosY * x + sinY * sinXY - sinY * cosXZ;
double newY = cosX * y + sinX * z;
double newZ = sinY * x + cosY * -sinXY + cosY * cosXZ;
grabbedObject.addTranslation(newX - x, newY - y, newZ - z);