From 14728c0c583179fef14a0a75b80cbb8376348ed8 Mon Sep 17 00:00:00 2001 From: James Clark Date: Wed, 5 Oct 2016 21:08:48 -0400 Subject: [PATCH 1/2] In the buoyancy demo, use the water plane's Y position instead of 0 when computing the area of the submerged shape. The plane's position in this demo happens to be 0 so it worked fine, but using the planePosition variable may help avoid a hurdle for people who use this code as a template. --- demos/buoyancy.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demos/buoyancy.html b/demos/buoyancy.html index 068100e0..b78395fb 100644 --- a/demos/buoyancy.html +++ b/demos/buoyancy.html @@ -86,7 +86,7 @@ } else if(aabb.lowerBound[1] < planePosition[1]){ // Partially submerged var width = aabb.upperBound[0] - aabb.lowerBound[0]; - var height = 0 - aabb.lowerBound[1]; + var height = planePosition[1] - aabb.lowerBound[1]; areaUnderWater = width * height; p2.vec2.set(centerOfBouyancy, aabb.lowerBound[0] + width / 2, aabb.lowerBound[1] + height / 2); } else { From fc33f4d515d17e78672cabdc54f47a0a96150a46 Mon Sep 17 00:00:00 2001 From: James Clark Date: Wed, 5 Oct 2016 21:16:24 -0400 Subject: [PATCH 2/2] In the buoyancy demo, divide the magnitude of the applied forces by the number of shapes in the body. This way, two bodies with equal mass and area but with different numbers of shapes will behave the same. Without this change, the forces were disproportionately larger for complex bodies (such as the four-rectangle body2 in the demo), causing them to settle higher in the water than simpler bodies would. --- demos/buoyancy.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/buoyancy.html b/demos/buoyancy.html index b78395fb..33304558 100644 --- a/demos/buoyancy.html +++ b/demos/buoyancy.html @@ -95,7 +95,7 @@ // Compute lift force p2.vec2.subtract(liftForce, planePosition, centerOfBouyancy); - p2.vec2.scale(liftForce, liftForce, areaUnderWater * k); + p2.vec2.scale(liftForce, liftForce, areaUnderWater * k / body.shapes.length); liftForce[0] = 0; // Make center of bouycancy relative to the body @@ -103,7 +103,7 @@ // Viscous force body.getVelocityAtPoint(v, centerOfBouyancy); - p2.vec2.scale(viscousForce, v, -c); + p2.vec2.scale(viscousForce, v, -c / body.shapes.length); // Apply forces body.applyForce(viscousForce,centerOfBouyancy);