Ragdoll not an Entity. SkeletonModel is a Ragdoll

This commit is contained in:
Andrew Meadows 2014-06-13 12:27:58 -07:00
parent 5bd37acdb8
commit 158c7de76e
5 changed files with 20 additions and 32 deletions

View file

@ -17,7 +17,9 @@
#include "Menu.h"
#include "SkeletonModel.h"
SkeletonModel::SkeletonModel(Avatar* owningAvatar) :
SkeletonModel::SkeletonModel(Avatar* owningAvatar, QObject* parent) :
Model(parent),
Ragdoll(),
_owningAvatar(owningAvatar) {
}
@ -36,10 +38,8 @@ void SkeletonModel::setJointStates(QVector<JointState> states) {
parentIndices.push_back(state.getFBXJoint().parentIndex);
points.push_back(state.getPosition());
}
// ... and feed the results to _ragDoll
// It is OK that_jointShapes is probably empty at this stage: _ragDoll keeps a
// pointer to it and things will start working as soon as the list is populated.
_ragDoll.init(&_jointShapes, parentIndices, points);
// ... and feed the results to Ragdoll
initShapesAndPoints(&_jointShapes, parentIndices, points);
}
}
@ -105,14 +105,13 @@ void SkeletonModel::simulate(float deltaTime, bool fullUpdate) {
}
void SkeletonModel::simulateRagdoll(float deltaTime) {
// move ragdoll points toward joints
QVector<glm::vec3>& points = _ragDoll.getPoints();
// move ragdoll _points toward joints
const int numStates = _jointStates.size();
assert(numStates == points.size());
assert(numStates == _points.size());
float fraction = 0.1f; // fraction = 0.1f left intentionally low for demo purposes
float oneMinusFraction = 1.0f - fraction;
for (int i = 0; i < numStates; ++i) {
points[i] = oneMinusFraction * points[i] + fraction * _jointStates[i].getPosition();
_points[i] = oneMinusFraction * _points[i] + fraction * _jointStates[i].getPosition();
}
// enforce the constraints
@ -121,7 +120,7 @@ void SkeletonModel::simulateRagdoll(float deltaTime) {
int iterations = 0;
float delta = 0.0f;
do {
delta = _ragDoll.enforceConstraints();
delta = enforceConstraints();
++iterations;
} while (delta > MIN_CONSTRAINT_ERROR && iterations < MAX_ITERATIONS);
}
@ -273,7 +272,7 @@ void SkeletonModel::updateJointState(int index) {
void SkeletonModel::updateShapePositions() {
if (isActive() && _owningAvatar->isMyAvatar() &&
Menu::getInstance()->isOptionChecked(MenuOption::CollideAsRagdoll)) {
_ragDoll.updateShapes(_rotation, _translation);
updateShapes(_rotation, _translation);
} else {
Model::updateShapePositions();
}
@ -505,15 +504,14 @@ void SkeletonModel::renderRagdoll() {
glPushMatrix();
Application::getInstance()->loadTranslatedViewMatrix(_translation);
QVector<glm::vec3> points = _ragDoll.getPoints();
int numPoints = points.size();
int numPoints = _points.size();
float alpha = 0.3f;
float radius1 = 0.008f;
float radius2 = 0.01f;
for (int i = 0; i < numPoints; ++i) {
glPushMatrix();
// draw each point as a yellow hexagon with black border
glm::vec3 position = _rotation * points[i];
glm::vec3 position = _rotation * _points[i];
glTranslatef(position.x, position.y, position.z);
glColor4f(0.0f, 0.0f, 0.0f, alpha);
glutSolidSphere(radius2, BALL_SUBDIVISIONS, BALL_SUBDIVISIONS);

View file

@ -19,12 +19,12 @@
class Avatar;
/// A skeleton loaded from a model.
class SkeletonModel : public Model {
class SkeletonModel : public Model, public Ragdoll {
Q_OBJECT
public:
SkeletonModel(Avatar* owningAvatar);
SkeletonModel(Avatar* owningAvatar, QObject* parent = NULL);
void setJointStates(QVector<JointState> states);
@ -121,7 +121,6 @@ private:
void setHandPosition(int jointIndex, const glm::vec3& position, const glm::quat& rotation);
Avatar* _owningAvatar;
Ragdoll _ragDoll;
};
#endif // hifi_SkeletonModel_h

View file

@ -26,7 +26,6 @@ public:
enum EntityType {
ENTITY_UNKNOWN,
ENTITY_MODEL,
ENTITY_RAGDOLL,
};
static void setShapeBackPointers(const QVector<Shape*>& shapes, PhysicalEntity* entity);

View file

@ -94,14 +94,14 @@ void DistanceConstraint::updateProxyShape(Shape* shape, const glm::quat& rotatio
// Ragdoll
// ----------------------------------------------------------------------------
Ragdoll::Ragdoll() : PhysicalEntity(PhysicalEntity::ENTITY_RAGDOLL), _shapes(NULL) {
Ragdoll::Ragdoll() : _shapes(NULL) {
}
Ragdoll::~Ragdoll() {
clear();
}
void Ragdoll::init(QVector<Shape*>* shapes, const QVector<int>& parentIndices, const QVector<glm::vec3>& points) {
void Ragdoll::initShapesAndPoints(QVector<Shape*>* shapes, const QVector<int>& parentIndices, const QVector<glm::vec3>& points) {
clear();
_shapes = shapes;
const int numPoints = points.size();
@ -121,9 +121,6 @@ void Ragdoll::init(QVector<Shape*>* shapes, const QVector<int>& parentIndices, c
_constraints.push_back(stick);
}
}
if (_shapes) {
PhysicalEntity::setShapeBackPointers(*_shapes, this);
}
}
/// Delete all data.
@ -134,9 +131,6 @@ void Ragdoll::clear() {
}
_constraints.clear();
_points.clear();
if (_shapes) {
PhysicalEntity::setShapeBackPointers(*_shapes, NULL);
}
_shapes = NULL;
}

View file

@ -17,7 +17,7 @@
#include <QVector>
#include "PhysicalEntity.h"
class Shape;
class Constraint {
public:
@ -61,7 +61,7 @@ private:
glm::vec3* _points[2];
};
class Ragdoll : public PhysicalEntity {
class Ragdoll {
public:
Ragdoll();
@ -69,9 +69,7 @@ public:
/// Create points and constraints based on topology of collection of joints
/// \param joints list of connected joint states
void init(QVector<Shape*>* shapes, const QVector<int>& parentIndices, const QVector<glm::vec3>& points);
void setShapes(QVector<Shape*>* shapes) { _shapes = shapes; }
void initShapesAndPoints(QVector<Shape*>* shapes, const QVector<int>& parentIndices, const QVector<glm::vec3>& points);
/// Delete all data.
void clear();
@ -89,7 +87,7 @@ public:
/// Moves and modifies elements of _shapes to agree with state of _points
void updateShapes(const glm::quat& rotation, const glm::vec3& translation) const;
private:
protected:
QVector<glm::vec3> _points;
QVector<Constraint*> _constraints;