Upset Baby Games

Upset Baby Games


UBG 3D2D Interaction

Getting Started:

The UBG 3D2D system helps convert data back and forth between 2D and 3D spaces. Features include translating a 3D point into 2D viewport space, translating a 2D point in viewport space into a 3D ray, and numerous shape and mesh interception scripts to allow click on 3D objects.

There are 2 essential structures that you need to consider when using this system:

  1. the translator
  2. the mesh tracker

The translator handles translating point data between spaces. In order to go from one space to another you need to create a translator with ubg_3d2d_create_translator and define your camera / view bounds. Once you have this you can use ubg_3d2d_translation_* and ubg_3d2d_position_* functions by passing in your translator structure. When you perform a translation from 2D to 3D space the function will return a ray structure. This is a position/direction pair which can be used with the mesh tracker to detect collisions in 3D space.

You can either check for collisions against primitives directly or use a meshtracker to detect a collision with a 3D mesh. All collision functions start with ubg_3d2d_ray_to_*. A mesh tracker takes an unfrozen vbuffer and model matrix and then reconstructs the data in a way more kind to collision checking. It also provides the ability to retrieve more specific data about collisions such as multiple intersection points and info about the specific triangles hit. You will need one meshtracker for each mesh which you can create by calling ubg_3d2d_create_meshtracker. Once created you can check for collisions between a ray and either it's OOB or the actual mesh itself triangle-by-triangle. Note that even precise checks will still do an OOB check to significantly speed up miss-checks.

An example initialization of the translator and meshtracker might look something like this:

translator = ubg_3d2d_create_translator(matrix_get(matrix_view), matrix_get(matrix_projection), view_current);
meshtracker = ubg_3d2d_create_meshtracker(matrix_get(matrix_model), model_vbuffer, pr_trianglelist, [vertex_format_color, vertex_format_position_3d]);

If you were to check if there was a precise collision against this mesh then your code would look like this:

ray = ubg_3d2d_position_to_ray(window_mouse_get_x(), window_mouse_get_y(), translator);
is_collision = ubg_3d2d_ray_to_mesh(ray, meshtracker, false);

The code above would convert the mouse coordinates into a 3D ray and then use that ray for a collision against the mesh. The function would return whether or not there was a collision and the specific collision data could then be retrieved from the meshtracker with various Getters

That's all there is to it! If you have any questions or issues that can't be found in this manual please feel free to contact us!
If you like this extension take a look at some of our other offerings!

Folder contents: