- Home /
Rigidbody stuck in corners of Mesh Collider
My plan is to have a sort of vertical "tilt the board to get the marble to the bottom" game. The conditions so far are:
I have a single mesh collider that doesn't move and a Rigidbody sphere collider.
The only thing I ever change is the direction of gravity (which rotates based on mouse position).
Also, this is restricted to a XY-plane (2D game).
Mostly, things are smooth, and the physics are great. But if you tilt the board and get the sphere in a corner for too long, it'll be stuck there, no matter where the gravity is rotated afterwards. It doesn't get stuck if the sphere stays in the corner for only a brief moment, though.
My first guess would be to do some kind of "sphere is in the wall" check, but I don't know how I would approach applying the correct force to get it out. As of right now, I do not apply any forces manually on the sphere; PhysX influences the sphere's velocity by itself.
The rotate gravity script is as follows:
private var baseGravity : Vector3 = Vector3(0.0,-48.0,0.0);
private var mousePos : Vector2;
private var rot : float;
private var rotSpeed : float = 250.0;
function Update ()
{
// Get mouse position in screen in range of -1.0/1.0
mousePos = Vector2(Mathf.Clamp(((Input.mousePosition.x / Screen.width) * 2.0) - 1.0,-1.0,1.0),
Mathf.Clamp(((Input.mousePosition.y / Screen.height) * 2.0) - 1.0,-1.0,1.0));
// Rotate rot by mouse's x
rot += mousePos.x*rotSpeed*Time.deltaTime;
if (rot > 360.0)
rot -= 360.0;
else if (rot < 0.0)
rot += 360.0;
// Rotate baseGravity and change physics
var v = Quaternion.AngleAxis(rot,-Vector3.forward) * baseGravity;
Physics.gravity = Vector3(v.x,v.y,0.0);
}
I've tried thus far:
Checking/unchecking the "Smooth Sphere Collisions" in the mesh collider.
Using standard primitive colliders in replacement of the mesh collider.
Every combination of interpolation/collision detection mode on the sphere's rigidbody.
Changing the scale of the objects in question (bigger/smaller), gravity speed (less/greater than -9.81), and increasing the speed of physics timesteps
Rotating the mesh collider instead of gravity, which leads to a lot more problems than simply rotating gravity (and doesn't actually solve the problem in question).
...amongst other things I probably can't remember. Any ideas would be greatly appreciated!
Did you check if the ball's rigidBody is sleeping when that happens? rb.IsSleeping()?
If that's the case, making it unable to sleep may solve this: rb.sleepAngularVelocity = 0f; rb.sleepVelocity = 0f;
Thank you so much! I didn't think to check if it were sleeping; now, it works!
gabs, you should probably post your comment as an answer. ;P
Your answer
Follow this Question
Related Questions
Camera gets flung off of the map when the player collides with certain objects. 0 Answers
Ignore sub-colliders for center of mass in Rigidbody2d? 1 Answer
Returning list of Triangles, Vertices or Points by raycasting through a mesh (iPhone) 1 Answer
Physics Possibilities?: Angles, Penetration, Exceptions 4 Answers
ball's collider sometimes catches an edge and bounces. when ball rolls over two aligned platforms 1 Answer