Jump to content

Sprites Collide and cause a nudging effect


Recommended Posts

Hi Guys,


I'm really enjoying Phaser and getting great results.


I have a question about collisions:


When two sprites hit each other, the sprites nudge each other, knock them back a little. This would normally be fine, but it is causing the sprite to slowly move in another direction because of the nudge. Is there a way to turn that off the nudging effect so that they collide without any force?




Link to comment
Share on other sites

    function update() {        game.physics.collide(sprite1, sprite2, collisionHandler, null, this);    }    function collisionHandler (obj1, obj2) {        obj1.body.velocity.x = 0;        obj2.body.velocity.x = 0;    }

just set the velocity to 0




Works perfectly :-)


Thanks 4ucai!!!

Link to comment
Share on other sites

Just to add that if you don't want a sprite to move when it's collided with, just do this:

sprite.body.immovable = true;

(you can toggle that boolean at any point in your game too)


And just to clarify - an immovable true sprite can still be adjusted via velocity / acceleration like normal, it just doesn't receive any impact velocity.


If you want a sprite to NEVER move, including when setting velocity on it, you'd use:

sprite.body.moves = false;
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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