From 76146ddeb06ea5300df9370df112826c5698b8d6 Mon Sep 17 00:00:00 2001 From: Almas Baimagambetov Date: Mon, 19 Sep 2022 13:56:29 +0100 Subject: [PATCH] feat: added bindToLookAt3D() which makes a transform look at another transform --- .../fxgl/entity/components/TransformComponent.kt | 16 ++++++++++++++++ .../java/sandbox/test3d/Collision3DSample.java | 1 - .../kotlin/com/almasb/fxgl/app/scene/Camera3D.kt | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fxgl-entity/src/main/kotlin/com/almasb/fxgl/entity/components/TransformComponent.kt b/fxgl-entity/src/main/kotlin/com/almasb/fxgl/entity/components/TransformComponent.kt index f2d58d8e6..3800a06f6 100644 --- a/fxgl-entity/src/main/kotlin/com/almasb/fxgl/entity/components/TransformComponent.kt +++ b/fxgl-entity/src/main/kotlin/com/almasb/fxgl/entity/components/TransformComponent.kt @@ -359,6 +359,22 @@ class TransformComponent(x: Double, y: Double, angle: Double, scaleX: Double, sc updateDirection() } + private var boundLookAt: TransformComponent? = null + + fun bindToLookAt3D(other: TransformComponent) { + boundLookAt = other + } + + fun unbindToLookAt3D() { + boundLookAt = null + } + + override fun onUpdate(tpf: Double) { + boundLookAt?.let { + lookAt(it.position3D) + } + } + /** * Move forward on the XZ plane. * No Y movement. diff --git a/fxgl-samples/src/main/java/sandbox/test3d/Collision3DSample.java b/fxgl-samples/src/main/java/sandbox/test3d/Collision3DSample.java index b5766e38f..820fa4054 100644 --- a/fxgl-samples/src/main/java/sandbox/test3d/Collision3DSample.java +++ b/fxgl-samples/src/main/java/sandbox/test3d/Collision3DSample.java @@ -71,7 +71,6 @@ protected void initGame() { .collidable() .buildAndAttach(); - // TODO: camera follow entity, which smoothly continuously calls "look at" entity? var camera = getGameScene().getCamera3D(); camera.getTransform().xProperty().bind(e.xProperty()); camera.getTransform().yProperty().bind(e.yProperty().subtract(10)); diff --git a/fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Camera3D.kt b/fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Camera3D.kt index ee8436436..ae31ba8dd 100644 --- a/fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Camera3D.kt +++ b/fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Camera3D.kt @@ -56,6 +56,8 @@ class Camera3D { fun update(tpf: Double) { tpfMoveSpeed = tpf * moveSpeed + + transform.onUpdate(tpf) } fun moveForward() {