mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 08:17:35 +02:00
PhysicalEntity gets backpointer to SimulationEngine
This commit is contained in:
parent
c98631f820
commit
7cd1f75282
4 changed files with 82 additions and 7 deletions
|
@ -18,7 +18,13 @@ PhysicalEntity::PhysicalEntity() :
|
|||
_rotation(),
|
||||
_boundingRadius(0.0f),
|
||||
_shapesAreDirty(true),
|
||||
_enableShapes(false) {
|
||||
_enableShapes(false),
|
||||
_simulation(NULL) {
|
||||
}
|
||||
|
||||
PhysicalEntity::~PhysicalEntity() {
|
||||
// entity should be removed from the simulation before it is deleted
|
||||
assert(_simulation == NULL);
|
||||
}
|
||||
|
||||
void PhysicalEntity::setTranslation(const glm::vec3& translation) {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "CollisionInfo.h"
|
||||
|
||||
class Shape;
|
||||
class SimulationEngine;
|
||||
|
||||
// PhysicalEntity is the base class for anything that owns one or more Shapes that collide in a
|
||||
// SimulationEngine. Each CollisionInfo generated by a SimulationEngine has back pointers to the
|
||||
|
@ -29,7 +30,7 @@ class PhysicalEntity {
|
|||
|
||||
public:
|
||||
PhysicalEntity();
|
||||
virtual ~PhysicalEntity() {}
|
||||
virtual ~PhysicalEntity();
|
||||
|
||||
void setTranslation(const glm::vec3& translation);
|
||||
void setRotation(const glm::quat& rotation);
|
||||
|
@ -45,18 +46,24 @@ public:
|
|||
virtual void buildShapes() = 0;
|
||||
virtual void clearShapes();
|
||||
|
||||
SimulationEngine* getSimulation() const { return _simulation; }
|
||||
|
||||
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const;
|
||||
bool findCollisions(const QVector<const Shape*> shapes, CollisionList& collisions);
|
||||
bool findSphereCollisions(const glm::vec3& sphereCenter, float sphereRadius, CollisionList& collisions, int skipIndex);
|
||||
bool findPlaneCollisions(const glm::vec4& plane, CollisionList& collisions);
|
||||
|
||||
protected:
|
||||
// SimulationEngine is a friend so that it can set the protected _simulation backpointer
|
||||
friend SimulationEngine;
|
||||
|
||||
glm::vec3 _translation;
|
||||
glm::quat _rotation;
|
||||
float _boundingRadius;
|
||||
bool _shapesAreDirty;
|
||||
bool _enableShapes;
|
||||
QVector<Shape*> _shapes;
|
||||
SimulationEngine* _simulation;
|
||||
};
|
||||
|
||||
#endif // hifi_PhysicalEntity_h
|
||||
|
|
|
@ -11,13 +11,18 @@
|
|||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "SharedUtil.h"
|
||||
#include "ShapeCollider.h"
|
||||
#include "SimulationEngine.h"
|
||||
|
||||
int MAX_DOLLS_PER_ENGINE = 32;
|
||||
#include "PhysicalEntity.h"
|
||||
#include "Ragdoll.h"
|
||||
#include "SharedUtil.h"
|
||||
#include "ShapeCollider.h"
|
||||
|
||||
int MAX_DOLLS_PER_ENGINE = 16;
|
||||
int MAX_ENTITIES_PER_ENGINE = 64;
|
||||
int MAX_COLLISIONS_PER_ENGINE = 256;
|
||||
|
||||
|
||||
const int NUM_SHAPE_BITS = 6;
|
||||
const int SHAPE_INDEX_MASK = (1 << (NUM_SHAPE_BITS + 1)) - 1;
|
||||
|
||||
|
@ -28,6 +33,55 @@ SimulationEngine::~SimulationEngine() {
|
|||
_dolls.clear();
|
||||
}
|
||||
|
||||
bool SimulationEngine::addEntity(PhysicalEntity* entity) {
|
||||
if (!entity) {
|
||||
return false;
|
||||
}
|
||||
if (entity->_simulation == this) {
|
||||
int numEntities = _entities.size();
|
||||
for (int i = 0; i < numEntities; ++i) {
|
||||
if (entity == _entities.at(i)) {
|
||||
// already in list
|
||||
assert(entity->_simulation == this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// belongs to some other simulation
|
||||
return false;
|
||||
}
|
||||
int numEntities = _entities.size();
|
||||
if (numEntities > MAX_ENTITIES_PER_ENGINE) {
|
||||
// list is full
|
||||
return false;
|
||||
}
|
||||
// add to list
|
||||
entity->_simulation = this;
|
||||
_entities.push_back(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SimulationEngine::removeEntity(PhysicalEntity* entity) {
|
||||
if (!entity || !entity->_simulation || !(entity->_simulation == this)) {
|
||||
return;
|
||||
}
|
||||
int numEntities = _entities.size();
|
||||
for (int i = 0; i < numEntities; ++i) {
|
||||
if (entity == _entities.at(i)) {
|
||||
if (i == numEntities - 1) {
|
||||
// remove it
|
||||
_entities.pop_back();
|
||||
} else {
|
||||
// swap the last for this one
|
||||
PhysicalEntity* lastEntity = _entities[numEntities - 1];
|
||||
_entities.pop_back();
|
||||
_entities[i] = lastEntity;
|
||||
}
|
||||
entity->_simulation = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SimulationEngine::addRagdoll(Ragdoll* doll) {
|
||||
if (!doll) {
|
||||
return false;
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
#include <QVector>
|
||||
|
||||
#include "CollisionInfo.h"
|
||||
#include "Ragdoll.h"
|
||||
|
||||
class PhysicalEntity;
|
||||
class Ragdoll;
|
||||
|
||||
class SimulationEngine {
|
||||
public:
|
||||
|
@ -23,7 +25,12 @@ public:
|
|||
SimulationEngine();
|
||||
~SimulationEngine();
|
||||
|
||||
/// \return true if doll was added to, or already in the list
|
||||
/// \return true if entity was added to or is already in the list
|
||||
bool addEntity(PhysicalEntity* entity);
|
||||
|
||||
void removeEntity(PhysicalEntity* entity);
|
||||
|
||||
/// \return true if doll was added to or is already in the list
|
||||
bool addRagdoll(Ragdoll* doll);
|
||||
|
||||
void removeRagdoll(Ragdoll* doll);
|
||||
|
@ -45,6 +52,7 @@ public:
|
|||
|
||||
private:
|
||||
CollisionList _collisionList;
|
||||
QVector<PhysicalEntity*> _entities;
|
||||
QVector<Ragdoll*> _dolls;
|
||||
|
||||
// some stats for performance queries
|
||||
|
|
Loading…
Reference in a new issue