private static void input() {
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
switch (Keyboard.getEventKey()) {
case Keyboard.KEY_C:
Vec2 bodyPosition = new Vec2(Mouse.getX(), Mouse.getY()).mul(0.5f).mul(1 / 30f);
BodyDef boxDef = new BodyDef();
boxDef.position.set(bodyPosition);
boxDef.type = BodyType.DYNAMIC;
PolygonShape boxShape = new PolygonShape();
boxShape.setAsBox(0.75f, 0.75f);
Body box = world.createBody(boxDef);
FixtureDef boxFixture = new FixtureDef();
boxFixture.density = 0.1f;
boxFixture.shape = boxShape;
box.createFixture(boxFixture);
bodies.add(box);
break;
}
}
}
for (Body body : bodies) {
if (body.getType() == BodyType.DYNAMIC) {
if (Keyboard.isKeyDown(Keyboard.KEY_A) && !Keyboard.isKeyDown(Keyboard.KEY_D)) {
body.applyAngularImpulse(+0.01f);
} else if (Keyboard.isKeyDown(Keyboard.KEY_D) && !Keyboard.isKeyDown(Keyboard.KEY_A)) {
body.applyAngularImpulse(-0.01f);
}
if (Mouse.isButtonDown(0)) {
Vec2 mousePosition = new Vec2(Mouse.getX(), Mouse.getY()).mul(0.5f).mul(1 / 30f);
Vec2 bodyPosition = body.getPosition();
Vec2 force = mousePosition.sub(bodyPosition);
body.applyForce(force, body.getPosition());
}
}
}
}