From c47c2c818082813229a11942044a996d3d433ad9 Mon Sep 17 00:00:00 2001 From: Dono <3781087+Donorhan@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:05:28 +0200 Subject: [PATCH] 1 - inputs: fix global coordinates + avoid vec2 memory allocation --- core/client/user-manager.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/client/user-manager.js b/core/client/user-manager.js index 33164e2a..71c4061e 100644 --- a/core/client/user-manager.js +++ b/core/client/user-manager.js @@ -241,14 +241,17 @@ userManager = { // when the mouse left click is down, we want to apply steering to the current direction of the character if (input.activePointer.isDown && allowPhaserMouseInputs()) { - // we compute the current direction vector between the mouse and our character on the screen - const directionVector = (new Phaser.Math.Vector2(input.activePointer.x, input.activePointer.y)) - .subtract(new Phaser.Math.Vector2(this.controlledCharacter.x, this.controlledCharacter.y)) - .normalize(); - - // we steer the character in the direction of the mouse, so it can be used along with other controls (keyboard etc.) - this.inputVector.x += directionVector.x; - this.inputVector.y += directionVector.y; + const worldPoint = input.activePointer.positionToCamera(this.scene.cameras.main); + const directionVector = [worldPoint.x - this.controlledCharacter.x, worldPoint.y - this.controlledCharacter.y]; + const directionLength = Math.sqrt(Math.abs(directionVector[0] * directionVector[0] + directionVector[1] * directionVector[1])); + + if (Math.abs(directionLength) > 2) { + const len = 1 / Math.sqrt(directionLength); + + // we steer the character in the direction of the mouse, so it can be used along with other controls (keyboard etc.) + this.inputVector.x += directionVector[0] * len; + this.inputVector.y += directionVector[1] * len; + } } const moving = this.inputVector.x !== 0 || this.inputVector.y !== 0;