kurhlaa Posted November 17, 2018 Share Posted November 17, 2018 Hello, In Arcade physics, if you move towards the other object - after the collision this object moves, so it's like you are pushing it. How to prevent pushing completely? I wish player stops after the collision completely (in collision direction only, X axis for example) and the other object does not move at all. (Player should be able to move in other directions of course). I've tried to manipulate "moves", "immovable", "isMoving", "blocked", "touching" params of both bodies, but no luck. Probably there is a special magic combination of them or other setting I haven't noticed. Link to comment Share on other sites More sharing options...
samme Posted November 17, 2018 Share Posted November 17, 2018 http://labs.phaser.io/edit.html?src=src/physics\arcade\sprite vs immovable.js Set bounce of the moving body to 0 to stop its movement also. Link to comment Share on other sites More sharing options...
kurhlaa Posted November 17, 2018 Author Share Posted November 17, 2018 In my case both objects are able to move in general, not immovable. They can move, they can stop. I wish to disable "pushing". So both objects can move, collide, but can't push/affect each other Link to comment Share on other sites More sharing options...
kurhlaa Posted November 17, 2018 Author Share Posted November 17, 2018 Simple modified version of labs example: var config = { type: Phaser.AUTO, parent: 'phaser-example', backgroundColor: '#0072bc', physics: { default: 'arcade', arcade: { debug: true } }, scene: { preload: preload, create: create, update: update } }; var cursors; var player; var game = new Phaser.Game(config); function preload () { this.load.image('block', 'assets/sprites/block.png'); this.load.image('flectrum', 'assets/sprites/flectrum.png'); } function create () { cursors = this.input.keyboard.createCursorKeys(); wall = this.physics.add.image(200, 300, 'flectrum'); player = this.physics.add.image(400, 300, 'block'); player.setCollideWorldBounds(true); } function update () { player.setVelocity(0); if (cursors.left.isDown) player.setVelocityX(-300); else if (cursors.right.isDown) player.setVelocityX(300); if (cursors.up.isDown) player.setVelocityY(-300); else if (cursors.down.isDown) player.setVelocityY(300); this.physics.world.collide(wall, player, function () { wall.setVelocityX(0); player.setVelocityX(0); console.log('hit?'); }); } if you try to move the box to the wall - box will "push" the wall left-right and so it will move too. How to prevent this? Link to comment Share on other sites More sharing options...
samme Posted November 18, 2018 Share Posted November 18, 2018 immovable bodies can still move. They just can't be moved by collisions. In that example, you need to check player.body.touching.* before setting the player's velocity. Link to comment Share on other sites More sharing options...
kurhlaa Posted November 19, 2018 Author Share Posted November 19, 2018 Can you please check this new example? I'm still doing something wrong, the wall is still moving. I see that during the collision touching.* is sometimes false. I've added collider instead of calling collide() manually and added checking for touch.left & touch.right .. var config = { type: Phaser.AUTO, parent: 'phaser-example', backgroundColor: '#0072bc', physics: { default: 'arcade', arcade: { debug: true } }, scene: { preload: preload, create: create, update: update } }; var cursors; var player; var game = new Phaser.Game(config); function preload () { this.load.image('block', 'assets/sprites/block.png'); this.load.image('flectrum', 'assets/sprites/flectrum.png'); } function create () { cursors = this.input.keyboard.createCursorKeys(); wall = this.physics.add.image(200, 300, 'flectrum'); player = this.physics.add.image(400, 300, 'block'); player.setCollideWorldBounds(true); this.physics.add.collider(wall, player, collideObjects, null, this); } function collideObjects() { wall.setVelocityX(0); player.setVelocityX(0); console.log('hit'); } function update () { player.setVelocity(0); if (cursors.left.isDown && !player.body.touching.left) { console.log(1, wall.body.touching); console.log(2, player.body.touching); player.setVelocityX(-300); } else if (cursors.right.isDown && !player.body.touching.right) { console.log(3, wall.body.touching); console.log(4, player.body.touching); player.setVelocityX(300); } if (cursors.up.isDown) player.setVelocityY(-300); else if (cursors.down.isDown) player.setVelocityY(300); } Link to comment Share on other sites More sharing options...
samme Posted November 19, 2018 Share Posted November 19, 2018 You're right that checking body.touching won't work continuously because after the two bodies are separated during the first step, they are no longer touching on the second step. So the player would still be pushing on every other step: https://codepen.io/samme/pen/NEwGeJ So you have to use immovable on the wall (commented out in linked pen). Link to comment Share on other sites More sharing options...
kurhlaa Posted November 19, 2018 Author Share Posted November 19, 2018 What do I do if I have multiple players, they all can move and collide - but shouldn't be able to push each other? I can't set immovable for everybody, because then collisions do not work. I wish there is a physics switch in Phaser to disable energy exchanging, so when 2 objects collide - the second one doesn't feel that Link to comment Share on other sites More sharing options...
samme Posted November 20, 2018 Share Posted November 20, 2018 It's not actually momentum exchange that's the problem, it's the separation. You need to do some kind of predictive collision check after processing player input. Link to comment Share on other sites More sharing options...
damphlett Posted March 14, 2020 Share Posted March 14, 2020 Did you ever get a satisfactory solution to this working? Link to comment Share on other sites More sharing options...
GamerWael Posted April 21, 2020 Share Posted April 21, 2020 Im still facing this problem.. any solution? Link to comment Share on other sites More sharing options...
Grenagar Posted April 24, 2020 Share Posted April 24, 2020 Hello! Have same problem i think. In my multiplayer game i just need circle players images cant pass throw each other and cant push each other. Like player cant pass world bounds - need same behavior within same group of sprites. May be collider just cant do it? may be use overlap instead? i dont know, cant find any good solution. Link to comment Share on other sites More sharing options...
samme Posted May 7, 2020 Share Posted May 7, 2020 (edited) See example: https://codepen.io/samme/pen/JjYLYEb Edited May 7, 2020 by samme space Link to comment Share on other sites More sharing options...
Andrew C Posted May 8, 2020 Share Posted May 8, 2020 21 hours ago, samme said: See example: https://codepen.io/samme/pen/JjYLYEb I don't think that this solution quite works: the distance it stops the player from the flectrum varies (and if the velocity cancellation happens after they've already intersected, the flectrum will actually move a pixel or two). Of course there's also the minor bug of not letting the player move vertically while in contact horizontally; I don't mean to include that, since that's just annoying extra code to also add ? I haven't tried this, but: I think the most physically sensible way for this to work is to modify the mass of the game object in proportion to its max speed: An object not moving has a mass of 10,000, an object moving at max speed has a mass of 1, and objects between have masses between. As a result, if the player is moving (all out run!) and bumps into an NPC (usually dawdling speed or in the initial example, completely still), then the player's mass will be less than the dawdler's mass, and so will move them very little. If the player is still and an npc bumps into them, the reverse. If both are moving, then the speeds will tend to interfere, but it should feel appropriate. Then use friction to dampen the remaining forces away, perhaps. I'm aware this isn't exactly what the initial request contained, but it feels like it might be an alternate solution to your problem to me. Link to comment Share on other sites More sharing options...
samme Posted May 14, 2020 Share Posted May 14, 2020 On 5/8/2020 at 11:42 AM, Andrew C said: I don't think that this solution quite works: the distance it stops the player from the flectrum varies (and if the velocity cancellation happens after they've already intersected, the flectrum will actually move a pixel or two). Of course there's also the minor bug of not letting the player move vertically while in contact horizontally; But none of that happens in https://codepen.io/samme/pen/JjYLYEb. Link to comment Share on other sites More sharing options...
Andrew C Posted August 5, 2020 Share Posted August 5, 2020 I observed all of the first paragraph happening on the example https://codepen.io/samme/pen/JjYLYEb (from a surface pro 5th gen/m3 on chrome back in May). My guess is that it breaks down on cases where line 93's `delta.copy(player.body.velocity).scale(1 / this.physics.world.fps)` doesn't match the actual update frequency; unfortunately, the device I'm on right now is much beefier than that computer was, and so I think it's actually able to keep up with the very low demands the experiment put on it. IT might also be a bug in physics? Or something else? I definitely haven't been able to repro moving the flectrum, though I did see it from the previous device. Even on my current beefier device, I'm able to forcefully reproduce the stop-distance-changes-thing by placing the cube adjacent to the flectrum and VERY RAPIDLY alternating left and right movement. Eventually it gets wedged one predictive step away; leaving and coming back towards the flectrum retains the offset. (normal behavior. I'm holding left, I can't move any further left) Flurry of left/rights! I can't get any closer to the flectrum, even if I go away and come back. (PS: I know I'm very late. Sorry! ) Link to comment Share on other sites More sharing options...
Recommended Posts