www.webdeveloper.com
Results 1 to 5 of 5

Thread: Mouse movement for an object

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    2

    Mouse movement for an object

    I have a cube that I want to move using mouse movements in javascript.
    I wrote the following code but I need a little bit help.
    I cant get it to work. Any help would be appreciated.
    Thanks

    Code:
    console.log(models["cube"].model.vertexArray.length);
    
        document.addEventListener('mouse', function(event) {
            if(document.onmousemove = Mouse.handleMouseMove;) { //move around
                mat4.identity(matRot);
                mat4.identity(matrixRotated);
                mat4.rotateY(matRot,-Math.PI/50,matrixRotated);
                models["cube"].model.rotate(matrixRotated);
    
            }
            else if(document.onmouseup = Mouse.handleMouseUp;) { // up
                models["cube"].model.translate(0,0,-0.1);
            }
            else if(document.onmousedown = Mouse.handleMouseDown;) {// down         
                models["cube"].model.translate(0,0,0.05);
            }
        });

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Do not use matrix to move a cube... Use only Euclidean vector dot product and cross product.
    See for exemple this page about a 2x2x2 Rubik'cube

  3. #3
    Join Date
    Dec 2013
    Posts
    21
    So, essentially you want drag-n-drop type movement?

  4. #4
    Join Date
    Jan 2014
    Posts
    2
    Thanks for the replys. I appreciate them.
    I dont want to drag and move or drag n drop. Since I have the following line
    console.log(models["cube"].model.vertexArray.length);

    It should my mouse should be initialized as the object am I correct?

    So now I just wanna be able to move my mouse around and i want the object to move around just like the mouse.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Your code is not sufficient ... Your tests on functions seem wrong ... What is Mouse ?

    Your have probably to work with mouse coordinates and with three events :

    onmousedown : 1/- store the mouse initial coordinates, 2/- store them in old coordinates and 3/- set a boolean okMov to true
    onmousemouse : if (okMov) {1/- store the mouse current coordinates, 2/- rotate the cube with current-old ccordinates and 3/- store current coordinates in old coordinates}
    onmouseup : okMov=false;

    The mouse coordinates and the boolean okMov are obviously global variables...
    See this simple page cube.htm (line 84 to 113 - we also define touchstart, touchmove and touchrend) for the principles. You have only to change the move ...
    Last edited by 007Julien; 01-06-2014 at 05:45 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles