use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class SnookerPhysicsSample method spawnBall.
private Entity spawnBall(double x, double y, Color color) {
var bd = new BodyDef();
bd.setBullet(true);
bd.setType(BodyType.DYNAMIC);
var p = new PhysicsComponent();
p.setBodyDef(bd);
p.setFixtureDef(new FixtureDef().density(BALL_DENSITY).restitution(BALL_ELASTICITY));
var shadow = new InnerShadow(3, Color.BLACK);
shadow.setOffsetX(-3);
shadow.setOffsetY(-3);
var c = new Circle(15, 15, 15, color);
c.setEffect(shadow);
var shine = new Circle(3, 3, 3, Color.color(0.7, 0.7, 0.7, 0.7));
shine.setTranslateX(5);
shine.setTranslateY(5);
return entityBuilder().at(x, y).bbox(new HitBox(BoundingShape.circle(15))).view(c).view(shine).with(p).with(new BallComponent()).buildAndAttach();
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class CollisionFilterSample method initGame.
@Override
protected void initGame() {
entityBuilder().type(EType.PLAYER).viewWithBBox(new Rectangle(40, 40, Color.BLUE)).view(new Text("PLAYER")).at(100, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
entityBuilder().type(EType.ENEMY).viewWithBBox(new Rectangle(40, 40, Color.RED)).view(new Text("ENEMY")).at(400, 100).with(new CollidableComponent(true), new PhysicsComponent()).buildAndAttach();
run(() -> {
CollidableComponent collidable = new CollidableComponent(true);
CollidableComponent collidable2 = new CollidableComponent(true);
if (i == 0) {
debug("No collision with player");
collidable.addIgnoredType(EType.PLAYER);
collidable2.addIgnoredType(EType.PLAYER);
} else if (i == 1) {
debug("No collision with enemy");
collidable.addIgnoredType(EType.ENEMY);
collidable2.addIgnoredType(EType.ENEMY);
} else if (i == 2) {
debug("No collision with player and enemy");
collidable.addIgnoredType(EType.PLAYER);
collidable.addIgnoredType(EType.ENEMY);
collidable2.addIgnoredType(EType.PLAYER);
collidable2.addIgnoredType(EType.ENEMY);
}
i++;
if (i == 3)
i = 0;
// a bullet with no physics
entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 100).with(new ProjectileComponent(new Point2D(1, 0), 100)).with(new OffscreenCleanComponent()).with(collidable).buildAndAttach();
// a bullet with physics
PhysicsComponent physics = new PhysicsComponent();
physics.setBodyType(BodyType.DYNAMIC);
physics.setOnPhysicsInitialized(() -> physics.setLinearVelocity(100, 0));
entityBuilder().type(EType.BULLET).viewWithBBox(new Rectangle(20, 10, Color.BLACK)).at(0, 130).with(physics).with(new OffscreenCleanComponent()).with(collidable2).buildAndAttach();
}, Duration.seconds(4));
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class RopeJointSample method initGame.
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.LIGHTGRAY);
entityBuilder().buildScreenBoundsAndAttach(50);
// platform
entityBuilder().at(400, 400).viewWithBBox(new Rectangle(500, 20, Color.BROWN)).with(new PhysicsComponent()).buildAndAttach();
PhysicsComponent physics = new PhysicsComponent();
physics.setFixtureDef(new FixtureDef().density(1.1f));
physics.setBodyType(BodyType.DYNAMIC);
Entity last = null;
for (int i = 0; i < 10; i++) {
PhysicsComponent physics2 = new PhysicsComponent();
if (last != null) {
physics2.setBodyType(BodyType.DYNAMIC);
}
FixtureDef fd = new FixtureDef();
fd.setDensity(1.0f);
physics2.setFixtureDef(fd);
Entity ball = entityBuilder().at(400 + i * 30, 160).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
physics2.getBody().setAngularDamping(1f);
if (last != null) {
var joint = getPhysicsWorld().addRopeJoint(last, ball);
}
last = ball;
}
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class PlatformerSample method createPlayer.
private Entity createPlayer(double x, double y, double width, double height) {
PhysicsComponent physics = new PhysicsComponent();
physics.addGroundSensor(new HitBox(new Point2D(5, height - 5), BoundingShape.box(width - 10, 10)));
physics.setBodyType(BodyType.DYNAMIC);
return entityBuilder().at(x, y).viewWithBBox(new Rectangle(width, height, Color.BLUE)).with(physics).buildAndAttach();
}
use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.
the class PlatformerSample method initInput.
@Override
protected void initInput() {
Input input = getInput();
input.addAction(new UserAction("Left") {
@Override
protected void onActionBegin() {
player.getComponent(PhysicsComponent.class).setVelocityX(-200);
}
}, KeyCode.A, VirtualButton.LEFT);
input.addAction(new UserAction("Right") {
@Override
protected void onActionBegin() {
player.getComponent(PhysicsComponent.class).setVelocityX(200);
}
}, KeyCode.D, VirtualButton.RIGHT);
input.addAction(new UserAction("Jump") {
@Override
protected void onActionBegin() {
PhysicsComponent physics = player.getComponent(PhysicsComponent.class);
if (physics.isOnGround()) {
physics.setVelocityY(-500);
}
// getDevPane().addDebugPoint(player.getCenter());
}
}, KeyCode.W, VirtualButton.A);
onKeyDown(KeyCode.I, "Info", () -> System.out.println(player.getCenter()));
input.addAction(new UserAction("Grow") {
@Override
protected void onActionBegin() {
player.setScaleX(player.getScaleX() * 1.25);
player.setScaleY(player.getScaleY() * 1.25);
// double x = player.getX();
// double y = player.getY();
//
// player.removeFromWorld();
//
// player = createPlayer(x, y, 60, 80);
}
}, KeyCode.SPACE);
input.addAction(new UserAction("Grow Poly") {
@Override
protected void onActionBegin() {
poly.setScaleX(poly.getScaleX() * 1.25);
poly.setScaleY(poly.getScaleY() * 1.25);
}
}, KeyCode.G);
}
Aggregations