# Camera Moving

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've searched high and low for a good explination on how to do this simply. I have figured this out so far: I can get the cross product of my lookAt vector and the vector of my last mouse pos - new mouse pos vector to get the axis I want to rotate (I believe), but how much do I rotate it? Whats the angle, I can get my current mouse position x and subtract that from my last mouse position x, but how do I translate that into an angle to rotate by? I want it so I can mouse the mouse around and my camera looks in that direction. Thanks Jeff.

##### Share on other sites
You rotate it by how much the mouse moved during the last frame, probably multiplied by some scalar value (sensitivity)

so it oldMousePos was (5,8)
and newMousePos is (10, 10)

mouseMoved = (5,2)

Assuming your world coordinate system (x is right, y is forwards, and z is up), Then you need to rotate 5 degree about the Z axis(heading) and 2 degree about the X axis(pitch)

This is a first person camera set up, not to sure if its exactly the answer to your solution, i have a little trouble picturing your exact problem.

##### Share on other sites
I use "gluLookAt(origin.x, origin.y, origin.z, target.x, target.y, target.z, upVector.x, upVector.y, upVector.z)" with this:

private void getMouseAngle()
{
float angleY = 0.0f;
float angleZ = 0.0f;
final Vector3D axis;

// Get the direction the mouse moved in, and bring it down to a reasonable amount.
angleY = -(float)(Mouse.getDX()) / sensitivity;
angleZ = (float)(Mouse.getDY()) / sensitivity;

// If mouse is inverted, invert rotation on Z axis.
if(inverted)
angleZ = -angleZ;

// Keep track of the current X Rotation so the camera can't do a full loop.
// TODO Mess with this so looping is possible (fly like an old lerk in NS).
currentXRot -= angleZ;

// Cap the X Rotation within 1 radian (+/- 90 degrees)
if(currentXRot > 1.0f)
currentXRot = 1.0f;
else if(currentXRot < -1.0f)
currentXRot = -1.0f;
else
{
axis = FunkMath.crossProduct(FunkMath.subtract(target, origin), upVector);
axis.crappyNormalize();

// Rotate Z and Y axis.
rotate(angleZ, axis.x, axis.y, axis.z);
rotate(angleY, 0, 1, 0);
}
}

private void rotate(float angle, float x, float y, float z)
{
Vector3D newTarget = new Vector3D();
Vector3D currentTarget = new Vector3D();

// Get the current facing direction
currentTarget.x = target.x - origin.x;
currentTarget.y = target.y - origin.y;
currentTarget.z = target.z - origin.z;

// Store Sin/Cos
float cosTheta = (float)Math.cos(angle);
float sinTheta = (float)Math.sin(angle);

newTarget.x = (cosTheta + (1 - cosTheta) * x * x) * currentTarget.x;
newTarget.x += ((1 - cosTheta) * x * y - z * sinTheta) * currentTarget.y;
newTarget.x += ((1 - cosTheta) * x * z + y * sinTheta) * currentTarget.z;

newTarget.y = ((1 - cosTheta) * x * y + z * sinTheta) * currentTarget.x;
newTarget.y += (cosTheta + (1 - cosTheta) * y * y) * currentTarget.y;
newTarget.y += ((1 - cosTheta) * y * z - x * sinTheta) * currentTarget.z;

newTarget.z = ((1 - cosTheta) * x * z - y * sinTheta) * currentTarget.x;
newTarget.z += ((1 - cosTheta) * y * z + x * sinTheta) * currentTarget.y;
newTarget.z += (cosTheta + (1 - cosTheta) * z * z) * currentTarget.z;

target.x = origin.x + newTarget.x;
target.y = origin.y + newTarget.y;
target.z = origin.z + newTarget.z;
}

"crappynormalize" should be interpreted as a normalize. I have funny naming schemes for test algorithms.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×