Jump to content

How to fire a bullet from gun's position after rotation ?


Recommended Posts

Hi everyone,


I'm currently making a game with Phaser, and I ran into a problem. Basically I've got a player on top-down view, that can shoot bullets to a target. 

I'm using something like in this tutorial : https://phaser.io/tutorials/coding-tips-007 (create a shoot'em up) to fire a bullet, with a BulletPool, a SingleBullet and a fire() method :

this.getFirstExists(false).fire(sprite.x, sprite.y, sprite.rotation, BulletPool.BULLET_SPEED);

My sprite got an anchor set at (0.5, 0.5) and rotates according to the pointer position. 
Imagine, if I want to fire from gun position, when angle=0, it could be :

this.getFirstExists(false).fire(sprite.x - sprite.width / 2 + 30, sprite.y - sprite.height / 2, sprite.rotation, BulletPool.BULLET_SPEED);

But when I'm rotating the sprite, the gun position changes and its position is no longer "sprite.x - sprite.width / 2 + 30"


Any idea to fix this one ?

Link to comment
Share on other sites

Yes, finally got it with Phaser.Point.rotate

    public fire(sprite: Phaser.Sprite, speed): void {        var point = new Phaser.Point(sprite.x - sprite.width / 2 + 72, sprite.y - sprite.height / 2 - this.height);        point.rotate(sprite.x, sprite.y, sprite.rotation);        this.reset(point.x, point.y);        this.rotation = this.body.rotation = sprite.rotation;        this.body.moveForward(speed);    }

 Thanks !

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...