From f2168995d37476d44c58708c0cfccc51445a4256 Mon Sep 17 00:00:00 2001 From: Andrey Kuzmin Date: Sat, 23 Mar 2024 21:30:08 +0100 Subject: [PATCH] update --- examples/review/src/ReviewConfig.elm | 2 +- sandbox/review/src/ReviewConfig.elm | 2 +- src/Collision/ConvexConvex.elm | 8 +++--- src/Internal/Body.elm | 10 ++++--- src/Internal/Solver.elm | 40 +++++++++++++++++----------- src/Internal/SolverBody.elm | 2 +- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/examples/review/src/ReviewConfig.elm b/examples/review/src/ReviewConfig.elm index 5630c29..2363de5 100644 --- a/examples/review/src/ReviewConfig.elm +++ b/examples/review/src/ReviewConfig.elm @@ -34,6 +34,6 @@ config = , NoUnused.Parameters.rule , NoUnused.Patterns.rule , NoUnused.Variables.rule - , Simplify.rule + , Simplify.rule Simplify.defaults , NoUnoptimizedRecursion.rule (NoUnoptimizedRecursion.optOutWithComment "IGNORE TCO") ] diff --git a/sandbox/review/src/ReviewConfig.elm b/sandbox/review/src/ReviewConfig.elm index dcb0cac..facabdf 100644 --- a/sandbox/review/src/ReviewConfig.elm +++ b/sandbox/review/src/ReviewConfig.elm @@ -35,6 +35,6 @@ config = , NoUnused.Parameters.rule , NoUnused.Patterns.rule , NoUnused.Variables.rule - , Simplify.rule + , Simplify.rule Simplify.defaults , NoUnoptimizedRecursion.rule (NoUnoptimizedRecursion.optOutWithComment "IGNORE TCO") ] diff --git a/src/Collision/ConvexConvex.elm b/src/Collision/ConvexConvex.elm index c8ea459..25c89d0 100644 --- a/src/Collision/ConvexConvex.elm +++ b/src/Collision/ConvexConvex.elm @@ -15,12 +15,12 @@ addContacts : Convex -> Convex -> List Contact -> List Contact addContacts convex1 convex2 contacts = case findSeparatingAxis convex1 convex2 of Just separatingAxis -> - let - reversedSeparatingAxis = - Vec3.negate separatingAxis - in case bestFace convex1.faces separatingAxis of Just face1 -> + let + reversedSeparatingAxis = + Vec3.negate separatingAxis + in case bestFace convex2.faces reversedSeparatingAxis of Just face2 -> clipTwoFaces face1 face2 reversedSeparatingAxis contacts diff --git a/src/Internal/Body.elm b/src/Internal/Body.elm index 1be60b2..ce5b114 100644 --- a/src/Internal/Body.elm +++ b/src/Internal/Body.elm @@ -113,15 +113,13 @@ compound shapes data = { id = -1 , data = data , material = Material.default - , transform3d = transform3d - , centerOfMassTransform3d = centerOfMassTransform3d , velocity = Vec3.zero , angularVelocity = Vec3.zero + , transform3d = transform3d + , centerOfMassTransform3d = centerOfMassTransform3d , mass = 0 , shapes = movedShapes , worldShapes = Shape.shapesPlaceIn shapeTransform shapes - , force = Vec3.zero - , torque = Vec3.zero , boundingSphereRadius = List.foldl Shape.expandBoundingSphereRadius 0 movedShapes -- default damping @@ -132,6 +130,10 @@ compound shapes data = , invMass = 0 , invInertia = Mat3.zero , invInertiaWorld = Mat3.zero + + -- forces + , force = Vec3.zero + , torque = Vec3.zero } diff --git a/src/Internal/Solver.elm b/src/Internal/Solver.elm index d287aad..ef9883a 100644 --- a/src/Internal/Solver.elm +++ b/src/Internal/Solver.elm @@ -228,20 +228,15 @@ solveEquationsGroup body1 body2 equations deltalambdaTot currentEquations = else deltalambdaPrev - invI1 = - body1.body.invInertiaWorld - - invI2 = - body2.body.invInertiaWorld - - k1 = - deltalambda * body1.body.invMass - - k2 = - deltalambda * body2.body.invMass - newBody1 = if body1.body.mass > 0 then + let + invI1 = + body1.body.invInertiaWorld + + k1 = + deltalambda * body1.body.invMass + in { body = body1.body , vX = body1.vX - k1 * vB.x , vY = body1.vY - k1 * vB.y @@ -257,6 +252,13 @@ solveEquationsGroup body1 body2 equations deltalambdaTot currentEquations = newBody2 = if body2.body.mass > 0 then + let + invI2 = + body2.body.invInertiaWorld + + k2 = + deltalambda * body2.body.invMass + in { body = body2.body , vX = body2.vX + k2 * vB.x , vY = body2.vY + k2 * vB.y @@ -282,7 +284,7 @@ solveEquationsGroup body1 body2 equations deltalambdaTot currentEquations = remainingEquations -updateBodies : { dt : Float, gravity : Vec3, gravityLength : Float } -> Array (SolverBody data) -> World data -> World data +updateBodies : { ctx | dt : Float, gravity : Vec3 } -> Array (SolverBody data) -> World data -> World data updateBodies ctx bodies world = let simulatedBodies = @@ -304,7 +306,15 @@ updateBodies ctx bodies world = in { world | bodies = - Array.toList simulatedBodies - |> List.filter (\{ id } -> id + 1 > 0) + Array.foldr + (\body list -> + if body.id + 1 > 0 then + body :: list + + else + list + ) + [] + simulatedBodies , simulatedBodies = simulatedBodies } diff --git a/src/Internal/SolverBody.elm b/src/Internal/SolverBody.elm index 9f48a34..368eec7 100644 --- a/src/Internal/SolverBody.elm +++ b/src/Internal/SolverBody.elm @@ -33,7 +33,7 @@ fromBody body = } -toBody : { dt : Float, gravity : Vec3, gravityLength : Float } -> SolverBody data -> Body data +toBody : { ctx | dt : Float, gravity : Vec3 } -> SolverBody data -> Body data toBody { dt, gravity } { body, vX, vY, vZ, wX, wY, wZ } = let -- Apply damping https://code.google.com/archive/p/bullet/issues/74