Jump to content

Mapping 2D Mouse XY to 3D XZ movement relative to camera


diamondage
 Share

Recommended Posts

Hi 

 

I am just starting out with Babylon JS for a research project and it seems I am stuck finding a solution for mapping 2D screen mouse coords to 3D XZ movements relative to the camera.  The project involves simple moving and stacking cubes.  The problem is when my camera is looking into the z-axis I am able to figure out how to make the x and y of the mouse related to XZ in the app.  However this goes to heck when I move the camera out of the z axis say by 90 degrees.  The x and y will still translate the object as x and z but in from the camera perspective I've turned 90 degrees and the x and y of the mouse should change to z and x.  I've tried unproject but it seems I don't understand the problem well enough and am missing the Z for the screen coordinates.  I cannot use the pick ground strategy because it causes blocks stacked on top to 'jump' to the ray that hits the ground behind it.

 

My sample is here:

http://www.babylonjs-playground.com/#BTHKN

 

Thanks in advance for any help!

Wil

Link to comment
Share on other sites

Hi Deltakosh,

 

Great questions.  This technique only works when you are able to select an object with a ray that hits the ground.  If I start to stack cubes 3 to 4 high, and you move the camera almost parallel to the ground, the pick ray will not hit the ground.  As a result we will be unable to move the cube until the pick ray (the mouse) is moved to somewhere on the ground.

 

thanks,

Will

Link to comment
Share on other sites

Hi Delta,

 

Yes I have, and the issue with creating a hover plane is that if you are right at the center y point of the camera you can't select and move the object.  Again it only works if you are looking slightly down at the plane.  Where as if I can figure out how to unproject the value for the XY screen coordinates to the object when the camera is not just looking at the Z axis that would be optimal.

 

W

Link to comment
Share on other sites

Hi Delta,

 

That is correct if your camera is as I positioned it in the example looking at the Z axis.  If you use the arrow keys and click mouse to rotate the camera 90 deg or even 180 the x and y no longer maps to X Z.  If you rotate to 90 deg, mouse X is not Z and mouse Y should be X.  So I am missing a transform that takes into account of the camera look at vector.

 

W

Link to comment
Share on other sites

  • 1 year later...

Attempting to do the same thing.  But am unclear on the implementation.

I looking to place a mesh under the mouse pointer at a z-depth of 40 relative to the camera.  

I have other selectable items beyond that depth, so putting a large plane or sprite and using a ray is not a good option. 

Thanks

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