Nick
2004.10.10, 05:47 PM
I've figured that my problem is the math of rotating my camera. The old code took the screen coordinates of the mouse, rotated the camera based on that, and then set the mouse back to the center of the screen. With my new code I'm using mouse deltas so there is a small problem :/. Here's some of the code. If anybody has some code for a camera class that uses deltas and Objective-C (or C++ works I guess) I would appreciate that.
PLEASE remember that this code DOES NOT work. I don't want somebody searching for this thinking it works. In the second method where the variables mouseDeltaX and mouseDeltaY are, those used to be screen coordinates, not deltas which is what's throwing my class into a fury. If you have any ideas how to fix this, I'm willing to try anything.
- (void)rotate: (float)angle : (float)x : (float)y : (float)z
{
Vector *newView;
newView = [[Vector alloc] init];
view = [view subtract: position];
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);
[newView setX: ((cosTheta + (1 - cosTheta) * x * x) * [view x])];
[newView addX: (((1 - cosTheta) * x * y - z * sinTheta) * [view y])];
[newView addX: (((1 - cosTheta) * x * z + y * sinTheta) * [view z])];
[newView setY: (((1 - cosTheta) * x * y + z * sinTheta) * [view x])];
[newView addY: ((cosTheta + (1 - cosTheta) * y * y) * [view y])];
[newView addY: (((1 - cosTheta) * y * z - x * sinTheta) * [view z])];
[newView setZ: (((1 - cosTheta) * x * z - y * sinTheta) * [view x])];
[newView addZ: (((1 - cosTheta) * y * z + x * sinTheta) * [view y])];
[newView addZ: ((cosTheta + (1 - cosTheta) * z * z) * [view z])];
view = [view add: newView];
}
- (void)setViewOnMouse
{
float angleY,angleZ;
static float currentRotX;
angleY = (float)( mouseDeltaX ) / 1000.0f;
angleZ = (float)( mouseDeltaY ) / 1000.0f;
currentRotX -= angleZ;
if(currentRotX > 1.0f)
currentRotX = 1.0f;
else if(currentRotX < -1.0f)
currentRotX = -1.0f;
else
{
Vector *tempVector = [view subtract: position];
Vector *tempVector2 = [tempVector crossProduct: up];
[tempVector2 normalize];
[self rotate: angleZ : [tempVector2 x] : [tempVector2 y] : [tempVector2 z]];
[self rotate: angleY : 0 : 1 : 0];
}
}
PLEASE remember that this code DOES NOT work. I don't want somebody searching for this thinking it works. In the second method where the variables mouseDeltaX and mouseDeltaY are, those used to be screen coordinates, not deltas which is what's throwing my class into a fury. If you have any ideas how to fix this, I'm willing to try anything.
- (void)rotate: (float)angle : (float)x : (float)y : (float)z
{
Vector *newView;
newView = [[Vector alloc] init];
view = [view subtract: position];
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);
[newView setX: ((cosTheta + (1 - cosTheta) * x * x) * [view x])];
[newView addX: (((1 - cosTheta) * x * y - z * sinTheta) * [view y])];
[newView addX: (((1 - cosTheta) * x * z + y * sinTheta) * [view z])];
[newView setY: (((1 - cosTheta) * x * y + z * sinTheta) * [view x])];
[newView addY: ((cosTheta + (1 - cosTheta) * y * y) * [view y])];
[newView addY: (((1 - cosTheta) * y * z - x * sinTheta) * [view z])];
[newView setZ: (((1 - cosTheta) * x * z - y * sinTheta) * [view x])];
[newView addZ: (((1 - cosTheta) * y * z + x * sinTheta) * [view y])];
[newView addZ: ((cosTheta + (1 - cosTheta) * z * z) * [view z])];
view = [view add: newView];
}
- (void)setViewOnMouse
{
float angleY,angleZ;
static float currentRotX;
angleY = (float)( mouseDeltaX ) / 1000.0f;
angleZ = (float)( mouseDeltaY ) / 1000.0f;
currentRotX -= angleZ;
if(currentRotX > 1.0f)
currentRotX = 1.0f;
else if(currentRotX < -1.0f)
currentRotX = -1.0f;
else
{
Vector *tempVector = [view subtract: position];
Vector *tempVector2 = [tempVector crossProduct: up];
[tempVector2 normalize];
[self rotate: angleZ : [tempVector2 x] : [tempVector2 y] : [tempVector2 z]];
[self rotate: angleY : 0 : 1 : 0];
}
}