google / liquidfun

2D physics engine for games
http://google.github.io/liquidfun
4.68k stars 641 forks source link

In the case of free fall movement, the change value of moving distance in the same time interval cannot be greater than 2, that is to say, it will change from free fall movement to uniform linear movement #105

Open oxDesigner opened 4 years ago

oxDesigner commented 4 years ago

In the case of free fall movement, the change value of moving distance in the same time interval cannot be greater than 2, that is to say, it will change from free fall movement to uniform linear movement

I want to know how to modify it

code:

const bodyDef = new b2BodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(0, 4); this.body = world.CreateBody(bodyDef);

const dynamicBox = new b2PolygonShape(); dynamicBox.SetAsBoxXY(0.1, 0.1); var lastX = this.body.GetPosition().y; setInterval(() => { var X = this.body.GetPosition().y;

console.log(X - lastX);
lastX = X;

}, 1000 / 60) const fixtureDef = new b2FixtureDef; fixtureDef.shape = dynamicBox; fixtureDef.density = 1; fixtureDef.friction = 0.2;

this.body.CreateFixtureFromDef(fixtureDef);

ljluestc commented 6 months ago

const bodyDef = new b2BodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0, 4);
this.body = world.CreateBody(bodyDef);

const dynamicBox = new b2PolygonShape();
dynamicBox.SetAsBoxXY(0.1, 0.1);

const fixtureDef = new b2FixtureDef;
fixtureDef.shape = dynamicBox;
fixtureDef.density = 1;
fixtureDef.friction = 0.2;
this.body.CreateFixtureFromDef(fixtureDef);

const maxFallSpeed = 2.0; // Maximum downward velocity (adjust as needed)

setInterval(() => {
    const velocity = this.body.GetLinearVelocity();
    if (velocity.y < -maxFallSpeed) {
        // Limit the downward velocity to the maximum
        velocity.y = -maxFallSpeed;
        this.body.SetLinearVelocity(velocity);
    }
}, 1000 / 60);