So, I added my physics classes...
[potpourri.git] / src / core / PhysicsSimulation.cpp
blobef5ced26914508fced1a1fc796c6d608e577b669
1 // Copyright 2008 Brian Caine
3 // This file is part of Potpourri.
5 // Potpourri is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Potpourri is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTIBILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Potpourri. If not, see <http://www.gnu.org/licenses/>.
19 // NOTES:
20 // sauce
22 #include "../../include/core/PhysicsSimulation.h"
23 #include <stdexcept>
25 using namespace fragrant;
27 PhysicsSimulation::PhysicsSimulation()
29 if (!initted)
31 cpInitChipmunk();
32 initted = true;
35 space = cpSpaceNew();
36 if (!space)
37 throw std::runtime_error(
38 "PhysicsSimulation::PhysicsSimulation(): couldn't make space");
40 space->gravity = cpv(GRAVITY.x, GRAVITY.y);
43 PhysicsSimulation::~PhysicsSimulation()
45 if (space)
46 cpSpaceFree(space);
49 void PhysicsSimulation::process(float max, float increment)
51 float tcur;
53 for (tcur = 0.0; tcur < max; tcur += increment)
55 int cur;
56 for (cur = 0; cur < objects.size(); cur++)
57 cpBodyResetForces(objects.at(cur)->body);
59 cpSpaceStep(space, increment);
62 return;
65 void PhysicsSimulation::addPhysicsObject(PhysicsObject* object)
67 objects.push_back(object);
69 object->parent = this;
70 object->addToSpace(space);
73 void PhysicsSimulation::removePhysicsObject(PhysicsObject* object)
75 std::vector<PhysicsObject*>::iterator iter;
76 std::vector<std::vector<PhysicsObject*>::iterator> to_del;
77 for (iter = objects.begin(); iter != objects.end(); iter++)
78 if (*iter == object)
79 to_del.push_back(iter);
81 int cur;
82 for (cur = (to_del.size() - 1); cur >= 0; cur--)
83 objects.erase(to_del.at(cur));
85 object->parent = 0;
86 object->removeFromSpace(space);
88 return;
91 bool PhysicsSimulation::initted = false;