5alidshammout Posted October 2, 2021 Share Posted October 2, 2021 I have a sprite that is rotated, how to move it forward? so if the angle was 0, then it will go 100px to the right and if 90, 100px downwards but how to calculate the x and y pixels that the sprite will move Link to comment Share on other sites More sharing options...
raaaahman Posted November 13, 2021 Share Posted November 13, 2021 You have several ways to go: if you use Arcade Physics, you have the velocityFromAngle() helper function ((example here). if you remember your Math lessons from school, you can apply sinus and cosinus to your angle and multiply your horizontal and vertical velocity with the results. if none of the above and you only move North / East / South or West, you can always use a good ol' switch Link to comment Share on other sites More sharing options...
talhaozdemir Posted November 14, 2021 Share Posted November 14, 2021 function create() { this.obj = this.add.image(400, 300, 'bunny').setScale(0.1); this.rot = 0; this.speedScalar = 1; this.input.on("pointerdown", (pointer) => { this.isDown = true; }); this.input.on("pointerup", (pointer) => { this.isDown = false; }); this.input.on("pointermove", (pointer) => { if (!this.isDown) { return; } this.pointerX = pointer.x; this.pointerY = pointer.y; let rot = Math.atan2( this.obj.y - this.pointerY, this.obj.x - this.pointerX, ); this.rot = rot; this.obj.rotation = rot - Math.PI * 0.5; }); } function update() { if (!this.isDown) { return; } this.obj.x -= Math.cos(this.rot) * this.speedScalar; this.obj.y -= Math.sin(this.rot) * this.speedScalar; } You can use Math.atan2 to find rotation of the object and you can use Math.sin for y positioning and Math.cos for x positioning. You can test the example above in the Labs Link to comment Share on other sites More sharing options...
5alidshammout Posted December 21, 2021 Author Share Posted December 21, 2021 thanks, I've solved it Link to comment Share on other sites More sharing options...
fnaf Posted April 5, 2022 Share Posted April 5, 2022 On 11/14/2021 at 1:54 PM, talhaozdemir said: You can use Math.atan2 to find rotation of the object and you can use Math.sin for y positioning and Math.cos for x positioning. You can test the example above in the Labs five nights at freddy's Thank for your solution. Link to comment Share on other sites More sharing options...
Recommended Posts