Search in sources :

Example 11 with FixtureDef

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

the class JointSample method createPhysicsEntity.

private Entity createPhysicsEntity() {
    // 1. create and configure physics component
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(0.7f);
    fd.setRestitution(0.3f);
    physics.setFixtureDef(fd);
    return Entities.builder().at(getInput().getMousePositionWorld()).with(physics).build();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 12 with FixtureDef

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

the class MarioFactory method newRobot.

@Spawns("robot")
public Entity newRobot(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(MarioType.ROBOT).bbox(new HitBox("main", new Point2D(275 / 2 - 105 / 2, 275 / 2 - 210 / 2), BoundingShape.box(105, 210))).bbox(new HitBox("lower", new Point2D(275 / 2 - 15, 125 * 2), BoundingShape.box(30, 10))).with(physics, new CollidableComponent(true)).with(new RobotControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 13 with FixtureDef

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

the class RigidBodyPhysicsSample 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 14 with FixtureDef

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

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

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