Search in sources :

Example 1 with ParticleGroupDef

use of com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef in project FXGL by AlmasB.

the class PhysicsWorld method createPhysicsParticles.

private void createPhysicsParticles(Entity e) {
    double x = e.getX();
    double y = e.getY();
    double width = e.getWidth();
    double height = e.getHeight();
    ParticleGroupDef def = e.getComponent(PhysicsParticleComponent.class).getDefinition();
    def.setPosition(toMetersF(x + width / 2), toMetersF(appHeight - (y + height / 2)));
    Shape shape = null;
    BoundingBoxComponent bbox = e.getBoundingBoxComponent();
    if (!bbox.hitBoxesProperty().isEmpty()) {
        if (bbox.hitBoxesProperty().get(0).getShape().type == ShapeType.POLYGON) {
            PolygonShape rectShape = new PolygonShape();
            rectShape.setAsBox(toMetersF(width / 2), toMetersF(height / 2));
            shape = rectShape;
        } else if (bbox.hitBoxesProperty().get(0).getShape().type == ShapeType.CIRCLE) {
            CircleShape circleShape = new CircleShape();
            circleShape.setRadius(toMetersF(width / 2));
            shape = circleShape;
        } else {
            log.warning("Unknown hit box shape: " + bbox.hitBoxesProperty().get(0).getShape().type);
            throw new UnsupportedOperationException();
        }
    }
    if (shape == null) {
        PolygonShape rectShape = new PolygonShape();
        rectShape.setAsBox(toMetersF(width / 2), toMetersF(height / 2));
        shape = rectShape;
    }
    def.setShape(shape);
    ParticleGroup particleGroup = jboxWorld.createParticleGroup(def);
    Color color = e.getComponent(PhysicsParticleComponent.class).getColor();
    e.addControl(new PhysicsParticleControl(particleGroup, color, this));
}
Also used : ParticleGroupDef(com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef) BoundingBoxComponent(com.almasb.fxgl.entity.component.BoundingBoxComponent) ParticleGroup(com.almasb.fxgl.physics.box2d.particle.ParticleGroup) Color(javafx.scene.paint.Color)

Example 2 with ParticleGroupDef

use of com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef in project FXGL by AlmasB.

the class PhysicsParticlesSample method initCloth.

private void initCloth() {
    // 1. define how particles should behave
    ParticleGroupDef groupDef = new ParticleGroupDef();
    groupDef.setTypes(EnumSet.of(ParticleType.ELASTIC));
    // 2. create component and set data
    PhysicsParticleComponent ppComponent = new PhysicsParticleComponent();
    ppComponent.setDefinition(groupDef);
    ppComponent.setColor(Color.DARKGREEN.brighter());
    // 3. create entity, place it and specify volume of particles via bounding box
    Entity cloth = new Entity();
    cloth.getPositionComponent().setValue(150, 10);
    cloth.getBoundingBoxComponent().addHitBox(new HitBox("MAIN", BoundingShape.box(75, 150)));
    // 4. add component
    cloth.addComponent(ppComponent);
    getGameWorld().addEntity(cloth);
}
Also used : ParticleGroupDef(com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef) PhysicsParticleComponent(com.almasb.fxgl.physics.PhysicsParticleComponent) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox)

Example 3 with ParticleGroupDef

use of com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef in project FXGL by AlmasB.

the class PhysicsParticlesSample method initLiquid.

private void initLiquid() {
    ParticleGroupDef groupDef = new ParticleGroupDef();
    groupDef.setTypes(EnumSet.of(ParticleType.VISCOUS, ParticleType.TENSILE));
    PhysicsParticleComponent ppComponent = new PhysicsParticleComponent();
    ppComponent.setDefinition(groupDef);
    ppComponent.setColor(Color.BLUE.brighter());
    Entity liquid = new Entity();
    liquid.getPositionComponent().setValue(300, 10);
    liquid.getBoundingBoxComponent().addHitBox(new HitBox("MAIN", BoundingShape.circle(35)));
    liquid.addComponent(ppComponent);
    getGameWorld().addEntities(liquid);
}
Also used : ParticleGroupDef(com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef) PhysicsParticleComponent(com.almasb.fxgl.physics.PhysicsParticleComponent) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox)

Example 4 with ParticleGroupDef

use of com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef in project FXGL by AlmasB.

the class ScifiSample method initLiquid.

private void initLiquid() {
    ParticleGroupDef groupDef = new ParticleGroupDef();
    groupDef.setTypes(EnumSet.of(ParticleType.WATER));
    PhysicsParticleComponent ppComponent = new PhysicsParticleComponent();
    ppComponent.setDefinition(groupDef);
    ppComponent.setColor(Color.BLUE.brighter());
    Entity liquid = new Entity();
    liquid.setPosition(playerControl.getEntity().getComponent(PositionComponent.class).getValue().subtract(0, 650));
    liquid.getBoundingBoxComponent().addHitBox(new HitBox("MAIN", BoundingShape.circle(55)));
    liquid.addComponent(ppComponent);
    getGameWorld().addEntities(liquid);
}
Also used : ParticleGroupDef(com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef) PhysicsParticleComponent(com.almasb.fxgl.physics.PhysicsParticleComponent) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PositionComponent(com.almasb.fxgl.entity.component.PositionComponent)

Aggregations

ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)4 Entity (com.almasb.fxgl.entity.Entity)3 HitBox (com.almasb.fxgl.physics.HitBox)3 PhysicsParticleComponent (com.almasb.fxgl.physics.PhysicsParticleComponent)3 BoundingBoxComponent (com.almasb.fxgl.entity.component.BoundingBoxComponent)1 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)1 ParticleGroup (com.almasb.fxgl.physics.box2d.particle.ParticleGroup)1 Color (javafx.scene.paint.Color)1