Skip to content

Commit 9f40b7f

Browse files
committed
[ts][phaser] Improved physics2 example transforming coordinates between spaces
1 parent d3ceb21 commit 9f40b7f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

spine-ts/spine-phaser/example/physics2.html

+15-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ <h1>Physics example 2 - Drag physics</h1>
3535

3636
let lastX, lastY;
3737
gameObject.on('dragstart', (pointer, dragX, dragY) => {
38-
lastX = gameObject.input.dragStartX;
39-
lastY = gameObject.input.dragStartY;
38+
lastX = dragX;
39+
lastY = dragY;
4040
})
4141

4242
gameObject.on('drag', (pointer, dragX, dragY) => {
43-
gameObject.x += (dragX - lastX);
44-
gameObject.y += (dragY - lastY);
45-
gameObject.skeleton.physicsTranslate((dragX - lastX) / gameObject.scale, (dragY - lastY) / gameObject.scale)
43+
// moving gameObject in its space
44+
gameObject.x += dragX - lastX;
45+
gameObject.y += dragY - lastY;
46+
47+
// converting drag movement to skeleton space
48+
const pointBefore = { x: lastX, y: lastY };
49+
const pointAfter = { x: dragX, y: dragY };
50+
gameObject.phaserWorldCoordinatesToSkeleton(pointBefore);
51+
gameObject.phaserWorldCoordinatesToSkeleton(pointAfter);
52+
53+
// transfer drag effect on physics contraints using physicsTranslate (we won't move the skeleton)
54+
gameObject.skeleton.physicsTranslate(pointAfter.x - pointBefore.x, pointAfter.y - pointBefore.y);
55+
4656
lastX = dragX;
4757
lastY = dragY;
4858
})

0 commit comments

Comments
 (0)