Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
share|improve this question
    
Do you nee to transform the grabbed object itself, or you just want to spectate it from a different angle? –  GPUquant Jan 23 '13 at 10:00
    
I need to transform the grabbed object itself. –  moby Jan 23 '13 at 12:39
add comment

closed as too localized by Byte56, Sean Middleditch, bummzack, Josh Petrie, Ali.S Mar 15 '13 at 23:25

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.