How do I stop my character toppling over

Post Reply
Brian Beuken
Posts: 46
Joined: Sat Jul 29, 2017 9:19 pm

How do I stop my character toppling over

Post by Brian Beuken »

I'm doing an FPS style demo, using bullet to move and control collision, as well as providing ray casts for the LOS systems and so on. But I am having a bit of trouble controlling my characters which are cylinder or capsule objects running around an internal maze. They work fine most of the time, but any kind of collision or rapid change in direction, tends to cause them to topple.. I don't want them to topple, I need them to stay upright (on their feet) at all times, is there some quick way obvious to prevent that rather than overriding the angular velocity on X and Z axis?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How do I stop my character toppling over

Post by drleviathan »

You can restrict a RigidBody to rotate only about one of the world's cardinal axes. Here is example code for restricting the rotation to be only about the world y-axis:

Code: Select all

body->setAngularFactor(btVector3(0.0f, 1.0f, 0.0f));
This should work as long as your "world up" direction is always along one of the cardinal axes.
Brian Beuken
Posts: 46
Joined: Sat Jul 29, 2017 9:19 pm

Re: How do I stop my character toppling over

Post by Brian Beuken »

Perfect, thank you, I appreciate the help, I wish there were more example projects based on game usage that would allow me to find out things like this, its proving quite time consuming trying to get thing to work as I expect them with trial and error.


angular motion being particularly hard to control in a simple FPS or 3PS character who just needs to move forward and rotate/turn to change direction. Impulse or torque movement gives motion but the momentum of the rotations just seems to be hard to get right, I've increased friction and damping but all that does is make movement harder. Its that old problem, of do I want realistic motion, or do I want game movement, I was hoping to find good balance, and probably will but its taking a lot of time and effort.

Edit...ah not so perfect, while it does indeed prevent the toppling it has a massive impact on the rotational decay of the character, meaning when I turn him to face a direction using a Torque Impulse he effectively spins as if on skates, even reducing the impulse, his inertia keeps him spinning on the spot... I'll have to think of something else, thanks though!
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How do I stop my character toppling over

Post by drleviathan »

It sounds like you're using a dynamic character. Let me offer a few ideas. Perhaps these will be helpful, perhaps not:

(1) Increase the angular damping?

(2) Rather than use torques to spin your character around try using velocity. That is, compute the difference between its current and desired orientations and compute a velocity that moves it in the right direction. This method sometimes doesn't work well if you want to control something that you also want to be affected by collisions, that is, if you want your character to be knocked out of alignment when hit by something.

Here is a very simple algorithm for spinning in the right direction:

Code: Select all

btScalar turnTimescale = 1.0f; // seconds
btQuaternion deltaRotation = desiredOrientation * inverseCurrentOrientation;
btScalar angle = deltaRotation.angle();
if (angle > minimumCorrectionAngle) {
    btVector3 axis = deltaRotation.axis();
    btVector3 angularVelocity = (-angle / turnTimescale) * axis;
    body->setAngularVelocity(angularVelocity);
} else {
    body->setAngularVelocity(btVector3(0.0f, 0.0f, 0.0f));
}
Call this every frame to provide exponential decay toward desiredOrientation. Tune the timescale to your tastes: long timescales move slow, short timescales go fast. This algorithm starts fast and ends slow: if you want to ease into things then it gets more complicated. A nice thing about this method is that it is stable.

(3) If you don't need your character to be responsive to collisions in dynamic collisions perhaps you should just slam its orientation to exactly what you want. Set the scalarFactor to zero across the board, compute the orientation you want and slam the orientation toward it every frame.
Brian Beuken
Posts: 46
Joined: Sat Jul 29, 2017 9:19 pm

Re: How do I stop my character toppling over

Post by Brian Beuken »

yeah velocity is the route I chose, I reset all angular velocity to 0 and set up rotations of +-1 degree on the y axis to alter direction, I now get perfectly solid movement, and no drifiting, but have gravity and collision detection all working fine. The only loss is bouncing off collisions due to the overrides but I could add a collision responder to provide it, tbh I don't think I will.

I found using angular damping stopped the friction working which was allowing me to decelerate when movement was stopped.
Overall just using velocity to move and altering orientation gives me a solid FPS feel so Im going to keep it that way.

Forces can probably be done well, but need too much balancing, and I just wanted a simple demo to function using Bullet instead of my own collision and gravity systems.

Thanks for the advice though some good tips there I will try in different situations.
Post Reply