| 1 | <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">struct</span></strong> CrushingContext { |
| 2 | cpFloat magnitudeSum; |
| 3 | cpVect vectorSum; |
| 4 | }; |
| 5 | |
| 6 | <strong><span style="color:#881350;">static</span></strong> <strong><span style="color:#881350;">void</span></strong> |
| 7 | <span style="color:#003369;">EstimateCrushingHelper</span>(cpBody *body, cpArbiter *arb, <strong><span style="color:#881350;">struct</span></strong> CrushingContext *context) |
| 8 | { |
| 9 | cpVect j = <span style="color:#003369;">cpArbiterTotalImpulseWithFriction</span>(arb); |
| 10 | context->magnitudeSum += <span style="color:#003369;">cpvlength</span>(j); |
| 11 | context->vectorSum = <span style="color:#003369;">cpvadd</span>(context->vectorSum, j); |
| 12 | } |
| 13 | |
| 14 | cpFloat |
| 15 | <span style="color:#003369;">EstimateCrushForce</span>(cpBody *body, cpFloat dt) |
| 16 | { |
| 17 | <strong><span style="color:#881350;">struct</span></strong> CrushingContext crush = {<span style="color:#0000ff;">0.0f</span>, cpvzero}; |
| 18 | <span style="color:#003369;">cpBodyEachArbiter</span>(body, (cpBodyArbiterIteratorFunc)EstimateCrushingHelper, &crush); |
| 19 | |
| 20 | <span style="color:#236e25;"><em>// Compare the vector sum magnitude and magnitude sum to see if |
| 21 | </em></span> <span style="color:#236e25;"><em>// how much the collision forces oppose one another. |
| 22 | </em></span> cpFloat crushForce = (crush.magnitudeSum - <span style="color:#003369;">cpvlength</span>(crush.vectorSum))*dt; |
| 23 | }</pre> |