0
\$\begingroup\$

I want to damp the velocity of a ball linearly on the x-axis. but when applying linear damping on the ball, the damping also applies in the y-axis which makes it look like its floating.

I tried to refer the Manual but it says: "Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. I generally do not use linear damping because it makes bodies look like they are floating."

So how can I damp the ball in the axis while there is no damping in the y-axis

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

I guess you will have to implement the dampening yourself. something along the lines of:

private static float dampening;

//called every frame
public void dampen(float deltaT){
    Vector2 vel = ball.getBody().getLinearVelocity();
    float newX = vel.x * (1 - delta * dampening);
    vel.x = newX;
    ball.getBody().setLinearVelocity(vel);
}

The comment about Dampening looking floaty refers to the fact, that the velocity never reaches 0 this way. So bodys that are only slowed down by dampening look somewhat like hovercraft.

To avoid that you can reduce the velocity by a fixed amount somewhat like friction.

My example would be better if the dampening was applyed as a force to the body instead of setting the velocity, but the math becomes more complicated.

Here is an example of my custom implementation of friction in my top-down game. It works pretty good so far:

    // Linar Gleitreibung
default void applyLinearFriction(float deltaT) {
    float fGleit = getFriction().getLinearFriction() * getBody().getMass();
    Vector2 curVel = getBody().getLinearVelocity();
    Vector2 wGleit;
    float fMax;
    // apply friction to full velocity
    wGleit = new Vector2(-curVel.x, -curVel.y);
    fMax = curVel.len() * getBody().getMass() / deltaT;

    if (fGleit > fMax) {
        wGleit.setLength(fMax);
    } else {
        wGleit.setLength(fGleit);
    }
    getBody().applyForceToCenter(wGleit, true);
}

sorry im too tired to write that into a clean example right now ;)

\$\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.