From 9f40b7f56b880e8ab164eec5acfbdda0d6ac7dbc Mon Sep 17 00:00:00 2001 From: Davide Tantillo Date: Wed, 17 Apr 2024 10:30:58 +0200 Subject: [PATCH] [ts][phaser] Improved physics2 example transforming coordinates between spaces --- spine-ts/spine-phaser/example/physics2.html | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spine-ts/spine-phaser/example/physics2.html b/spine-ts/spine-phaser/example/physics2.html index ec45be4fc..281c9bfc1 100644 --- a/spine-ts/spine-phaser/example/physics2.html +++ b/spine-ts/spine-phaser/example/physics2.html @@ -35,14 +35,24 @@ let lastX, lastY; gameObject.on('dragstart', (pointer, dragX, dragY) => { - lastX = gameObject.input.dragStartX; - lastY = gameObject.input.dragStartY; + lastX = dragX; + lastY = dragY; }) gameObject.on('drag', (pointer, dragX, dragY) => { - gameObject.x += (dragX - lastX); - gameObject.y += (dragY - lastY); - gameObject.skeleton.physicsTranslate((dragX - lastX) / gameObject.scale, (dragY - lastY) / gameObject.scale) + // moving gameObject in its space + gameObject.x += dragX - lastX; + gameObject.y += dragY - lastY; + + // converting drag movement to skeleton space + const pointBefore = { x: lastX, y: lastY }; + const pointAfter = { x: dragX, y: dragY }; + gameObject.phaserWorldCoordinatesToSkeleton(pointBefore); + gameObject.phaserWorldCoordinatesToSkeleton(pointAfter); + + // transfer drag effect on physics contraints using physicsTranslate (we won't move the skeleton) + gameObject.skeleton.physicsTranslate(pointAfter.x - pointBefore.x, pointAfter.y - pointBefore.y); + lastX = dragX; lastY = dragY; })