I have camera rotating by dragging mouse.
Im trying to add reset button that restart the scene to initial view.
The rotating camera works as expected but I can't figure out how to reset it.
The scene rotating in weird directions, not as expected.

I have 2 vars - absX, absY which hold the overall change angle value from the init mode.

when reset applied:

mat4.rotate(getRotationCameraMat, degToRad(-absX/10), [0, 1, 0]);
mat4.rotate(getRotationCameraMat, degToRad(-absY/10), [1, 0, 0]);

apply the rotate:

mat4.multiply(mvMatrix, cameraRotationMat);

the camera code:

var mouseDown = false;
var prevMouseX = null;
var prevMouseY = null;

var cameraRotationMat = mat4.create();

function handleMouseDown(event){
    mouseDown = true;
    prevMouseX = event.clientX;
    prevMouseY = event.clientY;

function handleMouseUp(event){
    mouseDown = false;
var absX=0;
var absY=0;

function handleMouseMove(event){
    if (!mouseDown){

    var currentX = event.clientX;
    var currentY = event.clientY;
    var deltaX = currentX - prevMouseX;
    var newRotationMatrix = mat4.create();

    mat4.rotate(newRotationMatrix, degToRad(deltaX/10), [0, 1, 0]);

    var deltaY = currentY-prevMouseY;
    mat4.rotate(newRotationMatrix, degToRad(deltaY/10), [1, 0, 0]);

    mat4.multiply(newRotationMatrix, cameraRotationMat,cameraRotationMat);

    prevMouseX = currentX;
    prevMouseY = currentY;


1 Answer 1


You don't need to do anything like that. If your camefa had no rotation before the user started using the apl, then you can simply create an identity matrix and set the camera's view matrix to it, or set every value back to 0.

If your camera had an initial position, you should store it in an object

var initialPos = {
    position: pos.clone(),
    // Do the same with the rotation
    // Make sure you clone the vectors so you don't have reference problems

Then when you want to reset it to the original position, you just set everything back.

If you don't use positions and rotations, and you immedialitely apply the transformations to the matrix (which is a terrible idea btw), then you can just copy the matrix and store it somewhere, then use it later to change it back.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .