Jump to content

Unexpected collision: problem with resetting body position?


Recommended Posts

Hi all. I'm very new to Phaser, so this may be a really stupid question, sorry if so. Phaser version 1.1.3.


In a game I have a missile sprite that sometimes unexpectedly kills an enemy where it shouldn't, even before the missile itself appears on the screen. In my firing function I put 

missile.reset(newx, newy);

Then I set up its `body` velocity (tweening from 0 to max value, if that may happen important). I'm checking collision with `game.physics.overlap` and handle the situation in a callback.


So, most of the time missile starts normally from where I expect it to appear and moves and collides correctly. On a rare occasion, which I'm not sure I understood, it just instantly kills an enemy somewhere. I discovered, that in this case its velocity is still zero, so it actually appears where the enemy is and collides with it. I tried putting

console.log(missile.x, missile.y, missile.body.x, missile.body.y);

just after the reset line, and to my surprise `body` position is different from just position. Body position basically refers to where the missile disappeared previous time.

So my guess is: `body` position will align to world position as soon as missile updates, but sometimes collision check happens before that, and if on missile previous position there is an enemy to collide with, collision happens.

Any ideas?

And why body position is not reset in the first place? Judging by source code it should.


PS. No way to put inline code on this forum?

Link to comment
Share on other sites

Ok, here's the "game" - code narrowed down to show the issue.

function testColl() {    var game = new Phaser.Game(400, 300, Phaser.CANVAS, '', {preload: preload, create: create, update: update});        function preload() {        game.stage.backgroundColor = '#c0c0c0';        game.load.image('missile', 'missile.png');        game.load.image('bomb', 'bomb.png');    }        function create() {        missile = game.add.sprite(0, 0, 'missile');        missile.visible = false;        missile.outOfBoundsKill = true;                bomb = game.add.sprite(370, 140, 'bomb');        bomb.body.velocity.x = -50;                game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(fire);    }        function update() {        game.physics.overlap(missile, bomb, hit);    }        function fire() {        missile.reset(0, 150);        missile.body.velocity.x = 100;    }        function hit(_missile, _bomb) {        console.log("Enemy hit at " + _bomb.x);        _bomb.kill();        _bomb.reset(370, 140);        _bomb.body.velocity.x = -50;                _missile.kill();    }}

I may provide two sprites if you need, but you may just use something relevant (my missile is 20x6, my bomb is 30x30, doesn't really matter as long as they'll overlap).

How to test:

1. Bomb will spawn at the right edge and will move to the left.

2. Press space to fire a missile from the left edge to the right at the same y-level.

3. When they meet, both will disappear. Note the place where bomb was when the hit happened! You may keep the mouse arrow above that place, for example.

4. Another bomb will spawn and start moving immediately. Press space again when the bomb is exactly where it was destroyed last time.

5. PROFIT... err... I mean, PROBLEM! Bomb will be destroyed immediately, missile won't even appear (or the eye can't catch it, so fast it's removed).

Link to comment
Share on other sites


  • Recently Browsing   0 members

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