Jump to content

(Arcade Physics) object A go through object B when pushed by object C


cccc
 Share

Recommended Posts

Hello, I am using Arcade Physics to do a very simple test. But I could not figure out what goes wrong.

 

I have a player, a ground, a ledge, and a block. Everything is enabled with Arcade Physics.

The player can move horizontally and "push" the block down the ledge. Its good.

But when the player "jump on" the block, the block will "go through" the edge and fall down to the ground. Its not what I want.

When the block is on the ground, and the player "jump on" the block. It will also "go  through" the ground. Its not what I want.

The block should be "on the ground", and should NOT "go through the ground".

 

My English is poor, please see the following videos for what I mean.

http://recordit.co/9PT0X6o4Qu

When turn on game.debug.body(), it looks like this 

http://recordit.co/WXEolzfO2G

 

The code is very simple.

player:

this.player = game.add.sprite(x, y, 'dude');
game.physics.arcade.enable(this.player);
this.player.body.gravity.y = 2000;
this.player.body.collideWorldBounds = true;

Ground and ledge:

this.platforms = game.add.group();
this.platforms.enableBody = true;
let ground = this.platforms.create(0, game.world.height - 24, 'ground');
ground.body.immovable = true;
ground = this.platforms.create(400, game.world.height - 24, 'ground');
ground.body.immovable = true;
ground = this.platforms.create(-100, 350, 'ground');
ground.body.immovable = true;

Block

this.block = game.add.sprite(270, 100, 'ground');
game.physics.arcade.enable(this.block);
this.block.body.gravity.y = 2000;

Update()

game.physics.arcade.collide(this.block, this.platforms);
game.physics.arcade.collide(this.block, this.player);
game.physics.arcade.collide(this.player, this.platforms);

 

Please tell me what I did wrong. Thanks you very much.

 

 

 

Link to comment
Share on other sites

I believe that you would have to add a callback to the collide event to check to only allow it to work if it is touching left or right?

Something like

 

game.physics.arcade.collide(this.block, this.platforms);
game.physics.arcade.collide(this.block, this.player, this.allowPush);
game.physics.arcade.collide(this.player, this.platforms);

this.allowPush = function(block, player) {
  return block.body.touching.left || block.body.touching.right
}

 

Link to comment
Share on other sites

You want to lock your player to the box and stop its velocity.y while on the box (the box should also have no velocity.y while block.body.touching.bottom), in the callback function.

Otherwise the player keeps pushing the box which eventually has enough force to push it through other objects.

 

Check out this coding tip, especially the "locking the player" part:

http://www.photonstorm.com/phaser/phaser-coding-tips-4

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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