System lag when using 250000 raycasters

I’m new, I have a problem and want a solution
Currently, I want to place cubes with different shapes on top of each other. The current logic is to drag object A along the Y axis and release it, object A will fall down along the Y axis and stop at the highest point it touches. enter
My current logic is to project a lot of rays (raycaster) from the position of object A shine downwards, and find the shortest distance (intersect) but here there is a problem because the number of mesh below is very large. much (about 500 mesh) and to get the correct height position, I have to draw a very large number of rays (about 500^2 rays), so the system is very laggy. If I reduce the number of rays to a smaller number, the accuracy is not good. is high, so is there any other solution to do this?
I also tried applying three-mesh-bvh but still no improvement.
Thanks



bvh speeds up raycast to some extend https://codesandbox.io/p/sandbox/bvh-txzeq8

though 250.000 rays, there is no way this can run fast. there has to be a better way. perhaps instead of casting hundreds of thousands of rays you should simply be aware of where each object is positioned. the scene should be the outcome of a function of state, and if you know the state model you know each model and therefore its height. the interaction becomes a simple distance limiter using

const highest = Math.max(
  ...objectsUnderneath.map(obj => obj.boundingBox.max.y)
)

if you don’t know where the objects are then imo this is a state management problem first and foremost.

1 Like

@drcmda thank you,
I have found objects that can collide when object A falls. But from the objects finding the highest object, it doesn’t seem right, for example, in the case below, when it falls, it must lie on the lower object.

That makes it harder. Is this something that a generic physics engine like rapier can’t do for you? I’ve made an example for planar drag and drop once, noting falls down on release but that’s just a gravity setting.

https://codesandbox.io/p/sandbox/drag-n-drop-with-physics-and-collisions-9pjzm6

@drcmda
I know it’s really difficult, especially for newbies, so I posted here to find a solution

Update! :grinning: :grinning: :grinning:

I’d suggest one of these:

  • three-bvh-csg, which can tell whether objects intersect
  • a physics engine, which can tell whether objects collide

You wrote you already tried three-mesh-bvh (which is what three-bvh-csg uses), but if you tried it to cast 250k rays, it should be too slow.