Jump to content

Mouse x,y "lags behind" mouse cursor


sparkbuzz
 Share

Recommended Posts

The following code in my application is used to move a tiny sphere to the x, y, z picked point on a plane as the mouse moves.

document.addEventListener('mousemove', function (event:MouseEvent) {var pickingInfo:BABYLON.PickingInfo = this.scene.pick(event.x, event.y, function(mesh:BABYLON.AbstractMesh) {        return mesh.name == 'pick_plane';    });    if(pickingInfo.pickedMesh != null) {        sphere.position = pickingInfo.pickedPoint;    }}.bind(this));

The problem I'm noticing is that, even during moderate mouse movement, the sphere's position lags behind the browsers' mouse cursor.

 

I'm assuming that BabylonJS uses requestAnimationFrame() for managing the render loop, and I suppose, because the sphere position is updated in a 'mousemove' event handler, outside of that context, is what's causing the lagging sphere.

 

Perhaps also, because the mouse movement event handler doesn't get called at the same frame rate as the requestAnimationFrame() handler, is what's causing the lag? Would have been awesome if I could get the mouse x, y coordinates on the spot, say for example from the window object.

 

 

Is there a way I can get the sphere to more closely track the position of the mouse cursor?

Link to comment
Share on other sites

Maybe show us in a playground so that we can play around? ;)

 

That should be a boilerplate automated answer after scanning a new message and seeing there is no link to a playground scene  :D

 

Regarding your question - the scene has pointerX and pointerY exactly for this reason. no need to access the event's data.

Maybe, if possible (I am not sure about your usecase), try implementing the movement tracking inside the before-render loop (again, only if possible), this way you don't use any external js events (which, as you perfectly said, will be triggered only "between frames".).

Link to comment
Share on other sites

I'll try getting into the habit of creating playground samples. I'm coding in Typescript, so not always certain the code spat out by the tsc compiler will be human readable.

 

Thanks for the pointerX and pointerY pointer, hehe, will incorporate that and see if performance improves.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...