How do I convert from mouse/touch coords to world space?

0 favourites
  • 6 posts
From the Asset Store
All popular touch mechanics - scrolling, zooming, swiping
  • Basically I have a 3d camera facing down on the world and I've given the camera a 45 degree twist on its perspective. Using the "Move along Layout Axis" I can have a very nice/smooth translation of the touch/mouse coordinates for scrolling the camera/world.

    However, I now want to add some game mechanics. I want to know where in the X and Y of the world I've clicked when I click on the screen. In other words, if I click at any point on the 3D plane in front of me I want to create a tile at that location: translating the touch/mouse x and y to the world space x and y.

    Imagine wherever I click on the screen, a blue tile should be created at that position.

    Any advice?

  • No ideas/suggestions? I feel like a built-in function to calculate this would be very helpful.

  • CanvasToLayerX("2dlayer",LayerToCanvasX("3dlayer", 2dobject.X,2dobject.Y),LayerToCanvasY("3dlayer", 2dobject.X,2dobject.Y)) CanvasToLayerY("2dlayer" LayerToCanvasX("3dlayer", 2dobject.X,2dobject.Y),LayerToCanvasY("3dlayer", 2dobject.X,2dobject.Y))

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hrmm. Why do you think two layers of translation would be needed?

    I tried what you suggested but it didn't work. It also seems like your suggestion would've needed to be calling the functions on a 3dCamera object, not with the system functions. Right? I tried that too but it also didn't work.

    Honestly I feel like the most logical way to do this would just be to use these for the x and the y coordinates of the tile I'm trying to create:

    For x:

    3DCamera.CanvasToLayerX("3dLayer", Touch.X, Touch.Y, 0)

    For y:

    3DCamera.CanvasToLayerY("3dLayer", Touch.X, Touch.Y, 0)

    But unfortunately this doesn't work. If you have more suggestions I'm all ears. Or Ashley might know?

  • The mouse/touch should automatically work with the 3d position, but I don't think it will return the 3d value. The formula with the expressions canvas to layer, and layer to canvas will return the translation for a 2d object.

  • Huh. I never would've expected something so magical to exist and that it would take care of the conversion for me. So I didn't even bother just trying mouse.x and mouse.y as the create coords. But you're right, that works.

    I personally feel that's a bit "too magical" in my mind. I'd personally prefer that touch.x and touch.y or mouse.x and mouse.y were always in screen coordinates and that we had to convert them to the 3D plane when necessary. But thanks for pointing me in the right direction.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)