I’m making a top-down game with a simple tilemap for the player to walk around using the keyboard. The player can shoot bullets using a Phaser.Weapon.
There appears to be a difference between the rotation of the bullet bounding box and the bullet texture. When the Weapon is aimed at a particular angle, the resulting bullets appear to be rotated correctly. However, the collision detection says otherwise:
- When shooting horizontal surfaces, the bullet goes into the wall until it is exactly half submerged
- When shooting vertical surfaces, the bullet collides prematurely. This is most noticeable at an angle
After some experimentation, I deduced that the bullet bounding box was fixed as a “wide” rectangle. So when shooting up, the texture appears as a “tall” bullet, but the bounding box is actually rotated at 90 degrees as a “wide” bullet. The rotation of the bounding box appears to be the same regardless of the shooting angle, an idea supported by examining the top and bottom properties of the Bullet, which always reflect the “thin” orientation of the bullet.
This is perhaps best demonstrated by a video. I am using long, slow-moving bullets to make this clearer.
Further details:
- Phaser CE 2.10
- Arcade physics. Body is enabled on player
- Collision detection between player bullets and tiles:
this.game.physics.arcade.collide(
this.player.weapon.bullets,
this.layer,
bullet => bullet.kill()
null,
this
)
- Creation of bullet texture:
const bulletGraphics = game.add.graphics(0, 0)
bulletGraphics.lineStyle(3, 0x000000)
bulletGraphics.moveTo(0, 0)
bulletGraphics.lineTo(bulletLength, 0)
bulletGraphics.endFill()
const bulletTexture = bulletGraphics.generateTexture()
- Creation of bullets inside Weapon subclass constructor:
this.createBullets(numberOfBullets, bulletTexture)
- Aiming at
x, y
with Weapon:this.fireAngle = radToDeg(angleBetween(player.world.x, player.world.y, x, y))
How can I keep the bullet texture and bounding box in sync to avoid these strange collisions?