How do I check if mouse overlaps 3D object? ... or how does overlap work in Construct's 3D anyway?

1 favourites
  • 12 posts
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • Hello,

    is there any way I can detect if my mouse is overlapping a 3D object?

    I want to spawn cubes on a grid, depending on where you click and if there are no other cubes already.

    Next step would be to place cubes on top of cubes, if you click the top face of a cube.

    Does anybody know if there is any way to make this work?

    Have an amazing weekend.

  • Overlapping, collisions, are still only 2d. There is no way to detect object collisions on the z plane. The touch, and mouse plugs will work in 3d with those objects, but again only in 2d. So basically you can interact with the floor.

  • Shame :P but thanks for your very quick answer!!!

  • Here is the closest I’ve come to that. It does a raycast from the mouse to a triangle mesh. The camera has a fixed fov and orientation.

    construct.net/en/forum/construct-3/your-construct-creations-9/3d-raycasting-obj-loader-test-167475

    Basically it’s all from scratch with math as construct doesn’t provide anything that helps. Getting the ray direction from the mouse involves the inverse of the view and camera matrices. Construct doesn’t expose those, maybe you can find them with scripting, or like in that c3p you can make your own from scratch that matches. To simplify the math I went with using a fixed camera orientation.

    Then it just does a Ray vs triangle calculation with a bunch of math. Construct doesn’t provide a way to access the mesh points or list of triangles so again, we have to build that ourselves and replicate the 3D objects with distort meshes.

    With just cubes I think things can be simplified a lot. We’d still need to calculate the Ray direction from the mouse but if we used signed distance fields (sdf) of boxes we could do the actual Ray casting a bit faster.

    Anyways, just thinking aloud here.

  • So here's an example with cubes.

    dropbox.com/s/zd3ur98uvok1523/cube_raycast.c3p

    It calculates the orientation of a 3dcamera that was set with the "look at" action.

    It then does a raycast from the mouse into the screen.

    It then positions a new cube by rounding the 3d position on a grid.

  • Oh god, I haven't looked much deeper than running over your amazing comments and then downloading your file and being utterly impressed. This is oooo cool =O Thank you so much for your help so far. Now I will try and understand how you did it actually :D but had to let go of this huge appreciation.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So here's an example with cubes.

    https://www.dropbox.com/s/zd3ur98uvok1523/cube_raycast.c3p?dl=1

    It calculates the orientation of a 3dcamera that was set with the "look at" action.

    It then does a raycast from the mouse into the screen.

    It then positions a new cube by rounding the 3d position on a grid.

    This stops working when I add camera control as in FPS :(

    How to fix it?

  • I just replicated the “look at” action. If any of the other actions that move or change the orientation of the camera are used then they would need to be replicated too.

    Currently the orientation is a 3x3 matrix stored in three sprites. It can be rotated by multiplying with a rotation matrix that rotates the amount you want to rotate. In practice we’d use some simplification of that.

    Another way that may be simpler would be to only use the “look at” action to orient the camera. Just change the look at target.

    X: camera.x+100*sin(a)

    Y: camera.y+100*cos(a)

    Z: camera.z

  • I just replicated the “look at” action. If any of the other actions that move or change the orientation of the camera are used then they would need to be replicated too.

    Currently the orientation is a 3x3 matrix stored in three sprites. It can be rotated by multiplying with a rotation matrix that rotates the amount you want to rotate. In practice we’d use some simplification of that.

    Another way that may be simpler would be to only use the “look at” action to orient the camera. Just change the look at target.

    X: camera.x+100*sin(a)

    Y: camera.y+100*cos(a)

    Z: camera.z

    can you make an example? I can't do anything :(

  • Here is an update that changes the view from the camera object's position and angle and looks down a bit. Also added some controls to move rotate and strafe.

    There are many ways to set it up so I just picked one and went with it.

    dropbox.com/s/r4g7r0x6ty782zx/cube_raycast2.c3p

  • Here is an update that changes the view from the camera object's position and angle and looks down a bit. Also added some controls to move rotate and strafe.

    There are many ways to set it up so I just picked one and went with it.

    https://www.dropbox.com/s/r4g7r0x6ty782zx/cube_raycast2.c3p?dl=1

    thank you so much for your help! is there any way to control the mouse instead of the keyboard?

  • You could set the camera angle to mouse.x or something. I’m guessing you might have something else in mind but I’m not sure.

    I know there are examples elsewhere on the forum of setting the 3d camera in different ways that could be adapted I suppose. However in this example I’m only supporting setting the camera with the look at action.

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