Matter-js: Prevent bouncing on collision with static bodies

Created on 18 Dec 2016  路  3Comments  路  Source: liabru/matter-js

I'm creating a tile-based game with Matter and I've noticed that no matter how I seem to configure my player's body, I can't stop it from bouncing when it collides with a static wall, even with restitution = 0.

Here's a reproduction (use arrow keys)

question

Most helpful comment

Hey, it looks like the problem you are experiencing is because you are trying to move the player by directly changing it's position, as a result, the player's body teleports directly into a tile. This can cause unexpected behavior. Instead, you should be modifying the velocity of the player, instead of the position.

EDIT: like so: https://plnkr.co/edit/R3IDtpb0w7ZX74zhRomr?p=preview
Also, be sure to set the inertia to infinity to prevent unwarranted player rotations/movements, this way no physical objects can effect the player unless you tell it to.

All 3 comments

Hey, it looks like the problem you are experiencing is because you are trying to move the player by directly changing it's position, as a result, the player's body teleports directly into a tile. This can cause unexpected behavior. Instead, you should be modifying the velocity of the player, instead of the position.

EDIT: like so: https://plnkr.co/edit/R3IDtpb0w7ZX74zhRomr?p=preview
Also, be sure to set the inertia to infinity to prevent unwarranted player rotations/movements, this way no physical objects can effect the player unless you tell it to.

@Technostalgic Thanks for the tip! Still believe this is incorrect behavior however. Maybe it'll be fixed by the CCD branch?

@Technostalgic is right, this is the approach you should take. The collision resolver can't do it's job unless you tell it what the body's velocity is, it's not a CCD related problem. If you still want to set position manually like that, you must also set velocity manually too for collisions to work correctly e.g.

  if (e.keyCode==38) {
    Matter.Body.translate(player, {x: 0, y: -3 });
    Matter.Body.setVelocity(player, {x: 0, y: -3 });
  }
  if (e.keyCode==37) {
    Matter.Body.translate(player, {x: -3, y: 0 });
    Matter.Body.setVelocity(player, {x: -3, y: 0 });
  }
  if (e.keyCode==39) {
    Matter.Body.translate(player, {x: 3, y: 0 });
    Matter.Body.setVelocity(player, {x: 3, y: 0 });
  }
  if (e.keyCode==40) {
    Matter.Body.translate(player, {x: 0, y: 3 });
    Matter.Body.setVelocity(player, {x: 0, y: 3 });
  }

Also take a look at the Body Manipulation example.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

car1ot picture car1ot  路  3Comments

moe091 picture moe091  路  3Comments

koko236 picture koko236  路  3Comments

BlueInt32 picture BlueInt32  路  4Comments

mrspeaker picture mrspeaker  路  3Comments