3
\$\begingroup\$

I'm using LibGDX to implement rubber rope in which player can fly and launch. I know I could use Box2D as physics engine and I'm going to one day, but I'd like to learn it myself for now.

I've already got it working, but some unexpected movements are sometimes noticeable and I don't understand why. I feel like I haven't thought of something important. So I figured you guys might find a flaw in my implementation.

For starters, it looks like this: image

My maths is basically taken straight from the physics class. There are two forces applied to the bird, because there are two fractions of the rope. Their sum is the actual force.

Both forces are obviously laid along the corresponding rope fractions. Magnitude of the force is |F| = kx, where k is some constant and x is how much this fraction had to stretch. I cache its initial length at the beginning of collision, then measure it each frame and calculate the difference. The code:

private void calculateForces() {
    //A and B are blue circles in the picture; M and Z are red

    //Negative number means no extension, hence Math.max call
    float modifierA = Math.max(0, A.dst(M) - mLengthA);
    float modifierB = Math.max(0, B.dst(Z) - mLengthB);

    mForceA.set(A.x - M.x, A.y - M.y).setLength(K*modifierA);
    mForceB.set(B.x - Z.x, B.y - Z.y).setLength(K*modifierB);
}

What am I not taking into consideration?

EDIT:

As requested, I made a video. You can see some weird movement at the end of second swing, and the third one is completely wrong.

I also tried getting rid of Math.max() call changing line to float modifierA = A.dst(M) - mLengthA; and, suprisingly, it looks much better. Video. The question is why. Isn't it logical for the rope not to apply any force when it's not being stretched?

\$\endgroup\$
  • \$\begingroup\$ +1 for I know I could use Box2D as physics engine and I'm going to one day, but I'd like to learn it myself for now. and actually having something already done. But I think you should try to describe unexpected movements part better. Or make video screen capture of this behaviour and post link to yt. \$\endgroup\$ – Mars Aug 12 '16 at 9:48
  • 1
    \$\begingroup\$ When you say "unexpected movements", what do you mean? How does the problem manifest itself? \$\endgroup\$ – bornander Aug 12 '16 at 9:50
  • \$\begingroup\$ What happens if you start stretching the rubber, then move the bird to the side ? Does it slide along, or are M and Z fixed until it detaches ? \$\endgroup\$ – Quentin Aug 12 '16 at 13:43
  • \$\begingroup\$ 1) Earth's G? 2) Length L0 after which the band begins to extend (= no force if dist is shorter)? 3) @Quentin 's question above. Helps if you describe the problem... \$\endgroup\$ – Stormwind Aug 12 '16 at 18:37
  • \$\begingroup\$ @Mars @bornander I edited original question and added some videos. @Quentin it just goes to the side, nothing special. M and Z are contact points and they get recalculated each frame. @Stormwind there's no gravity. I'm not sure if I understand second question. mLengthA and mLengthB are initial lengths and if AM < mLengthA, there's no force from that segment of the band. \$\endgroup\$ – Maciej Dziuban Aug 13 '16 at 19:30

Your Answer

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

Browse other questions tagged or ask your own question.