Search in sources :

Example 21 with PhysicsComponent

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

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

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

the class MarioFactory method newEnemy.

@Spawns("enemy")
public Entity newEnemy(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().type(MarioType.ENEMY).from(data).viewFromNodeWithBBox(new Rectangle(30, 30, Color.RED)).with(physics).with(new EnemyControl()).build();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Rectangle(javafx.scene.shape.Rectangle)

Example 24 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.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().type(MarioType.PLAYER).from(data).bbox(new HitBox(BoundingShape.box(32, 42))).with(physics).with(new CollidableComponent(true)).with(new PlayerControl(), new EffectControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) EffectControl(com.almasb.fxgl.entity.control.EffectControl)

Example 25 with PhysicsComponent

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

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