This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Moving Physics Body
Pawel Pastuszak edited this page Dec 7, 2018
·
1 revision
Box2d physics body can be obtained from PhysicsBody
. Then you can use standard box2d methods to set velocity, apply forces, impulses etc. If Entity has VisSprite
, it will be automatically updated by VisRuntime, this behavior can be disabled in Scene Config.
Example of moving physics body:
public class PlayerSystem extends BaseSystem implements AfterSceneInit {
//assigned by artemis
ComponentMapper<VisSprite> spriteCm;
ComponentMapper<PhysicsBody> physicsCm;
VisIDManager idManager;
VisSprite sprite;
Body body;
@Override
public void afterSceneInit() {
Entity player = idManager.get("player");
sprite = spriteCm.get(player);
body = physicsCm.get(player).body;
}
@Override
protected void processSystem() {
float x = body.getLinearVelocity().x;
float y = body.getLinearVelocity().y;
float desiredVel = 0;
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
desiredVel = -20;
sprite.setFlip(true, false);
} else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
desiredVel = 20;
sprite.setFlip(false, false);
}
if (Gdx.input.isKeyJustPressed(Keys.UP)) {
float impulse = body.getMass() * 200;
body.applyForce(0, impulse, body.getWorldCenter().x, body.getWorldCenter().y, true);
}
float velChange = desiredVel - x;
float impulse = body.getMass() * velChange;
body.applyForce(impulse, 0, body.getWorldCenter().x, body.getWorldCenter().y, true);
}
}