Jump to content

Prevent pushing other object


kurhlaa
 Share

Recommended Posts

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

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

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

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

  • 1 year later...
  • 1 month later...

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

  • 2 weeks later...

 

21 hours ago, samme 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; 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

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

  • 2 months later...

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.

image.png.25a275f236007398486e76181c835a13.png

(normal behavior. I'm holding left, I can't move any further left)

Flurry of left/rights!

image.png.da59a71345490eb38617e7d2c45c0ac0.png

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

 Share

  • Recently Browsing   0 members

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