Something went wrong. Try again.
Box2d lite but uses fixed points instead of floats for embedded devices, fork from https://github.com/erincatto/box2d-lite github.com/erincatto/box2d-lite
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198/** Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies.* Erin Catto makes no representations about the suitability * of this software for any purpose. * It is provided "as is" without express or implied warranty.*/
#include "box2d-lite/Arbiter.h"#include "box2d-lite/Body.h"#include "box2d-lite/World.h"
Arbiter::Arbiter(Body* b1, Body* b2){ if (b1 < b2) { body1 = b1; body2 = b2; } else { body1 = b2; body2 = b1; }
numContacts = Collide(contacts, body1, body2);
friction = fpm::sqrt(body1->friction * body2->friction);}
void Arbiter::Update(Contact* newContacts, int numNewContacts){ Contact mergedContacts[2];
for (int i = 0; i < numNewContacts; ++i) { Contact* cNew = newContacts + i; int k = -1; for (int j = 0; j < numContacts; ++j) { Contact* cOld = contacts + j; if (cNew->feature.value == cOld->feature.value) { k = j; break; } }
if (k > -1) { Contact* c = mergedContacts + i; Contact* cOld = contacts + k; *c = *cNew; if (World::warmStarting) { c->Pn = cOld->Pn; c->Pt = cOld->Pt; c->Pnb = cOld->Pnb; } else { c->Pn = fixed_float(0.0f); c->Pt = fixed_float(0.0f); c->Pnb = fixed_float(0.0f); } } else { mergedContacts[i] = newContacts[i]; } }
for (int i = 0; i < numNewContacts; ++i) contacts[i] = mergedContacts[i];
numContacts = numNewContacts;}
void Arbiter::PreStep(fixed_float inv_dt){ const fixed_float k_allowedPenetration { 0.01f } ; fixed_float k_biasFactor = World::positionCorrection ? fixed_float(0.2f) : fixed_float(0.0f);
for (int i = 0; i < numContacts; ++i) { Contact* c = contacts + i;
Vec2 r1 = c->position - body1->position; Vec2 r2 = c->position - body2->position;
// Precompute normal mass, tangent mass, and bias. fixed_float rn1 = Dot(r1, c->normal); fixed_float rn2 = Dot(r2, c->normal); fixed_float kNormal = body1->invMass + body2->invMass; kNormal += body1->invI * (Dot(r1, r1) - rn1 * rn1) + body2->invI * (Dot(r2, r2) - rn2 * rn2); c->massNormal = fixed_float(1.0f) / kNormal;
Vec2 tangent = Cross(c->normal, fixed_float(1.0f)); fixed_float rt1 = Dot(r1, tangent); fixed_float rt2 = Dot(r2, tangent); fixed_float kTangent = body1->invMass + body2->invMass; kTangent += body1->invI * (Dot(r1, r1) - rt1 * rt1) + body2->invI * (Dot(r2, r2) - rt2 * rt2); c->massTangent = fixed_float(1.0f) / kTangent;
c->bias = -k_biasFactor * inv_dt * Min(fixed_float(0.0f), c->separation + k_allowedPenetration);
if (World::accumulateImpulses) { // Apply normal + friction impulse Vec2 P = c->Pn * c->normal + c->Pt * tangent;
body1->velocity -= body1->invMass * P; body1->angularVelocity -= body1->invI * Cross(r1, P);
body2->velocity += body2->invMass * P; body2->angularVelocity += body2->invI * Cross(r2, P); } }}
void Arbiter::ApplyImpulse(){ Body* b1 = body1; Body* b2 = body2;
for (int i = 0; i < numContacts; ++i) { Contact* c = contacts + i; c->r1 = c->position - b1->position; c->r2 = c->position - b2->position;
// Relative velocity at contact Vec2 dv = b2->velocity + Cross(b2->angularVelocity, c->r2) - b1->velocity - Cross(b1->angularVelocity, c->r1);
// Compute normal impulse fixed_float vn = Dot(dv, c->normal);
fixed_float dPn = c->massNormal * (-vn + c->bias);
if (World::accumulateImpulses) { // Clamp the accumulated impulse fixed_float Pn0 = c->Pn; c->Pn = Max(Pn0 + dPn, fixed_float(0.0f)); dPn = c->Pn - Pn0; } else { dPn = Max(dPn, fixed_float(0.0f)); }
// Apply contact impulse Vec2 Pn = dPn * c->normal;
b1->velocity -= b1->invMass * Pn; b1->angularVelocity -= b1->invI * Cross(c->r1, Pn);
b2->velocity += b2->invMass * Pn; b2->angularVelocity += b2->invI * Cross(c->r2, Pn);
// Relative velocity at contact dv = b2->velocity + Cross(b2->angularVelocity, c->r2) - b1->velocity - Cross(b1->angularVelocity, c->r1);
Vec2 tangent = Cross(c->normal, fixed_float(1.0f)); fixed_float vt = Dot(dv, tangent); fixed_float dPt = c->massTangent * (-vt);
if (World::accumulateImpulses) { // Compute friction impulse fixed_float maxPt = friction * c->Pn;
// Clamp friction fixed_float oldTangentImpulse = c->Pt; c->Pt = Clamp(oldTangentImpulse + dPt, -maxPt, maxPt); dPt = c->Pt - oldTangentImpulse; } else { fixed_float maxPt = friction * dPn; dPt = Clamp(dPt, -maxPt, maxPt); }
// Apply contact impulse Vec2 Pt = dPt * tangent;
b1->velocity -= b1->invMass * Pt; b1->angularVelocity -= b1->invI * Cross(c->r1, Pt);
b2->velocity += b2->invMass * Pt; b2->angularVelocity += b2->invI * Cross(c->r2, Pt); }}