2010-09-09 19:41:14 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal *
|
|
|
|
* in the Software without restriction, including without limitation the rights *
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included in *
|
|
|
|
* all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
|
|
|
* THE SOFTWARE. *
|
|
|
|
********************************************************************************/
|
2009-02-10 14:31:43 +00:00
|
|
|
|
2010-07-19 19:35:50 +00:00
|
|
|
#ifndef PHYSICSWORLD_H
|
|
|
|
#define PHYSICSWORLD_H
|
2009-02-10 14:31:43 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <vector>
|
2010-09-05 07:56:24 +00:00
|
|
|
#include <algorithm>
|
2009-02-10 14:31:43 +00:00
|
|
|
#include "../mathematics/mathematics.h"
|
|
|
|
#include "../body/Body.h"
|
2010-02-06 19:39:04 +00:00
|
|
|
#include "../constraint/Constraint.h"
|
|
|
|
#include "../constraint/Contact.h"
|
2009-02-10 14:31:43 +00:00
|
|
|
|
|
|
|
// Namespace reactphysics3d
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
Class PhysicsWorld :
|
2010-02-06 19:19:57 +00:00
|
|
|
This class represents the world of the
|
|
|
|
physics engine. The physics world contains all the bodies of the physics
|
2009-02-10 14:31:43 +00:00
|
|
|
engine.
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class PhysicsWorld {
|
|
|
|
protected :
|
2010-04-06 16:45:48 +00:00
|
|
|
std::vector<Body*> bodies; // list that contains all bodies of the physics world
|
2010-08-05 15:06:34 +00:00
|
|
|
std::vector<Body*> addedBodies; // Added bodies since last update
|
|
|
|
std::vector<Body*> removedBodies; // Removed bodies since last update
|
2010-04-06 16:45:48 +00:00
|
|
|
std::vector<Constraint*> constraints; // List that contains all the current constraints
|
2009-02-10 14:31:43 +00:00
|
|
|
Vector3D gravity; // Gravity vector of the world
|
2009-03-07 14:40:55 +00:00
|
|
|
bool isGravityOn; // True if the gravity force is on
|
2009-02-10 14:31:43 +00:00
|
|
|
|
|
|
|
public :
|
|
|
|
PhysicsWorld(const Vector3D& gravity); // Constructor
|
|
|
|
virtual ~PhysicsWorld(); // Destructor
|
|
|
|
|
2010-09-05 07:56:24 +00:00
|
|
|
void addBody(Body* body); // Add a body to the physics world
|
|
|
|
void removeBody(Body const* const body); // Remove a body from the physics world
|
2010-08-05 15:06:34 +00:00
|
|
|
void clearAddedAndRemovedBodies(); // Clear the addedBodies and removedBodies sets
|
2009-02-12 12:38:27 +00:00
|
|
|
Vector3D getGravity() const; // Return the gravity vector of the world
|
2009-03-07 14:40:55 +00:00
|
|
|
bool getIsGravityOn() const; // Return if the gravity is on
|
|
|
|
void setIsGratityOn(bool isGravityOn); // Set the isGravityOn attribute
|
2010-09-05 07:56:24 +00:00
|
|
|
void addConstraint(Constraint* constraint); // Add a constraint
|
|
|
|
void removeConstraint(Constraint* constraint); // Remove a constraint
|
|
|
|
void removeAllContactConstraints(); // Remove all collision contacts constraints
|
|
|
|
void removeAllConstraints(); // Remove all constraints and delete them (free their memory)
|
|
|
|
std::vector<Constraint*>::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list
|
|
|
|
std::vector<Constraint*>::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list
|
|
|
|
std::vector<Body*>::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world
|
|
|
|
std::vector<Body*>::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world
|
|
|
|
std::vector<Body*>& getAddedBodies(); // Return the added bodies since last update of the physics engine
|
|
|
|
std::vector<Body*>& getRemovedBodies(); // Retrun the removed bodies since last update of the physics engine
|
2009-02-10 14:31:43 +00:00
|
|
|
};
|
|
|
|
|
2010-09-05 07:56:24 +00:00
|
|
|
// Add a body to the physics world
|
|
|
|
inline void PhysicsWorld::addBody(Body* body) {
|
|
|
|
std::vector<Body*>::iterator it;
|
|
|
|
|
|
|
|
assert(body);
|
|
|
|
it = std::find(bodies.begin(), bodies.end(), body);
|
|
|
|
assert(it == bodies.end());
|
|
|
|
|
|
|
|
// The body isn't already in the bodyList, therefore we add it to the list
|
|
|
|
bodies.push_back(body);
|
|
|
|
addedBodies.push_back(body);
|
|
|
|
it = std::find(removedBodies.begin(), removedBodies.end(), body);
|
|
|
|
if (it != removedBodies.end()) {
|
|
|
|
removedBodies.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a body from the physics world
|
|
|
|
inline void PhysicsWorld::removeBody(Body const* const body) {
|
|
|
|
std::vector<Body*>::iterator it;
|
|
|
|
|
|
|
|
assert(body);
|
|
|
|
it = std::find(bodies.begin(), bodies.end(), body);
|
|
|
|
assert(*it == body);
|
|
|
|
|
|
|
|
// Remove the body
|
|
|
|
bodies.erase(it);
|
|
|
|
addedBodies.erase(it);
|
|
|
|
removedBodies.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a constraint into the physics world
|
|
|
|
inline void PhysicsWorld::addConstraint(Constraint* constraint) {
|
|
|
|
assert(constraint != 0);
|
|
|
|
constraints.push_back(constraint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a constraint and free its memory
|
|
|
|
inline void PhysicsWorld::removeConstraint(Constraint* constraint) {
|
|
|
|
std::vector<Constraint*>::iterator it;
|
|
|
|
|
|
|
|
assert(constraint);
|
|
|
|
it = std::find(constraints.begin(), constraints.end(), constraint);
|
|
|
|
assert(*it == constraint);
|
|
|
|
delete *it;
|
|
|
|
constraints.erase(it);
|
|
|
|
}
|
2009-02-12 12:38:27 +00:00
|
|
|
|
2010-08-05 15:06:34 +00:00
|
|
|
// Clear the addedBodies and removedBodies sets
|
|
|
|
inline void PhysicsWorld::clearAddedAndRemovedBodies() {
|
|
|
|
addedBodies.clear();
|
|
|
|
removedBodies.clear();
|
|
|
|
}
|
|
|
|
|
2009-02-12 12:38:27 +00:00
|
|
|
// Return the gravity vector of the world
|
|
|
|
inline Vector3D PhysicsWorld::getGravity() const {
|
|
|
|
return gravity;
|
|
|
|
}
|
|
|
|
|
2009-03-07 14:40:55 +00:00
|
|
|
// Return if the gravity is on
|
|
|
|
inline bool PhysicsWorld::getIsGravityOn() const {
|
|
|
|
return isGravityOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the isGravityOn attribute
|
|
|
|
inline void PhysicsWorld::setIsGratityOn(bool isGravityOn) {
|
|
|
|
this->isGravityOn = isGravityOn;
|
|
|
|
}
|
|
|
|
|
2010-02-06 19:39:04 +00:00
|
|
|
// Return a start iterator on the constraint list
|
2010-06-05 22:22:06 +00:00
|
|
|
inline std::vector<Constraint*>::iterator PhysicsWorld::getConstraintsBeginIterator() {
|
2010-04-06 16:45:48 +00:00
|
|
|
return constraints.begin();
|
2010-02-06 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a end iterator on the constraint list
|
2010-06-05 22:22:06 +00:00
|
|
|
inline std::vector<Constraint*>::iterator PhysicsWorld::getConstraintsEndIterator() {
|
2010-04-06 16:45:48 +00:00
|
|
|
return constraints.end();
|
|
|
|
}
|
|
|
|
|
2010-06-05 22:22:06 +00:00
|
|
|
// Return an iterator to the beginning of the bodies of the physics world
|
|
|
|
inline std::vector<Body*>::iterator PhysicsWorld::getBodiesBeginIterator() {
|
|
|
|
return bodies.begin();
|
2010-04-06 16:45:48 +00:00
|
|
|
}
|
|
|
|
|
2010-06-05 22:22:06 +00:00
|
|
|
// Return an iterator to the end of the bodies of the physics world
|
|
|
|
inline std::vector<Body*>::iterator PhysicsWorld::getBodiesEndIterator() {
|
|
|
|
return bodies.end();
|
2010-02-06 19:39:04 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 15:06:34 +00:00
|
|
|
// Return the added bodies since last update of the physics engine
|
|
|
|
inline std::vector<Body*>& PhysicsWorld::getAddedBodies() {
|
|
|
|
return addedBodies;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrun the removed bodies since last update of the physics engine
|
|
|
|
inline std::vector<Body*>& PhysicsWorld::getRemovedBodies() {
|
|
|
|
return removedBodies;
|
|
|
|
}
|
|
|
|
|
2009-02-10 14:31:43 +00:00
|
|
|
} // End of the ReactPhysics3D namespace
|
|
|
|
|
2010-06-05 22:22:06 +00:00
|
|
|
#endif
|