use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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();
}
use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef 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.box2d.dynamics.FixtureDef 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.box2d.dynamics.FixtureDef in project FXGL by AlmasB.
the class RevoluteJointSample 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();
}
use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.
the class RopeJointSample method createPhysicsEntity.
private Entity createPhysicsEntity() {
PhysicsComponent physics = new PhysicsComponent();
physics.setBodyType(BodyType.DYNAMIC);
physics.setFixtureDef(new FixtureDef().density(0.1f).restitution(0.3f));
return entityBuilder().at(getInput().getMousePositionWorld()).with(physics).build();
}
Aggregations