schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

Buoyancy demo nitpick #262

Closed sbj42 closed 7 years ago

sbj42 commented 7 years ago

In the buoyancy demo, if I put the "water" plane at a different Y position, the objects float at a different depth. Try setting plane.position[1] to -1 or 1, and see where the shapes come to rest.

I believe this is because of a bug on line 89:

var height = 0 - aabb.lowerBound[1];

This is meant to compute the height of the portion of the shape's bounding box that is "underwater". I think that 0 should actually be planePosition[1], so like this:

var height = planePosition[1] - aabb.lowerBound[1];
schteppe commented 7 years ago

It's not technically a bug, because the user cannot change the position of the plane in the demo?

In any case you're right and it should be using the correct bounds, if someone wants to reuse the code. Wanna submit a PR?

sbj42 commented 7 years ago

You're right. In fact that's how I spotted this, I was adapting it for my own project.

Yes, I'll submit a pull request. I found some other things along the way that I think would be worth having in the demo, so I'll include those as well. For instance, The applied force for each shape can be multiplied by body.mass / body.shapes.length so that it works better for bodies with different masses and compound bodies.

sbj42 commented 7 years ago

Pull request #263 submitted. I decided not to include the body.mass multiplier because that's a slippery slope leading to questions of body density and I didn't think that was necessarily important for the demo.