I have an object pinned against the ground by a revolute joint. A motor on the joint makes the object spin. Sometimes the object becomes stuck pushing against a static body. When it happens the object stops rotating and it sleeps.

Yet, if a different object collides with the spinning one, sometimes (not always) it gets pushed away, like the figure illustrates:

enter image description here

This feels very unrealistic: the blue object shouldn't get kicked away, since the red one is still.

Is it possible to avoid this?

I believe that this happens because the red piece has some angular velocity set at the beginning of a simulation step. This angular velocity gets removed when Box2D resolves the collision between the red and the black piece. However if the collision between blue and red is resolved when red still has angular velocity, then it gets kicked away. How can I avoid this?

  • \$\begingroup\$ Could you turn off the motor once you detect that it is stuck? \$\endgroup\$
    – Vaillancourt
    Sep 8 '21 at 15:59
  • \$\begingroup\$ @Vaillancourt: I wouldn't know how to check if the object gets unstuck at any point. And if I turn off the motor, the red piece is likely to spin backwards if something touches it (or even just due to gravity). \$\endgroup\$ Sep 8 '21 at 16:54
  • \$\begingroup\$ I'm not super familiar with how box2d does things, but you could detect that the red piece is in contact with a static object and that its actual angular velocity is zero, then set the force of the motor "high" to stay at a zero velocity. Then that would "turn it off" but "with strength". How do you let the red piece move again? If it's the end of the road for it, you might as well transform it into a static body? \$\endgroup\$
    – Vaillancourt
    Sep 8 '21 at 17:04
  • \$\begingroup\$ @Vaillancourt good idea. The red object may resume moving, because in my simulation, pieces like the black one may be added and removed dynamically. I guess I could monitor whether red and black are touching, and remove the motor's limit when the contact ends. This may cause a "lag" in red's behavior by up to one physical frame, but it doesn't sound like the end of the world. I will have to test it. \$\endgroup\$ Sep 9 '21 at 3:22
  • \$\begingroup\$ Coming here with no experience on box2d, can you try adding the black object earlier to the scene than any blue ones? Maybe internally box2d handles collisions in order. In this works, consider teleporting the black pieces into a stati scenery item, instead of adding/removing them dynmically \$\endgroup\$
    – Ferrybig
    Sep 9 '21 at 9:52

You must log in to answer this question.

Browse other questions tagged .