简体   繁体   中英

Box2d strange applyLinearImpulse

I have two dynamic body. At each step i try set const velocity for this bodies. Exist three variant how do this:

body.applyLinearImpulse(Helper.getVec(config.getLinearVelocity()*body.getMass(), body.getAngle()), body.getPosition());

or

body.applyForce(Helper.getVec(config.getLinearVelocity()*body.getMass(), body.getAngle()), body.getPosition());

or

body.setLinearVelocity(Helper.getVec(config.getLinearVelocity(), body.getAngle()));

where config.getLinearVelocity() return value V for first body and 1.5*V for second body.

If i call setLinearVelocity on each step, then bodies normally move, each with own speed. But if i call other functions on each step, bodies move at the same speed, at that their velocity is several times higher than if use setLinearVelosity . Why?

You have to call applyLinearImpulse only once, because when you call this function, the mouvement will be decomposed in multiple steps. For example, if you want to make a player jump, you call applyLinearImpulse() on its body only 1 time (when you touch the screen for example) and box2d will increase de velocity, then the gravity force will decrease it.

On the other hand, setLinearVelocity will set the velocity just for the current step of the world

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM