InsaneHero Posted May 20, 2014 Share Posted May 20, 2014 I ran into this in a little game I'm building, but I've managed to boil it down to a simple example that illustrates the problem (I modified the Arcade Physics "sprite vs sprite" example). sprite2 falls under gravity until it lands on sprite1, then after sitting there for one frame (essential to demo the bug) it gains a steadily increasing upwards force. Expected: sprite2 will take off and fly up to the top of the screen.Actual: sprite2 sticks to sprite1 and tows it up to the top of the screen. If we enable sprite1.immovable, sprite2 will stick to sprite1 (and not move upwards) regardless of the amount of force applied.var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });function preload() { game.load.image('atari', 'assets/sprites/atari130xe.png'); game.load.image('mushroom', 'assets/sprites/mushroom2.png');}var sprite1;var sprite2;var force2 = 0;var takeOff2 = false;var count2 = 0;function create() { game.physics.startSystem(Phaser.Physics.ARCADE); sprite1 = game.add.sprite(300, game.world.height, 'atari'); sprite2 = game.add.sprite(300, game.world.height - 200, 'mushroom'); game.physics.enable([sprite1, sprite2], Phaser.Physics.ARCADE); sprite1.name = 'atari';// sprite1.body.immovable = true; sprite1.body.collideWorldBounds = true; sprite2.name = 'mushroom'; sprite2.body.gravity.y = 100; sprite2.body.collideWorldBounds = true;}function update() { if (takeOff2) force2 -= .5; sprite2.body.velocity.y += force2; game.physics.arcade.collide(sprite1, sprite2, collisionHandler, null, this);}function collisionHandler (obj1, obj2) { // let it sit in contact for one frame before attempting to take off if (count2++ > 0) takeOff2 = true;}function render() { game.debug.text("force " + force2, 0, 50);} Link to comment Share on other sites More sharing options...
rich Posted May 20, 2014 Share Posted May 20, 2014 If you collide first, then apply velocity, it acts as you'd expect (because it's had a chance to resolve the collision and perform separation before velocity is applied)function update() { game.physics.arcade.collide(sprite1, sprite2, collisionHandler, null, this); if (takeOff2) force2 -= .5; sprite2.body.velocity.y += force2;} InsaneHero and Heppell08 2 Link to comment Share on other sites More sharing options...
Heppell08 Posted May 20, 2014 Share Posted May 20, 2014 Generally, its best to have the collision code at the top of the update function so anything afterwards (like in your case and as rich pointed out) can be modded and coded for differnt post collision behaviors. InsaneHero 1 Link to comment Share on other sites More sharing options...
InsaneHero Posted May 20, 2014 Author Share Posted May 20, 2014 Ahh! Right.The reason it's this way around is I'm trying to keep my code OOP so I have a mobile object class, and a static object class (helicopter & buildings in the first instance) and the buildings are processing the collisions against them. I can just juggle the main loop order so the chopper update is after the buildings update, but that is not an obvious requirement so I'll have to jack in a ton of big warning labels in the code comments.Is there a suggested approach for keeping the code solid regardless of function order? NOTE: just tested that change and my chopper can now fly vertically from roof-tops, lots of big warning comments! Link to comment Share on other sites More sharing options...
Recommended Posts