How to rotate a vector in Mouse handler.
The following code rotates a viewdirection vector based on changes in the mouse:
// this code is called whenever the mouse moves
// Gangle is a global angle variable with the current view angle around the up
// vector of the Tank
function handleMouseMove(event) {
if (!mouseDown) { // only do things when mouse button down.
return;
}
var newX = event.clientX;
var newY = event.clientY;
var deltaX = newX - lastMouseX;
// get the adjust changing angle
Gangle = Gangle + deltaX;
// adjust the speed of the tank
var deltaY = newY - lastMouseY;
Gspeed = Gspeed - deltaY/1000000.0;
lastMouseX = newX
lastMouseY = newY;
// we have the latest angle, now lets rotate the viewdirection
upVector = [0.0,1.0,0.0]; //we will just use Y as the up vector
tankRotation = mat4.create();
mat4.identity(tnakRotation);
// create the rotation matrix
mat4.rotate(tankRotation, -degToRad(Gangle), upVector);
// ok now rotate the viewdirection by the current angle
mat4.multiplyVec3(tankRotation, viewdirection);
// so viewdirection now changes as the mouse is moved left and right.
// if your code is using viewdirection in your genViewMatrix function
// this will give you the ability to rotate your view.
}
The following helper function degToRad() is useful for converting angles to Radians.
function degToRad(degrees)
{
return degrees * Math.PI / 180;
}