Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raycast to detect Softbody
#1
Hello Jose,

I'm wondering how I can perform a Raycast to detect if it is colliding with a Softbody.

Perhaps I can create a MeshCollider component and update it to match the current deformation of the Softbody's mesh and detect if the Raycast is colliding with that. Not sure how to get the current deformed mesh of the Softbody if this is even possible/the best option.

Thank you!
Reply
#2
(20-08-2020, 10:31 PM)maceb Wrote: Hello Jose,

I'm wondering how I can perform a Raycast to detect if it is colliding with a Softbody.

Perhaps I can create a MeshCollider component and update it to match the current deformation of the Softbody's mesh and detect if the Raycast is colliding with that. Not sure how to get the current deformed mesh of the Softbody if this is even possible/the best option.

Thank you!

Hi there!

There's many options.

You described one of them: The deformed mesh is done using a SkinnedMeshRenderer, so you can use its BakeMesh() method to obtain the deformed mesh. Then, raycast against it. To raycast, you have two options: A) generate a MeshCollider and raycast against it B) iterate over all triangles yourself performing a ray/triangle intersection test for each one, and keep the closest.

Option A) is easier, and probably best if you plan on reusing the same mesh for a few frames at least, or if you need to do many raycasts per frame. Generating a MeshCollider from a mesh is somewhat expensive as Unity has to generate a internal spatial partitioning structure to speed up raycast queries, so you should amortize the cost of generating it over as many raycasts as possible. Option B) is better if you don't want to get MeshColliders involved, or if you only plan to raycast when the user clicks (a single raycast per frame, only some frames). This is how tools like Polybrush work, for instance.

Another option is to iterate over all particles, and determine the closest one to the ray. This is what ObiParticlePicker/Dragger does to allow you to click and drag softbodies around. The downside of this is that poor particle coverage can mean the ray misses the object entirely, so "thickening" the ray is a must.

cheers,
Reply