I need insect-like agents that have fallen/rolled on their back/side to naturally re-orient themselves. The agents are modeled as simple cuboids in the physics engine (Bullet) to approximate an ant-like shape.

At the moment I correct the orientation by instantly altering it when the dot product of surface 'up' and agent 'up' is less than some number (e.g. -0.5). Thus the agents can fall over only so much before they are insta-righted in the blink of an eye.

Obviously this looks nasty. Plus in the border cases, where the dot product isn't quite low enough, they can still fall over and don't get self-righted.

So, I need a way to have the critters automatically roll back onto their feet. How can I do this? The way I see it there are two problems:

1) How to determine the best rotation axis (e.g. better to roll on their back than head-to-tail)?

2) How to work out the magnitude of torque required to overcome the physics engine, whilst not being too much that has them flipping end over end?

[Full disclosure: I originally posted this question on gamedev.net, to no response. I also posted in StackOverflow, but deleted the post when this stackexchange was recommended instead]

  • \$\begingroup\$ Sorry, maybe not the most helpful... but it seems like what would be intuitive would be if the critters rocked back & forth to right themselves. Or if you had a static animation sequence showing them going from on-their-backs to upright. But maybe I'm missing your point entirely :? \$\endgroup\$
    – Mordachai
    Commented Dec 8, 2010 at 16:21
  • \$\begingroup\$ Rocking back and forth is an option, I hadn't really considered that, and would look most natural and be more realistic. However, I'm not sure of the math to do this. Animation isn't really an option, as this is mainly a simulation rather than game per se, and interaction with other objects would be possible during the self-righting procedure. \$\endgroup\$
    – Dave
    Commented Dec 8, 2010 at 16:24
  • \$\begingroup\$ I'm sure in the past I've seen pseudo-physics in racing sims that will roll a car back onto its wheels in a natural looking way. This is what I've had in mind trying to solve this problem \$\endgroup\$
    – Dave
    Commented Dec 8, 2010 at 16:26
  • 2
    \$\begingroup\$ With the rocking, when angular velocity is below some limit and not right-side-up yet, apply an increasing force to rock back. Should get right-side-up without flipping in a couple tries. \$\endgroup\$
    – Zan Lynx
    Commented Dec 8, 2010 at 21:36

2 Answers 2


My feeling is it would be easier to fake it than do it with full physics. If your bugs can be animated, give them an animation of leaping up in the air, rotating, then landing.

You know that sooner or later any physics based method is going to be (heh) buggy. Bug on a slope, object in its way, etc.

  • \$\begingroup\$ I would really rather avoid animation. This is much closer to a simulation project in this respect. I take your point about scenarios where physics based solutions could fail, though. I really like Mordachai's suggestion of rocking back and forth. Plus, like I said, I'm sure there are arcade racing sims with pseudo-physics methods of self-righting cars that have rolled..? \$\endgroup\$
    – Dave
    Commented Dec 8, 2010 at 17:24
  • 2
    \$\begingroup\$ Could they just repeatedly try until they get it right? I'm thinking of Click Beetles (see en.wikipedia.org/wiki/Click_beetle) and what they do. They basically keep flipping themselves up in the air until they land upright. It's not terribly graceful but it gets the job done. \$\endgroup\$
    – Tim Holt
    Commented Dec 8, 2010 at 18:11
  • \$\begingroup\$ Reading about those click beetles made me chuckle. Unfortunately I'm focused on ants/termites here, which afaik can't jump :) \$\endgroup\$
    – Dave
    Commented Dec 8, 2010 at 19:52

You should do some kind of rotation impulse.

Probably sideways is the easiest to flip over, so have it either flip over to the right or left, but choose randomly each time.

As for figuring out how much power is needed for the impulse, just test it out. Start it low and keep bumping it till it works correctly. Then figure out the min and max power to flip over correctly. Randomly select one of those when you need to flip.

To make it a bit more realistic, set a small timer between when the object knows its upside down, wait, and when when its time to flip, check if it is still upside down, if so, then flip.

  • \$\begingroup\$ Problem is, it might not be on its back - could be lying on its side, or even, in rare cases, its head/tail. So problem #1 remains, how to determine the best rotation direction? I figure some cross-producting will be involved, but I'm not sure. Problem #2 - determining the magnitude: you don't think it's feasible to analytically calculate the size of the impulse required, then? Best just to start small and keep increasing until correct orientation is achieved? \$\endgroup\$
    – Dave
    Commented Dec 8, 2010 at 19:49
  • \$\begingroup\$ No, it is feasible to calculate the impulse required, but that might make the simulation less fun, as bugs would always flip over correctly instead of sometimes messing up. And besides, I am not familiar with the physics engine you use, so wouldn't know where to start with that. \$\endgroup\$ Commented Dec 8, 2010 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .