use of com.almasb.fxgl.core.math.BezierSpline in project FXGL by AlmasB.
the class ViewportSample method cinematicViewportBezier.
private void cinematicViewportBezier() {
// 1. get viewport
Viewport viewport = getGameScene().getViewport();
viewport.setX(-getWidth() / 2);
viewport.setY(-getHeight() / 2);
// 2. define "waypoints"
Vec2[] points = { new Vec2(98, 80), new Vec2(1520, 90), new Vec2(1470, 272), new Vec2(171, 293), new Vec2(154, 485), new Vec2(1500, 483), new Vec2(1474, 683), new Vec2(144, 698), new Vec2(161, 892), new Vec2(1475, 888), new Vec2(1477, 1064), new Vec2(108, 1066) };
BezierSpline spline = FXGLMath.closedBezierSpline(points);
Path path = new Path();
path.getElements().add(new MoveTo(98, 80));
for (BezierSpline.BezierCurve c : spline.getCurves()) {
path.getElements().add(new CubicCurveTo(c.getControl1().x, c.getControl1().y, c.getControl2().x, c.getControl2().y, c.getEnd().x, c.getEnd().y));
}
// if open bezier is needed
path.getElements().remove(path.getElements().size() - 1);
Entity camera = Entities.builder().build();
viewport.xProperty().bind(camera.getPositionComponent().xProperty().subtract(getWidth() / 2));
viewport.yProperty().bind(camera.getPositionComponent().yProperty().subtract(getHeight() / 2));
Entities.animationBuilder().duration(Duration.seconds(24)).translate(camera).buildAndPlay();
}
Aggregations