3
\$\begingroup\$

i'm losing myself in this...

Situation: Working on a game in cocos2d with box2d and I have a ropejoint between one fixed body and one dynamic body.

When I drop the dynamic body is swings from left to right and then from right to left due to the gravity in the world.

The problem: The swings are getting shorter and shorter till finally the dynamic body hangs still beneath the fixed body. This is normal behavior but I need it to keep swinging.

My thoughts: I think I need to apply a tangential force to the ropejoint in the direction of the swinging but how to do this is a mystery for now :)

EDIT 1 The swinging does not have to be in perfect balance, it just can't slowly die.

\$\endgroup\$
2
\$\begingroup\$

Add an impulse to the dynamic body at a regular interval. You can apply it in the direction of movement when the dynamic body is (almost) directly beneath the fixed body. But the impulse should be timed, not based on position.

You might be able to calculate the interval by analyzing your system as an ideal pendulum to determine its frequency...but there's no guarantee that number will be correct in box2d. You should experiment with different values that are roughly the period of oscillation of your swing. If you get close, the deviation from an ideal pendulum will provide some negative feedback to the system. Then you'll get a regular, persistent swing.

\$\endgroup\$
1
\$\begingroup\$

If you have any other bodies that will interact with your body attached to the rope or even the rope itself during your game (eg. a collision mid-swing), then it will be a really hard task to find the forces necessary to restore the swinging motion after collisions have been resolved.

And if you can ensure that there will be no interaction with other dynamic bodies, why use a physics-engine for the swinging animation in the first place? You could just as well make a looping animation?

If I had to build this with Box2D, I'd probably add a "Revolute Joint" at the top of the rope and enable its motor. Then use the motor to apply a rotational force to the rope. My first try would be a sine force, or just enable the motor in specific intervals/impulses. But you would have to experiment with the setup to see which values work best.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.