Search in sources :

Example 1 with PhysicsComponent

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

the class ScifiFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(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(ScifiType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).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 2 with PhysicsComponent

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

the class MarioFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    physics.setGenerateGroundSensor(true);
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(MarioType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).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 3 with PhysicsComponent

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

the class MarioFactory method newGhostPlatform.

@Spawns("ghost_platform")
public Entity newGhostPlatform(SpawnData data) {
    Entity platform = Entities.builder().from(data).type(MarioType.GHOST_PLATFORM).viewFromTexture("ghost_platform.png").bbox(new HitBox("main", BoundingShape.box(data.<Integer>get("width"), data.<Integer>get("height")))).with(new PhysicsComponent(), new CollidableComponent(true)).build();
    platform.getView().setVisible(false);
    return platform;
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent)

Example 4 with PhysicsComponent

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

the class Entities method makeScreenBounds.

/**
 * Create an entity with bounding box around the screen with given thickness.
 *
 * @param thickness thickness of hit boxes around the screen
 * @return entity with screen bounds
 */
public static Entity makeScreenBounds(double thickness) {
    double w = FXGL.getSettings().getWidth();
    double h = FXGL.getSettings().getHeight();
    Entity bounds = new Entity();
    bounds.getBoundingBoxComponent().addHitBox(new HitBox("LEFT", new Point2D(-thickness, 0), BoundingShape.box(thickness, h)));
    bounds.getBoundingBoxComponent().addHitBox(new HitBox("RIGHT", new Point2D(w, 0), BoundingShape.box(thickness, h)));
    bounds.getBoundingBoxComponent().addHitBox(new HitBox("TOP", new Point2D(0, -thickness), BoundingShape.box(w, thickness)));
    bounds.getBoundingBoxComponent().addHitBox(new HitBox("BOT", new Point2D(0, h), BoundingShape.box(w, thickness)));
    bounds.addComponent(new PhysicsComponent());
    return bounds;
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D)

Example 5 with PhysicsComponent

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

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