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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137/** 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/World.h"#include "box2d-lite/Body.h"#include "box2d-lite/Joint.h"
using std::vector;using std::map;using std::pair;
typedef map<ArbiterKey, Arbiter>::iterator ArbIter;typedef pair<ArbiterKey, Arbiter> ArbPair;
bool World::accumulateImpulses = true;bool World::warmStarting = true;bool World::positionCorrection = true;
void World::Add(Body* body){ bodies.push_back(body);}
void World::Add(Joint* joint){ joints.push_back(joint);}
void World::Clear(){ bodies.clear(); joints.clear(); arbiters.clear();}
void World::BroadPhase(){ // O(n^2) broad-phase for (int i = 0; i < (int)bodies.size(); ++i) { Body* bi = bodies[i];
for (int j = i + 1; j < (int)bodies.size(); ++j) { Body* bj = bodies[j];
if (bi->invMass == 0.0f && bj->invMass == 0.0f) continue;
Arbiter newArb(bi, bj); ArbiterKey key(bi, bj);
if (newArb.numContacts > 0) { ArbIter iter = arbiters.find(key); if (iter == arbiters.end()) { arbiters.insert(ArbPair(key, newArb)); } else { iter->second.Update(newArb.contacts, newArb.numContacts); } } else { arbiters.erase(key); } } }}
void World::Step(float dt){ float inv_dt = dt > 0.0f ? 1.0f / dt : 0.0f;
// Determine overlapping bodies and update contact points. BroadPhase();
// Integrate forces. for (int i = 0; i < (int)bodies.size(); ++i) { Body* b = bodies[i];
if (b->invMass == 0.0f) continue;
b->velocity += dt * (gravity + b->invMass * b->force); b->angularVelocity += dt * b->invI * b->torque; }
// Perform pre-steps. for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb) { arb->second.PreStep(inv_dt); }
for (int i = 0; i < (int)joints.size(); ++i) { joints[i]->PreStep(inv_dt); }
// Perform iterations for (int i = 0; i < iterations; ++i) { for (ArbIter arb = arbiters.begin(); arb != arbiters.end(); ++arb) { arb->second.ApplyImpulse(); }
for (int j = 0; j < (int)joints.size(); ++j) { joints[j]->ApplyImpulse(); } }
// Integrate Velocities for (int i = 0; i < (int)bodies.size(); ++i) { Body* b = bodies[i];
b->position += dt * b->velocity; b->rotation += dt * b->angularVelocity;
b->force.Set(0.0f, 0.0f); b->torque = 0.0f; }}