Search in sources :

Example 11 with PhysicsComponent

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();
}
Also used : Group(javafx.scene.Group) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) Polygon(javafx.scene.shape.Polygon)

Example 12 with PhysicsComponent

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();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) ExpireCleanComponent(com.almasb.fxgl.dsl.components.ExpireCleanComponent) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 13 with PhysicsComponent

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();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 14 with PhysicsComponent

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);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D)

Example 15 with PhysicsComponent

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());
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Rectangle(javafx.scene.shape.Rectangle)

Aggregations

PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)32 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)19 HitBox (com.almasb.fxgl.physics.HitBox)18 Point2D (javafx.geometry.Point2D)13 Rectangle (javafx.scene.shape.Rectangle)12 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)5 BodyDef (com.almasb.fxgl.physics.box2d.dynamics.BodyDef)5 Entity (com.almasb.fxgl.entity.Entity)4 ExpireCleanComponent (com.almasb.fxgl.dsl.components.ExpireCleanComponent)3 EntityView (com.almasb.fxgl.entity.view.EntityView)2 Input (com.almasb.fxgl.input.Input)2 UserAction (com.almasb.fxgl.input.UserAction)2 Circle (javafx.scene.shape.Circle)2 Polygon (javafx.scene.shape.Polygon)2 Vec2 (com.almasb.fxgl.core.math.Vec2)1 OffscreenCleanComponent (com.almasb.fxgl.dsl.components.OffscreenCleanComponent)1 ProjectileComponent (com.almasb.fxgl.dsl.components.ProjectileComponent)1 Spawns (com.almasb.fxgl.entity.Spawns)1 CollidableComponent (com.almasb.fxgl.entity.components.CollidableComponent)1 EffectControl (com.almasb.fxgl.entity.control.EffectControl)1