Jump to content

Drag and drop


Temechon
 Share

Recommended Posts

Hello guys, 


 


I'm currently workng on several small thing with our favorite 3D engine, and one of these things is the mesh drag 'n drop.


I know this feature is in the babylon roundmap, but I will need it very soon for a bigger project.


 


Today, I have a drag and drop that is currently working in 2 dimensions (on X and Y axis). I use the scene.pick method to get the picked mesh.


Here is the code : 



var pickedMesh;
// Add event listener
window.addEventListener("click", function(evt){
var pick = scene.pick(evt.clientX, evt.clientY);
if (pick.pickedMesh) {
pickedMesh = pick.pickedMesh;
}
});

window.addEventListener("mousemove", function(evt) {
if (pickedMesh){
var pick = scene.pick(evt.clientX, evt.clientY);
pickedMesh.position.x = pick.pickedPoint.x;
pickedMesh.position.y = pick.pickedPoint.y;
}
});

However, it is not working as intended : if the mouse is out of the selected mesh, then pick.pickedPoint is null, and the box position is not updated.


 


What I would like is a way to convert the mouse coordinates to scene coordinates. I tried to look at the class BABYLON.Ray, but I need a world matrix. In the method scene.pick, the mesh world matrix is used, but obviously i cannot use it (as my scene can be empty).


Is there a way to create a "scene.getWorldMatrix" in BABYLON ? What is the best way to do it ?


 


Thank you for your help !


 


Link to comment
Share on other sites

Hi Delta,

 

Thanks for your answer.

I tried to use the method scene.createPickingRay(x, y), with x and y my mouse coordinates. In this method, the identity matrix is used when world is not defined, so I guess I was using it without declaring it explicitly.

 

However, it does not work : the origin of the ray is always something like (0,9,0). (My camera is in (0,10,0) and look at (0,0,0) ).

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...