Search in sources :

Example 6 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class PhysicsPlaygroundSample method spawnBlock.

private void spawnBlock(double x, double y) {
    var p = new PhysicsComponent();
    p.setBodyType(BodyType.DYNAMIC);
    p.setFixtureDef(new FixtureDef().friction(fieldFriction.getFloat()).density(fieldDensity.getFloat()).restitution(fieldRestitution.getFloat()));
    BoundingShape shape;
    Node view;
    switch(cb.getValue()) {
        case BOX:
            shape = BoundingShape.box(40, 40);
            break;
        case CIRCLE:
            shape = BoundingShape.circle(20);
            break;
        case TRIANGLE:
        default:
            shape = BoundingShape.polygon(new Point2D(0, 40), new Point2D(20, 0), new Point2D(40, 40));
            break;
    }
    switch(cb.getValue()) {
        case BOX:
            view = new Rectangle(40, 40, Color.BLUE);
            ((Rectangle) view).setStroke(Color.DARKBLUE);
            break;
        case CIRCLE:
            view = new Circle(20, 20, 20, Color.YELLOW);
            ((Circle) view).setStroke(Color.ORANGE);
            break;
        case TRIANGLE:
        default:
            view = new Polygon(0, 40, 20, 0, 40, 40);
            ((Polygon) view).setFill(Color.RED);
            ((Polygon) view).setStroke(Color.DARKRED);
            break;
    }
    entityBuilder().at(x, y).bbox(shape).view(view).with(p).buildAndAttach();
}
Also used : BoundingShape(com.almasb.fxgl.physics.BoundingShape) Circle(javafx.scene.shape.Circle) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Node(javafx.scene.Node) Rectangle(javafx.scene.shape.Rectangle) Polygon(javafx.scene.shape.Polygon) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 7 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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 8 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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 FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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 10 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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)

Aggregations

PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)19 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)19 HitBox (com.almasb.fxgl.physics.HitBox)11 Point2D (javafx.geometry.Point2D)6 BodyDef (com.almasb.fxgl.physics.box2d.dynamics.BodyDef)5 Rectangle (javafx.scene.shape.Rectangle)4 ExpireCleanComponent (com.almasb.fxgl.dsl.components.ExpireCleanComponent)3 Entity (com.almasb.fxgl.entity.Entity)3 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)3 Circle (javafx.scene.shape.Circle)2 Vec2 (com.almasb.fxgl.core.math.Vec2)1 Spawns (com.almasb.fxgl.entity.Spawns)1 StateComponent (com.almasb.fxgl.entity.state.StateComponent)1 EntityView (com.almasb.fxgl.entity.view.EntityView)1 BoundingShape (com.almasb.fxgl.physics.BoundingShape)1 RevoluteJointDef (com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef)1 Rectangle2D (javafx.geometry.Rectangle2D)1 Node (javafx.scene.Node)1 Button (javafx.scene.control.Button)1 InnerShadow (javafx.scene.effect.InnerShadow)1