How do I correctly select a 3D object with the mouse?

Not favoritedFavorited Favorited 0 favourites
  • 2 posts
From the Asset Store
Template for a basketball game with a 3D aspect (illusion of 3D)
  • I am making a game that is partially 3D, with six 2D layouts. In my 3d layout, I have a cube. Each side is supposed to be linked to a 2D layout, so that when you click on a face of the cube, it sends you to the corresponding 2d layout. I cannot figure out how to do this. If I new how to calculate a line from the camera to the mouse, I could cast a sprite in that direction and check for collision; however, I do not know how to do this. Any new ideas and information are very greatly appreciated, for I have searched and searched for a solution and do not have time to search anymore. Thanks in advance for any help given.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, what does construct provide to be able to do this? Maybe:

    System: pick 3dshape overlapping point (mouse.x, mouse.y)

    If the bottom of the 3dshape is on the z=0 plane then that will let you click on the bottom face. The other faces though? you're out of luck with Construct features. You'll have to do something from scratch...

    First you'll need the mouse ray. You can get that from the camera distance to the screen. One way to calculate that is from a non scrolling 2d layer: xyz: (mouse("2d").x-320, 240-mouse("2d").y, 240/tan(fov/2)). 320,240 is half the viewport size. Next you'll have to normalize that vector and rotate it by the inverse of the 3d camera orientation. That will give you a ray direction from the camera position.

    The second part is to do some kind of raycasting to see if and where the ray hits a 3d shape. Using an sdf with raymarching is a relatively simple solution for a single unrotated cube.

    dropbox.com/scl/fi/on24os5ezt8bqkugnea58/raycast_mouse_cube.c3p

    Overall for this kind of problem you figure out how to solve it from scratch and then adapt it to what features construct provides.

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