I’ve had a hunt around but can’t find anything to answer exactly this.
Here, the rigidbody cube is trying to slide around inside a capsule mesh collider, but it’s toppling over when it has hit one of the edges between polys:
I want it to simply keep sliding around perfectly, so when the leading base edge of the cube hits a new incline, it just keeps sliding on up it.
.
The surface is a custom mesh, so could be anything.
I want this behaviour so that Unity’s physics can sort out the cube’s orientation, then I take the normal of the cube’s base (and not the normal of the surfaces it is resting on).
I built this object in, of all things, LittleBigPlanet and, amazingly enough, it seems like the same strategy would work here. (O_O)
Each Update (or whatever):
Move Cube along its forward.
For each 'corner' which must remain grounded:
If (or, dangerously, while) a raycast from (that Corner plus Cube-Up)
to Cube-Down does not hit the current surface
OR
A raycast from Corner to Cube-Down hits the current surface, but only
after an unacceptable distance:
Choose a Pivot point (should be defined per Corner;
using the center of Cube ends up showing some clipping)
Rotate Cube around Pivot towards this Corner
If (while) the raycast from (Corner plus Up)
to (Corner) hits the surface:
Choose a pivot point and rotate *away* from this Corner
This might be very similar to Fattie’s answer; you could use repulsion/attraction forces at the Corners instead of pivot points.
May this could work:
you do not move the cube directly but handle a projector of some kind. Think of the bowl if it was a completely hollow sphere. Place a Gameobject in it’s center of which you controller the rotation. Shoot 4 raycasts onto the innersurface of the bowl where they have the approximate distance of the cubes bottom corners from each other. Maybe by checking a 5th center raycast.
Then get a plane through all the contactpoints and rotate and transform your cube about it. This would make the cube move nearly precise I guess.
Even if that’s not what you had in mind it might help thinking about the problem another way.
A sphere doesn’t topple, but doesn’t give a good representation of the normal that would result from a cube base sliding onto a new surface, like this: http://snag.gy/TM4ai.jpg
But the 4-agents ideas above made me think of combining both. One small sphere at each corner, but instead of using them to apply correctional forces, just use them as colliders. The spheres shouldn’t catch on edges and thus shouldn’t ever cause the cube to topple.
To stop the spheres from rotating as spheres do, set their frictions to zero.