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.

How do I apply an arcball (using quaternions) along with mouse events, to allow the user to look around the screen using the o3d WebGL framework?

This sample uses the arcball for rotating the transform of an "object," but rather than apply this to a transform, I would like to apply the rotation to the camera's target, to create a first person style ability to look around the scene, as if the camera is inside the centre of the arcball instead of rotating from the outside.

The code that is used in this sample is

var rotationQuat = g_aball.drag([e.x, e.y]);
var rot_mat = g_quaternions.quaternionToRotation(rotationQuat);
g_thisRot = g_math.matrix4.mul(g_lastRot, rot_mat);

The code that I am using which doesn't work

var rotationQuat = g_aball.drag([e.x, e.y]);
var rot_mat = g_quaternions.quaternionToRotation(rotationQuat);
g_thisRot = g_math.matrix4.mul(g_lastRot, rot_mat);

var cameraRotationMatrix4 = g_math.matrix4.lookAt(
  g_eye, g_target, [g_up[0], g_up[1] * -1, g_up[2]]
);
var cameraRotation = g_math.matrix4.setUpper3x3(
  cameraRotationMatrix4, g_thisRot
);
g_target = g_math.addVector(cameraRotation, g_target);

where am I going wrong?

share|improve this question
    
How exactly does it "not work?" –  Josh Petrie Jan 10 '11 at 17:25
add comment

2 Answers

Without knowing exactly how it "doesn't work," it's a bit hard to say. I will say that the canonical arcball approach involves projection the 2D screen position onto the surface of an imaginary sphere that surrounds the object, and might be overcomplex for the kind of rotation you want for your camera.

It sounds like you would be able to get away with building a shortest-arc quaternion between the previous mouse position and the new one (in screen space, during a drag) and using that quaternion to modify your view matrix.

share|improve this answer
add comment

I agree with Josh Petrie's answer, use ray cast to find intersection with an imaginary sphere now find the vector from intersection point to the center of the sphere for the first mouse position, now repeat the step for the next mouse position, you will have two vectors, make a cross product to the find the axis that you should rotate your camera around.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.