Skip to content

Commit

Permalink
[ts][phaser] Improved physics2 example transforming coordinates betwe…
Browse files Browse the repository at this point in the history
…en spaces
  • Loading branch information
davidetan committed Apr 17, 2024
1 parent d3ceb21 commit 9f40b7f
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions spine-ts/spine-phaser/example/physics2.html
Expand Up @@ -35,14 +35,24 @@ <h1>Physics example 2 - Drag physics</h1>

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;
})
Expand Down

0 comments on commit 9f40b7f

Please sign in to comment.