- Home /
How can I slide a cube perfectly over a changing surface?
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).
Is there a non-fudge-it way to do this?
So hang on - how would you define it's base? Presumably the base would rest based on it's support at a number of points - you're going to have to work that out I imagine. Where it's resting...
Oh, I forgot to mention it will be using rigidbodies.
It is resting on the surface as a cube normally would. But unlike reality, I don't want it to topple forward if it hits an incline. Rather, it just keeps sliding smoothly.
A bit hard to describe simple things without images. :/
The mesh is literally just a capsule with the normals turned inside out so I can move around its inside. I made the capsule in Blender; a UV sphere with its middle edge loopjust extruded out into a longer capsule shape.
Is it normal that lots of our posts get cut?
Racing game, ok.
Answer by Loius · Apr 01, 2013 at 05:09 PM
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.
So basically if a corner is airborn, rotate the cube so that it's not.
Not sure how to choose those pivot points; could be 3 corners off the ground. Also, what do you mean by "Cube-Up"? The cube's local height? Likewise with Cube-Down, and in line 12, "Up".
Yep.
Those are local directions. World-Up is Vector3.up, Cube-Up (Up) is cubeTransform.up
I imagine that pivot point would simply be the edge that remains on the ground, or in some cases, the single corner that remains on the ground.
I haven't tried this approach yet though. Just writing that for any readers.
Answer by hexagonius · Apr 01, 2013 at 11:13 AM
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.
note that fortunately if it's an actual sphere, the solution is trivial - the cube just points towards the centre all the time. you can do it instantly in unity with "TrickHolder" as described above, fortunately
Your answer
Follow this Question
Related Questions
Rigidbody Sphere slides around when rotating. 0 Answers
How to calculate the angle of a trajectory to hit the target 1 Answer
Keep Horizontal Momentum after Jump 2 Answers
Collision.impulse = 0 in OnCollisionStay Kinematic Static collision pair after Update 0 Answers
RigidBody2D Movement Stuttering 3 Answers