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/>.
22 #include "../../include/core/PhysicsSimulation.h"
25 using namespace fragrant
;
27 PhysicsSimulation::PhysicsSimulation()
37 throw std::runtime_error(
38 "PhysicsSimulation::PhysicsSimulation(): couldn't make space");
40 space
->gravity
= cpv(GRAVITY
.x
, GRAVITY
.y
);
43 PhysicsSimulation::~PhysicsSimulation()
49 void PhysicsSimulation::process(float max
, float increment
)
53 for (tcur
= 0.0; tcur
< max
; tcur
+= increment
)
56 for (cur
= 0; cur
< objects
.size(); cur
++)
57 cpBodyResetForces(objects
.at(cur
)->body
);
59 cpSpaceStep(space
, increment
);
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
++)
79 to_del
.push_back(iter
);
82 for (cur
= (to_del
.size() - 1); cur
>= 0; cur
--)
83 objects
.erase(to_del
.at(cur
));
86 object
->removeFromSpace(space
);
91 bool PhysicsSimulation::initted
= false;