Search in sources :

Example 16 with FixtureDef

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

the class FlyingKeysPhysicsSample method addButtonEntity.

private void addButtonEntity(double x, double y, char c) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    physics.setFixtureDef(new FixtureDef().density(0.7f).restitution(0.3f));
    Button button = new Button(c + "");
    button.setFont(Font.font(18));
    button.setPrefWidth(BUTTON_WIDTH);
    button.setPrefHeight(BUTTON_HEIGHT);
    entityBuilder().at(x, y).bbox(BoundingShape.box(BUTTON_WIDTH, BUTTON_HEIGHT)).view(button).with(physics).buildAndAttach();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Button(javafx.scene.control.Button) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 17 with FixtureDef

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

the class RopeJointSample 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 18 with FixtureDef

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

the class RevoluteJointSample 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 block = entityBuilder().at(600, 100).viewWithBBox(new Rectangle(80, 50)).with(physics).buildAndAttach();
    PhysicsComponent physics2 = new PhysicsComponent();
    physics2.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(1.0f);
    physics2.setFixtureDef(fd);
    Entity ball1 = entityBuilder().at(600, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
    PhysicsComponent physics3 = new PhysicsComponent();
    physics3.setBodyType(BodyType.DYNAMIC);
    physics3.setFixtureDef(fd);
    Entity ball2 = entityBuilder().at(700, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics3).buildAndAttach();
    physics2.getBody().setAngularDamping(1f);
    physics3.getBody().setAngularDamping(1f);
    getPhysicsWorld().addRevoluteJoint(block, ball1, new Point2D(80, 50), new Point2D(15, 15));
    getPhysicsWorld().addRevoluteJoint(block, ball2, new Point2D(0, 50), new Point2D(15, 15));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 19 with FixtureDef

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

the class RevoluteJointSample 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