Jump to content

Collisions in arcade physics


NickOver
 Share

Recommended Posts

Hi!

I write a game with top view, player see character from top and can moving them on x and y. But i have problem with collisions. I use ARCADE physics. That mean for moving i use following code:

if (game.input.keyboard.isDown(Phaser.Keyboard.A))
{
    player.x -= speed * game.time.physicsElapsed;
}

if (game.input.keyboard.isDown(Phaser.Keyboard.D))
{
    player.x += speed * game.time.physicsElapsed;
}

if (game.input.keyboard.isDown(Phaser.Keyboard.W))
{
    player.y -= speed * game.time.physicsElapsed;
}

if (game.input.keyboard.isDown(Phaser.Keyboard.S))
{
    player.y += speed * game.time.physicsElapsed;	    
}

If character hit a wall

game.physics.arcade.collide(player, walls);

return true, but i must know before move is player hit a wall after move.

Is someone have a clever solution (or phaser have) excluding function changing player cords, checking collision, and changing cords second time if collision exists?

Thanks for any ideas.

Link to comment
Share on other sites

But if character be near wall collide return true, so i can't move in any direction. So i wanna get collision direction but i have a problem with collide method. In docs (http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.html#collide) they writing about callbacks. So i add:

game.physics.arcade.collide(player, walls, b(), c(), this);

function b(a) {
    console.log('b');
    console.log(a);
}

function c(a) {
    console.log('c');
    console.log(a);
}

for checking the collision direction but all time console.log(a); display undefined. Also i read about sprite.body.touching but all directions always have false.

Link to comment
Share on other sites

33 minutes ago, The_dude8080 said:

You can create a physics body a little bit larger than the actual body you want to collide with. Then you check collision with that body. That body would of course, if I understand your needs, be invisible 

Nope. Character is controlled by player (is sprite with physics). And wall will be a hmm... wall :D everything are visible.

Link to comment
Share on other sites

20 minutes ago, NickOver said:

Also i read about sprite.body.touching but all directions always have false.

You should test `sprite.body.touching` (or `body.blocked`) immediately after calling `arcade.collide`.

Get rid of the callbacks for now.

game.physics.arcade.collide(player, walls, collideCallback, processCallback, this);

if (!player.body.touching.none) {
    // touching something …
}

 

Link to comment
Share on other sites

10 minutes ago, samme said:

You should test `sprite.body.touching` (or `body.blocked`) immediately after calling `arcade.collide`.

Get rid of the callbacks for now.


game.physics.arcade.collide(player, walls, collideCallback, processCallback, this);

if (!player.body.touching.none) {
    // touching something …
}

 

game.physics.arcade.collide(player, walls, collideCallback, processCallback, this);
console.log(player.body.touching);

All time none = true, everything else = false.

Link to comment
Share on other sites

It looks like you're setting the position directly by assigning to x and y. Arcade Physics can really only do its thing if you set the velocity of the physics body rather than moving the sprite directly. My guess is that's why you're seeing the body.touching properties are all false.

Also, in that first code snippet, you did this: "game.physics.arcade.collide(player, walls, b(), c(), this);" Those parts where you're saying "b()" and "c()": you're *calling* the functions and passing the results instead of passing the functions (which is what you want).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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