Pave Posted September 7, 2018 Share Posted September 7, 2018 I am trying to move my Arcade Sprite to a specific point (let's say it's (50|15) and the sprite is currently at (300|500)). Therefore I am using the moveTo() function. The movement of the object works but the direction it is facing is completely wrong. So how can I let my sprite rotate to this point? (with a short transition would be very nice) Link to comment Share on other sites More sharing options...
samme Posted September 7, 2018 Share Posted September 7, 2018 There are Phaser.Math methods to compute the angle between two points or two coordinate pairs. Then you just tween sprite.body.angle. Link to comment Share on other sites More sharing options...
Pave Posted September 7, 2018 Author Share Posted September 7, 2018 Thanks for your reply. Unfortunately I can't exactly find the methods you are mentioning in the docs. https://photonstorm.github.io/phaser3-docs/Phaser.Math.html Can you maybe give me an example? Thanks in advance ? Pave Link to comment Share on other sites More sharing options...
samme Posted September 8, 2018 Share Posted September 8, 2018 https://photonstorm.github.io/phaser3-docs/Phaser.Math.Angle.html Pave 1 Link to comment Share on other sites More sharing options...
Damian Pastorini Posted September 8, 2018 Share Posted September 8, 2018 Hi @Pave, I'm new into Phaser but maybe you can check this example: https://github.com/jackyrusly/jrgame There's a "direction" parameter sent, is that what you are looking for? Best Link to comment Share on other sites More sharing options...
Pave Posted September 8, 2018 Author Share Posted September 8, 2018 Thanks @Damian Pastorini unfortunately this is just a sprite with animations, supporting only the four directions: UP, LEFT, DOWN and RIGHT. In want to be able to rotate my sprite exactly to a point, so I need to use all 360 degree and not only the four directions. Link to comment Share on other sites More sharing options...
Pave Posted September 8, 2018 Author Share Posted September 8, 2018 @samme thanks for the correct link. Now I found the Math function BetweenPoints(). Unfortunately I can't get it to work. Here is part of my code: class Enemy extends Phaser.Physics.Arcade.Sprite { constructor (scene, x,y) { super(scene, x,y); } preUpdate (time, delta) { this.rotateFunc(); } rotateFunc(){ let currentPoint = new Phaser.Geom.Point(this.x,this.y+this.displayHeight/2); let pointToMoveTo = new Phaser.Geom.Point(Player.players.getChildren()[0].x, Player.players.getChildren()[0].y); this.rotation = Phaser.Math.Angle.BetweenPoints(currentPoint, pointToMoveTo); this.arcadePhysics.moveTo(this, Player.players.getChildren()[0].x, Player.players.getChildren()[0].y, 30); } } In this example I want to rotate the enemy sprite to face the player sprite. For the error see the attached video. 2018-09-08 11-48-37_1.mp4 Link to comment Share on other sites More sharing options...
samme Posted September 9, 2018 Share Posted September 9, 2018 Is it that the enemy direction looks wrong? For that you need a texture where the ship is facing rightwards, because that's the direction of rotation=0. Pave 1 Link to comment Share on other sites More sharing options...
Pave Posted September 9, 2018 Author Share Posted September 9, 2018 You were correct! Thanks @samme I could solve the problem by subtracting π/2 from the rotation. Link to comment Share on other sites More sharing options...
lpbr Posted May 27, 2021 Share Posted May 27, 2021 (edited) On 9/8/2018 at 12:05 AM, Damian Pastorini said: Hi @Pave, I'm new into Phaser but maybe you can check this example: https://github.com/jackyrusly/jrgame In fact this is useless as a sample to "find" the direction once the direction is already determined by the control buttons... Edited May 27, 2021 by lpbr Link to comment Share on other sites More sharing options...
Recommended Posts