denisb Posted January 13, 2016 Share Posted January 13, 2016 I can't figure out why my spacecraft overlaps with the asteroid and does not collide. I used Phaser.Utils.Debug and found that the display objects to not touch each other. Are the bodies in different containers? Thanks for your help. var MyGame={}; MyGame.Level1=function(){ this.spacecraft; this.asteroid; this.cursors; }; MyGame.Level1.prototype={ preload:function(){ this.load.image('spacecraft', 'assets/spacecraft.png'); this.load.image('asteroid', 'assets/asteroid.png'); }, create:function(){ this.game.physics.startSystem(Phaser.Physics.ARCADE); this.asteroid=this.add.sprite(500,100,'asteroid'); this.physics.arcade.enable(this.asteroid,Phaser); this.asteroid.body.immovable=true; this.spacecraft=this.add.sprite(0,100,'spacecraft'); this.game.physics.arcade.enable(this.spacecraft); }, update:function(){ this.physics.arcade.collide(this.spacecraft,this.asteroid,this.collisionHandler,this.processHandler,this); if(this.game.input.keyboard.isDown(Phaser.Keyboard.UP)){ this.spacecraft.body.y-=4; } else if(this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){ this.spacecraft.body.y+=4; } if(this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){ this.spacecraft.body.x-=4; } else if(this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){ this.spacecraft.body.x+=4; } }, processHandler:function(spacecraft,asteroid){ return true; }, collisionHandler:function(spacecraft,asteroid){ spacecraft.kill(); } }; Link to comment Share on other sites More sharing options...
denisb Posted January 13, 2016 Author Share Posted January 13, 2016 It works when I change each axis in update: from this.spacecraft.body.y-=4 to this.spacecraft.body.velocity.y=-200 and so on. Can anyone explain why this works while the other way does not? Link to comment Share on other sites More sharing options...
koko Posted January 13, 2016 Share Posted January 13, 2016 I have a similar problem, does changing collide() to overlap() with the same arguments make it work, cause it does for me? (IDK why however). Link to comment Share on other sites More sharing options...
drhayes Posted January 13, 2016 Share Posted January 13, 2016 Physics only works when one of the bodies has a velocity. If they're not moving then the physics system doesn't know how to separate them on collision. Link to comment Share on other sites More sharing options...
Cudabear Posted January 13, 2016 Share Posted January 13, 2016 drhayes is correct. In order to correctly use the physics system to detect collisions, you need to give things velocity rather than controlling their positions directly with x and y. However, if you do use x and y, the overlap() call still works because this just checks for rectangle overlap on the bodies, and doesn't care about velocity. This can be useful in a few instances, depending on your use case. As a general statement, however, using physics and velocity is your best bet for consistent game logic. Link to comment Share on other sites More sharing options...
koko Posted January 13, 2016 Share Posted January 13, 2016 Oh, that makes sense! Thank you. One more question then. If I have a character carrying something and I want to check for a collision with a table, to see if that something was put ON the table, then how am I supposed to move it? Right now I use a group to 'attach' the object to the hero and I move the whole group which by itself has no body, so I suppose I should rather add the object as a child of hero sprite? Will it inherit parent's velocity so I could detect it's collision with the table or is it basically pointless to check for collision of a child at all? We're talking about Arcade physics of course. Link to comment Share on other sites More sharing options...
Recommended Posts