Generate Custom Mesh Collider dynamically + options for Rigid body

Hello Needle,

I would like to use the current physic engine of Needle for my project, I need to move tooth, and I want to manage collision with an offset between collision.
I would like to had a mesh collider but I got an error (surely because my mesh doesn’t have one), so how could I generate this custom mesh collider by myself ?
Additionally, I tried tests with BoxCollider but when I move a mesh, the collision make it go far away and I only want that my mesh move to a position where there is no collision, I don’t want a speed movement added to my mesh.

  • I forgot how to see physics for debugging, I tried debugphysics in my url but it doesn’t work

Does somebody has an idea ?

Original Post on Discord

by user 224464722878005248

Hey :wave: this seems like a lot is happening at once, could you by any chance make a video?


I forgot how to see physics for debugging, I tried debugphysics in my url but it doesn’t work
showcolliders - to visualize colliders, possibly if you feel the scene is too bussy, you could try wireframe as well.
debugcollisions - extra logs
debugcolliderplacement - i think this freezes every collider inplace.
debugphysics - extra logs


Also, worth noting, Needle uses Rapier under the hood:

You dont have to generate a custom mesh. Either assign one in unity to the meshcollider component or not - if none is assigned the mesh from the renderer will be used. Do you perhaps have a multimaterial object? And please include exact error messages (“i get an error” does not help)

For debugging use ?showcolliders

Here the code I use :

const rigidBodyRight = GameObject.addNewComponent(this.rightTooth, Rigidbody)
        rigidBodyRight.useGravity = false
        const box = GameObject.addNewComponent(this.rightTooth, BoxCollider)

Here the video :

by user 224464722878005248


by user 224464722878005248

I will regenerate the error with the mesh collider by replacing BoxCollider by MeshCollider

by user 224464722878005248

Here the error with MeshCollider :

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
    at q (rapier_wasm3d.js:133:28)
    at _.trimesh (rapier_wasm3d.js:3830:22)
    at oI.intoRaw (shape.ts:817:25)
    at MI.createCollider (collider_set.ts:65:35)
    at sI.createCollider (world.ts:342:31)
    at _RapierPhysics.createCollider (engine_physics_rapier.ts:653:32)
    at _RapierPhysics.addMeshCollider (engine_physics_rapier.ts:583:30)
    at MeshCollider.onEnable (Collider.ts:93:42)
    at MeshCollider.__internalEnable (Component.ts:499:14)
    at set enabled [as enabled] (Component.ts:554:18)

by user 224464722878005248

In fact, I’d like to point out that the tooth models I load are not very common because the mesh vertex locations are not in the centre of the object.
This explains why the mesh collider is offset from the vertex.
The explosion in question comes from the fact that the boxes are all in the same place at the start.

by user 224464722878005248

Unfortunately I can’t replace them.
Hence the fact that I need a mesh collider so that it can be positioned on the vertices and not on the origin of the mesh position (which is always 0 0 0).

by user 224464722878005248

I think you are able to add colliders to the teeth in the editor, no?

I presume every tooth is an individual object, so it should be possible to add the collider there in the Editor to overcome the origin being at 0,0,0.

As Marcel said, You should be able to add a MeshCollider to whatever object you have a MeshRenderer on.

I can’t do it from the Editor, I load it from another server by .obj

by user 224464722878005248

Ah, i see :slight_smile:

The MeshCollider doesn’t have any offset that you can leverage.

But, Rigidbodies work in a way that the colldier can be on the same object as the rigidbody OR it can be on a children.

So in your case, you should be able to create a new GameObject, parent it under the tooth and add the necessary offset and components.

You can set the mesh collider like so meshCollider.sharedMesh = xyz
and you can obtain it from a MeshRenderer by meshRenderer.sharedMesh
I presume every tooth is a unique mesh, otherwise you could run into issues due to the meshes being reused.

Hmm, yes ok but, How could add the MeshCollider ? (I don’t understand the error below)

In your example xyz is the mesh that I want to be influence by the collider ?

by user 224464722878005248

So, the error is likely caused by the mesh not being specified on the MeshCollider.

MeshCollider gets the mesh data and creates collision objects based on that data.
MeshRenderer gets the mesh data and renders that mesh data.

So, not exactly influencing, but yes, the MeshCollider will get to the shape of the mesh.