Search in sources :

Example 6 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.

the class SnookerPhysicsSample method spawnBall.

private Entity spawnBall(double x, double y, Color color) {
    var bd = new BodyDef();
    bd.setBullet(true);
    bd.setType(BodyType.DYNAMIC);
    var p = new PhysicsComponent();
    p.setBodyDef(bd);
    p.setFixtureDef(new FixtureDef().density(BALL_DENSITY).restitution(BALL_ELASTICITY));
    var shadow = new InnerShadow(3, Color.BLACK);
    shadow.setOffsetX(-3);
    shadow.setOffsetY(-3);
    var c = new Circle(15, 15, 15, color);
    c.setEffect(shadow);
    var shine = new Circle(3, 3, 3, Color.color(0.7, 0.7, 0.7, 0.7));
    shine.setTranslateX(5);
    shine.setTranslateY(5);
    return entityBuilder().at(x, y).bbox(new HitBox(BoundingShape.circle(15))).view(c).view(shine).with(p).with(new BallComponent()).buildAndAttach();
}
Also used : Circle(javafx.scene.shape.Circle) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) InnerShadow(javafx.scene.effect.InnerShadow) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 7 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.

the class CollisionFilterSample method initGame.

@Override
protected void initGame() {
    entityBuilder().type(EType.PLAYER).viewWithBBox(new Rectangle(40, 40, Color.BLUE)).view(new Text("PLAYER")).at(100, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
    entityBuilder().type(EType.ENEMY).viewWithBBox(new Rectangle(40, 40, Color.RED)).view(new Text("ENEMY")).at(400, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
    run(() -> {
        CollidableComponent collidable = new CollidableComponent(true);
        CollidableComponent collidable2 = new CollidableComponent(true);
        if (i == 0) {
            debug("No collision with player");
            collidable.addIgnoredType(EType.PLAYER);
            collidable2.addIgnoredType(EType.PLAYER);
        } else if (i == 1) {
            debug("No collision with enemy");
            collidable.addIgnoredType(EType.ENEMY);
            collidable2.addIgnoredType(EType.ENEMY);
        } else if (i == 2) {
            debug("No collision with player and enemy");
            collidable.addIgnoredType(EType.PLAYER);
            collidable.addIgnoredType(EType.ENEMY);
            collidable2.addIgnoredType(EType.PLAYER);
            collidable2.addIgnoredType(EType.ENEMY);
        }
        i++;
        if (i == 3)
            i = 0;
        // a bullet with no physics
        entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 100).with(new ProjectileComponent(new Point2D(1, 0), 100)).with(new OffscreenCleanComponent()).with(collidable).buildAndAttach();
        // a bullet with physics
        PhysicsComponent physics = new PhysicsComponent();
        physics.setBodyType(BodyType.DYNAMIC);
        physics.setOnPhysicsInitialized(() -> physics.setLinearVelocity(100, 0));
        entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 130).with(physics).with(new OffscreenCleanComponent()).with(collidable2).buildAndAttach();
    }, Duration.seconds(4));
}
Also used : OffscreenCleanComponent(com.almasb.fxgl.dsl.components.OffscreenCleanComponent) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.components.CollidableComponent) Rectangle(javafx.scene.shape.Rectangle) Text(javafx.scene.text.Text) ProjectileComponent(com.almasb.fxgl.dsl.components.ProjectileComponent)

Example 8 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.

the class RopeJointSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.LIGHTGRAY);
    entityBuilder().buildScreenBoundsAndAttach(50);
    // platform
    entityBuilder().at(400, 400).viewWithBBox(new Rectangle(500, 20, Color.BROWN)).with(new PhysicsComponent()).buildAndAttach();
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().density(1.1f));
    physics.setBodyType(BodyType.DYNAMIC);
    Entity last = null;
    for (int i = 0; i < 10; i++) {
        PhysicsComponent physics2 = new PhysicsComponent();
        if (last != null) {
            physics2.setBodyType(BodyType.DYNAMIC);
        }
        FixtureDef fd = new FixtureDef();
        fd.setDensity(1.0f);
        physics2.setFixtureDef(fd);
        Entity ball = entityBuilder().at(400 + i * 30, 160).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
        physics2.getBody().setAngularDamping(1f);
        if (last != null) {
            var joint = getPhysicsWorld().addRopeJoint(last, ball);
        }
        last = ball;
    }
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 9 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.addGroundSensor(new HitBox(new Point2D(5, height - 5), BoundingShape.box(width - 10, 10)));
    physics.setBodyType(BodyType.DYNAMIC);
    return entityBuilder().at(x, y).viewWithBBox(new Rectangle(width, height, Color.BLUE)).with(physics).buildAndAttach();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle)

Example 10 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).setVelocityX(-200);
        }
    }, KeyCode.A, VirtualButton.LEFT);
    input.addAction(new UserAction("Right") {

        @Override
        protected void onActionBegin() {
            player.getComponent(PhysicsComponent.class).setVelocityX(200);
        }
    }, KeyCode.D, VirtualButton.RIGHT);
    input.addAction(new UserAction("Jump") {

        @Override
        protected void onActionBegin() {
            PhysicsComponent physics = player.getComponent(PhysicsComponent.class);
            if (physics.isOnGround()) {
                physics.setVelocityY(-500);
            }
        // getDevPane().addDebugPoint(player.getCenter());
        }
    }, KeyCode.W, VirtualButton.A);
    onKeyDown(KeyCode.I, "Info", () -> System.out.println(player.getCenter()));
    input.addAction(new UserAction("Grow") {

        @Override
        protected void onActionBegin() {
            player.setScaleX(player.getScaleX() * 1.25);
            player.setScaleY(player.getScaleY() * 1.25);
        // double x = player.getX();
        // double y = player.getY();
        // 
        // player.removeFromWorld();
        // 
        // player = createPlayer(x, y, 60, 80);
        }
    }, KeyCode.SPACE);
    input.addAction(new UserAction("Grow Poly") {

        @Override
        protected void onActionBegin() {
            poly.setScaleX(poly.getScaleX() * 1.25);
            poly.setScaleY(poly.getScaleY() * 1.25);
        }
    }, KeyCode.G);
}
Also used : UserAction(com.almasb.fxgl.input.UserAction) Input(com.almasb.fxgl.input.Input) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent)

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