Search in sources :

Example 26 with PhysicsComponent

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

the class RigidBodyPhysicsSample method spawnHorizontal.

private void spawnHorizontal(double x, double y) {
    var physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().density(4.5f).friction(1.0f).restitution(0.05f));
    physics.setBodyType(BodyType.DYNAMIC);
    var t = texture("brick.png").subTexture(new Rectangle2D(0, 0, 64, 5));
    entityBuilder().at(x, y).viewWithBBox(t).with(physics).buildAndAttach();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Rectangle2D(javafx.geometry.Rectangle2D) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 27 with PhysicsComponent

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

the class RobotFactory method newRobot.

@Spawns("robot")
public Entity newRobot(SpawnData data) {
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    bd.setType(BodyType.DYNAMIC);
    PhysicsComponent physics = new PhysicsComponent();
    // friction 0 to avoid sticking to walls
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    physics.setBodyDef(bd);
    physics.addGroundSensor(new HitBox("GROUND_SENSOR", new Point2D(275 / 2 - 3, 260 - 5), BoundingShape.box(6, 10)));
    return entityBuilder(data).from(data).bbox(new HitBox("head", new Point2D(110, 50), BoundingShape.box(70, 70))).bbox(new HitBox("body", new Point2D(110, 120), BoundingShape.box(40, 130))).bbox(new HitBox("legs", new Point2D(275 / 2 - 25, 125 * 2), BoundingShape.box(40, 10))).scaleOrigin(275 / 2, 125 * 2).collidable().with(new StateComponent()).with(physics).with(new RobotComponent()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef) StateComponent(com.almasb.fxgl.entity.state.StateComponent) Spawns(com.almasb.fxgl.entity.Spawns)

Example 28 with PhysicsComponent

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

the class SnookerPhysicsSample method spawnTable.

private Entity spawnTable(double x, double y) {
    var rect = new Rectangle(getAppWidth() - 200 - 200, getAppHeight() - 150 - 150, Color.LIGHTSEAGREEN);
    rect.setArcWidth(15);
    rect.setArcHeight(15);
    rect.setStroke(Color.BLACK);
    rect.setStrokeWidth(10);
    rect.setStrokeType(StrokeType.OUTSIDE);
    return entityBuilder().at(x, y).bbox(new HitBox(new Point2D(-40, 0), box(40, rect.getHeight()))).bbox(new HitBox(new Point2D(rect.getWidth(), 0), box(40, rect.getHeight()))).bbox(new HitBox(new Point2D(0, -40), box(rect.getWidth(), 40))).bbox(new HitBox(new Point2D(0, rect.getHeight()), box(rect.getWidth(), 40))).view(rect).with(new PhysicsComponent()).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 29 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent 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 30 with PhysicsComponent

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

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