schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

Shape offset behavior? #300

Open wildex999 opened 7 years ago

wildex999 commented 7 years ago

Hello, I'm trying out P2 for collisions and movement handling in a game, and have met some weird behavior. I have two bodies: A Static Box:

let body = new p2js.Body();
body.mass = 0;
body.type = p2js.Body.STATIC;
body.position[0] = 100
body.position[1] = 100
let shape = new p2js.Box({width: 16, height: 16});
body.addShape(shape);

A Dynamic Circle:

this.body = new p2js.Body();
this.body.mass = 1;
let shape = new p2js.Circle({radius:5});
this.body.type = p2js.Body.DYNAMIC;
this.body.addShape(shape, [0, 5]);

Idea here being the box is a tilemap collider, and the circle is the player. The problem I have is that if I do not have an offset in this.body.addShape(shape);, everything works fine. When the circle hits the box it stops immediately, as expected.

However, as soon as I start adding an offset(I.e trying to get the Circle to the feet of my player character) the collisions starts behaving oddly. With an y-offset of between 0 and ~0.8, it works. With an y-offset of 5 my character will behave as if the circle has a diameter of < 1, and go inside the box from all directions except below. Sometimes it will collide correctly above, then suddenly fall into the box. The sides won't collide except when I try to hit the exact middle of the box. The character will also start "bouncing" against the places where it does collide, which it does not with no offset. I move the Dynamic body by setting a velocity each frame.

Have I just misunderstood how Shape Offset is supposed to work? Is there a variable I have not set correctly? Or is this a bug? I tried with both version 0.7.1 and master.

schteppe commented 7 years ago

It should work... Did you try running this code in the Demo framework? If not, copy one of the HTML files in demos/ and add your code into it. It's much easier to debug