liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.53k stars 1.96k forks source link

Small tunneling with Body.setVelocity #335

Open HildericSB opened 7 years ago

HildericSB commented 7 years ago

Hi !

I'm trying to do a platformer game with matter.js.

And I got trouble when pressing right key, that set Body.setVelocity X to -10 and jumping that set Body.setVelocity Y to -10. The problem is that the body enter slightly in the body next to him even with : inertia : Infinity, frictionStatic : 0, friction : 0, slop : 0, So because of this, the body collide with a body above him, and it should not. It' supposed to "glide" on the other body.

Img : sans titre

liabru commented 7 years ago

See comments in #334 which solve a similar issue.

HildericSB commented 7 years ago

You think it's a friction problem ? I don't get it ( sory i'm not really good about physics ). For me, the body that goes upward should not collide on Y abscisse, but just on his X.

sans titre

liabru commented 7 years ago

Are you saying it's colliding as if the block was overhanging, but it's not? A jsfiddle example would help here!

HildericSB commented 7 years ago

I'll try do to a JS fiddle. I'll try to copy this demo https://codepen.io/tanker837/pen/qaeQKZ for the movement. In this demo the player movement is set with force.x or force.y, and I use the setVelocity fn. It's may be better to use the force system ?

And do you know some other good examples of platformer games made with matterJS ?

Thanks for you help !

liabru commented 7 years ago

It depends on what the issue is, if it's what I think you're saying then there might be a bug. The velocity approach is a good one when you need precise movement (like a platformer player), the force approach is useful when you have a body that accelerates or turns more slowly (like a car).

The demo you linked is a great one though. I should probably do a more simple platform game example myself though!

HildericSB commented 7 years ago

It's hard to know if it's a bug, or if it's juste because I use badly the engine. I really should give you an example. The force approach (like in the linked demo) is a bit strange for a platformer. But I don't find example with velocity approach.

A simple example of a platform game could be awesome ! I would really like it.

HildericSB commented 7 years ago

I finnaly got an example !

https://jsfiddle.net/hsassewa/

gif

On the gif, the player body seems to get stuck between the others smaller body. How to avoid that ?

liabru commented 7 years ago

Thanks for the example, this might take some investigation as to whether it's a bug and if it's possible to solve in the engine.

Until then it looks like you're on the right approach to a good solution: check the direction is clear before moving there. A good way might be to try using Query.ray from the edge of the body's bounds along the velocity vector.

Another method you could try is to find all the current collisions for the player, then loop through them, find the vector between the player and the colliding body. Then dot product this with the collision.penetration vector and prevent movement if they're near parallel.

HildericSB commented 7 years ago

Hum so there is so there is no simple solution :/ I should try your second solution, and try to prevent movement if they're near parallel.

landgreen commented 7 years ago

I experienced this as well. I shaped the player's body to have angles at the bottom to avoid the problem.

https://landgreen.github.io/sidescroller/ Press T to enter testing to see the player shape.

HildericSB commented 7 years ago

Hum that's an interesting work around !

liabru commented 7 years ago

It is and you could probably use { chamfer: 8 } to get a similar effect easily?

HildericSB commented 7 years ago

Seems much better but still not perfect : https://jsfiddle.net/hsassewa/2/

eirikb commented 6 years ago

@hilde2 Did you find a good solution? Here is my current approach: https://jsfiddle.net/a7wjrh4u/2 . Movement: wad

The { chamfer: 8 } (thanks @liabru) helps a bit, but not completely. Another trick would be to simply not stack static bodies, and instead have a big one, like this: https://jsfiddle.net/a7wjrh4u/8 .
I'm not sure if movement is correctly done, the jumping feels a bit strange.

@hilde2 for your first pictures you could try putting the static bodies into each other, like this: https://jsfiddle.net/a7wjrh4u/9/