[ts][phaser] Improved physics2 example transforming coordinates between spaces

This commit is contained in:
Davide Tantillo 2024-04-17 10:30:58 +02:00
parent d3ceb219f8
commit 9f40b7f56b
No known key found for this signature in database
GPG Key ID: 473ABA42CC0FDDC6

View File

@ -35,14 +35,24 @@
let lastX, lastY; let lastX, lastY;
gameObject.on('dragstart', (pointer, dragX, dragY) => { gameObject.on('dragstart', (pointer, dragX, dragY) => {
lastX = gameObject.input.dragStartX; lastX = dragX;
lastY = gameObject.input.dragStartY; lastY = dragY;
}) })
gameObject.on('drag', (pointer, dragX, dragY) => { gameObject.on('drag', (pointer, dragX, dragY) => {
gameObject.x += (dragX - lastX); // moving gameObject in its space
gameObject.y += (dragY - lastY); gameObject.x += dragX - lastX;
gameObject.skeleton.physicsTranslate((dragX - lastX) / gameObject.scale, (dragY - lastY) / gameObject.scale) 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; lastX = dragX;
lastY = dragY; lastY = dragY;
}) })