use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class PlatformerSample method createPlatforms.
private void createPlatforms() {
entityBuilder().at(0, 500).view(new Rectangle(120, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(120, 0), new Point2D(120, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach();
entityBuilder().at(180, 500).view(new Rectangle(400, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(400, 0), new Point2D(400, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach();
var view = new Group();
var first = new Polygon(0, 0, 200, 0, 250, 100, 0, 30);
first.setTranslateX(-15);
first.setTranslateY(10);
view.getChildren().add(first);
Polygon second = new Polygon(0, -30, 30, -30, 60, 30, 0, 30);
second.setTranslateX(280);
second.setTranslateY(50);
view.getChildren().add(second);
var third = new Rectangle(30, 30);
third.setTranslateX(250);
third.setTranslateY(-30);
view.getChildren().add(third);
poly = entityBuilder().at(180, 350).view(view).bbox(new HitBox("Main", new Point2D(-15, 10), BoundingShape.polygon(new Point2D(0, 0), new Point2D(200, 0), new Point2D(250, 100), new Point2D(0, 30)))).bbox(new HitBox("2nd", new Point2D(250, -30), BoundingShape.box(30, 30))).bbox(new HitBox("3rd", new Point2D(280, 50), BoundingShape.polygon(new Point2D(0, -30), new Point2D(30, -30), new Point2D(60, 30), new Point2D(0, 30)))).with(new PhysicsComponent()).buildAndAttach();
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class RevoluteJointSample method spawnBullet.
private void spawnBullet(double x, double y, double vx, double vy) {
var physics = new PhysicsComponent();
physics.setFixtureDef(new FixtureDef().density(25.5f).restitution(0.5f));
physics.setBodyType(BodyType.DYNAMIC);
physics.setOnPhysicsInitialized(() -> {
physics.setLinearVelocity(vx * 10, vy * 10);
});
entityBuilder().at(x, y).bbox(new HitBox(BoundingShape.circle(450 / 15.0 / 2.0))).view(texture("ball.png", 450 / 15.0, 449 / 15.0)).with(physics).with(new ExpireCleanComponent(Duration.seconds(5)).animateOpacity()).buildAndAttach();
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class RopeJointSample method createPhysicsEntity.
private Entity createPhysicsEntity() {
PhysicsComponent physics = new PhysicsComponent();
physics.setBodyType(BodyType.DYNAMIC);
physics.setFixtureDef(new FixtureDef().density(0.1f).restitution(0.3f));
return entityBuilder().at(getInput().getMousePositionWorld()).with(physics).build();
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class PlatformerSample method initInput.
@Override
protected void initInput() {
Input input = getInput();
input.addAction(new UserAction("Left") {
@Override
protected void onActionBegin() {
player.getComponent(PhysicsComponent.class).setLinearVelocity(new Point2D(-200, 0));
}
}, KeyCode.A);
input.addAction(new UserAction("Right") {
@Override
protected void onActionBegin() {
player.getComponent(PhysicsComponent.class).setLinearVelocity(new Point2D(200, 0));
}
}, KeyCode.D);
input.addAction(new UserAction("Jump") {
@Override
protected void onActionBegin() {
PhysicsComponent physics = player.getComponent(PhysicsComponent.class);
if (physics.isOnGround()) {
double dx = physics.getLinearVelocity().getX();
physics.setLinearVelocity(new Point2D(dx, -100));
}
}
}, KeyCode.W);
input.addAction(new UserAction("Grow") {
@Override
protected void onActionBegin() {
double x = player.getX();
double y = player.getY();
player.removeFromWorld();
player = createPlayer(x, y, 60, 80);
}
}, KeyCode.SPACE);
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class PlatformerSample method createPlayer.
private Entity createPlayer(double x, double y, double width, double height) {
PhysicsComponent physics = new PhysicsComponent();
physics.setGenerateGroundSensor(true);
physics.setBodyType(BodyType.DYNAMIC);
return Entities.builder().at(x, y).viewFromNodeWithBBox(new Rectangle(width, height, Color.BLUE)).with(physics).buildAndAttach(getGameWorld());
}
Aggregations