Non-convex (torus) collision shapes for VisionOS/RealityKit

I have tried entity.generateCollisionShapes (generating simple box-shaped collision) and ShapeResource.generateConvex(from: entity) (generating convex-shaped collision, as the name suggests). Unfortunately neither suits my case, where I have a torus entity inside which no collision should happen with other entities - namely, smaller entities should be able to "fall through" the torus, thus the title of this post.

Was wondering if there's any solution that I overlooked. Thanks 🙏

Hello @allenfrostline,

You can actually use ShapeResource.generateConvex(from: [SIMD3<Float>]) to achieve this by passing in the vertices of each face of the torus mesh. This approach means you will generate a single shape resource for each face which you can add to your model as an array of shapes.

This works not only for torus but pretty much any mesh, but beware that shapes can be resource heavy.

Convex cooking requires at least four vertices, so (assuming a triangle mesh) you have to add an extra vertex position for each face before using the function above. That extra position needs to lie outside the plane of the face as the convex cooking fails if the vertices are coplanar. I am using the negative normal direction of the face to simply add an extra position just inside the mesh, but you might find a better approach for your use case.

The convex cooking might also fail if you pass vertices that are very(!) close together, so if you run into this error I would add some checks to skip or otherwise handle faces that have a significantly small surface area. I don’t remember the exact resolution for this, so you will have to experiment with this yourself.

Non-convex (torus) collision shapes for VisionOS/RealityKit
 
 
Q