Note that there are some explanatory texts on larger screens.

plurals
  1. POrotate a globe with the mouse
    primarykey
    data
    text
    <p>I can determine where on a globe the user has <a href="https://stackoverflow.com/questions/14964257/clicking-on-a-sphere">clicked</a>.</p> <p>When the user first clicks the mouse button down, I can store where they have clicked; lets call it the <code>pin</code>.</p> <p>Then, as they drag the mouse around, I want to put the <code>pin</code> under the mouse cursor.</p> <p><img src="https://i.stack.imgur.com/tYacR.jpg" alt="enter image description here"></p> <p>Here is some code that seems to roughly keep the <code>pin</code> under the mouse pointer, but doesn't correctly maintain the globe; it flickers and spins randomly. Sometimes it flips the axis entirely so the globe is momentarily back-to-front.</p> <pre><code>function evtPos(evt) { if(!scene.ortho) return null; var x = lerp(scene.ortho[0],scene.ortho[1],evt.clientX/canvas.width), y = lerp(scene.ortho[3],scene.ortho[2],evt.clientY/canvas.height), // flipped sqrd = x*x+y*y; return (sqrd &gt; 1)? null: mat4_vec3_multiply(mat4_inverse(scene.mvMatrix),[x,y,Math.sqrt(1-sqrd)]); } function onMouseDown(evt) { pin = evtPos(evt); } function onMouseMove(evt,keys,isMouseDown) { if(!isMouseDown) return; var pt = evtPos(evt); if(pin == null) pin = pt; if(pt == null) return; var d = vec3_sub(pt,pin), rotx = Math.atan2(d[1],d[2]), roty = (d[2] &gt;= 0)? -Math.atan2(d[0] * Math.cos(rotx),d[2]): Math.atan2(d[0] * Math.cos(rotx),-d[2]), rotz = Math.atan2(Math.cos(rotx),Math.sin(rotx)*Math.sin(roty)); scene.mvMatrix = mat4_multiply(scene.mvMatrix,mat4_rotation(rotx,[1,0,0])); scene.mvMatrix = mat4_multiply(scene.mvMatrix,mat4_rotation(roty,[0,1,0])); scene.mvMatrix = mat4_multiply(scene.mvMatrix,mat4_rotation(rotz,[0,0,1])); } function onMouseUp(evt) { pin = null; } </code></pre> <p>Also, over time, an error seems to build up and the <code>pin</code> drifts further and further from the mouse pointer. I presume I should somehow compute the <code>mvMatrix</code> completely rather than by lots of samll increments each event?</p> <p>I want the user to be able to drag the globe around to navigate naturally. All code to spin globes that I've found uses fixed speeds e.g. arrow keys, rather than 'pinning' the globe under a mouse pointer. Unity has a function <code>Quaternion.FromToRotation(fromPos,toPos)</code> which seems very promising but the source is not available.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload