Raycaster point threshold. Floating-point precision error

Hello everyone!

Currently, I am working on the project, where I should implement the controller which is dragging items inside the room. The room may contain multiple floors and walls (call them planes). The floors are created using Mesh objects. The geometries for them are being constructed dynamically by the user (user is drawing the floorplan on canvas element, then the floorplan is converted to the scene but nevermind).

I have the problem with the collisions of loaded GLTF items and. I have tried several methods using Box3 and OBB. For the items came up for the best solution with OBB since the item has rotation attribute and the cubes are ok to work with the items.

The challenge I am dealing with right now.
I have raycaster set from camera. When the ray intersects the item and the wall plane I need to move item’s position so the closest point of its obb lay on the wall plane. I have it’s min/max corners, but the problem is that I am trying to do it using another raycasters from the corners in the wall face’s normal direction. Doing so, I have faced the problem with precision error.

You may see the demo here. As you can see in the log the geometry’s vertices all has y value as 0, but when the ray intersects the mesh, sometimes it may be not directly 0. Therefore the distance which I am moving the item may come through the plane which leads to the problem that the ray from the corner will never intersect this plane anymore.

chatGPT suggested to create constant threshold and correct this value and place the needed one. The problem is that I just can’t make it work since it will be really difficult to find that needed point plus the value may be either negative and positive.

I have stuck on this task for a week by now…
Really appreciate any help! :pray:

I’m not sure I get the story with the second raycaster, but if its origin may bypass the target due to calculation precision issues, would it is possible before raycasting to step back and raycast form there? If the step is 1 unit, this would be oversufficient to eliminate numericls fluctutions, which are usually less than 10-10.

Pavel, great thanks for your reply!

The story with the additional raycaster is about projecting point of obb item on the plane and access that point to measure distance. I know another method using dot product but it didn’t work for some reason…

Actually while writing this response I came up to some ideas which I will try to implement and share if I succeed :slight_smile:

About the topic above it is just a weird behaviour which I don’t know how to handle. Moving origin closer or further on specific value in the same direction won’t give anything different since I am logging y parameter of the mesh intersection. Means it should be on the mesh, but because of the precision error it is giving the point which has overpassed the plane or didn’t reach it.

I just think I am doing something wrong and there is another technique to reach the result I am trying to get.