finding the intern centroid of an object

Godot 4.3 compatibility

Dear programmers, I summon your intellect to aid me on a hard quest! :crossed_swords:

I’m trying to find the internal centroid of a 3D object in Godot.
By “internal centroid,” I mean a point that is located inside the closed volume of the mesh, representing a central or balanced position within the shape.

The classic centroid or average of all vertices often ends up outside the mesh — and for my needs, that just won’t do.

So I’m looking for ideas or algorithms to calculate a point that is always inside the volume, and reasonably close to the true centroid.
It doesn’t have to be 100% mathematically accurate — just something robust that works across various 3D shapes.

If anyone has suggestions, tricks, or even code snippets to share, you’d really get me out of a jam. :pray:

Thanks in advance, o wise ones of the engine!

Sadly I don’t have any code snippet for that as it’s a very specific and complicated problem, but here are some thoughts:

  • You could sample points inside the mesh bounding box, for instance in a XYZ grid, discard the ones that are outside the mesh, and then, from the remaining points, get the one that has the most neighbouring points, in other words the biggest density. That would give you a point that is inside and at a position where the object mass is more important.
    The more points you have, the more precise the algorithm will be, at the cost of performances.

  • Probably the most mathematical approach but I have no idea how to implement that: find the biggest inscribed sphere inside the mesh, and get its center. Same idea as above, but with a perfect result.

  • You could also do some pre-computation by hand or hard-code some positions for each mesh, but that can also be a good way of doing things if implementing a complex algorithm is not something you feel like doing. Since you’re asking for an algorithm, I guess you cannot really do that, but I’m adding that point, just in case.

Anyway, all this to say that this is a complicated question with no easy answer (or at least no answer that I know, but hopefully someone else will come up with something smarter :smile: ), but I hope that can help, if just a bit.

1 Like

Thanks a lot!

Yeah, I’m not the best at coding, but I do have some foundations. What I’m really looking for is brainpower—I need solutions and ways of thinking. That way, I can figure out a doable solution to my problem. I really appreciate your time and the brain cells you spent on this—they’ll definitely be put to good use!

1 Like

Also, do some research elsewhere than in Godot related videos/forums. For instance, this video seems like a working points inside mesh generation in Unity, but you may have a look at the code (I didn’t look at it myself so maybe it’s a terrible implementation, idk).
Of course you’d then have to translate the implementation into Godot, but you get my point.

1 Like

understood thank you

1 Like