mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 16:41:02 +02:00
split SkeletonModel and Ragdoll classes apart
This commit is contained in:
parent
ca4a23e532
commit
46c91052c9
7 changed files with 263 additions and 162 deletions
|
@ -83,10 +83,11 @@ MyAvatar::MyAvatar() :
|
||||||
for (int i = 0; i < MAX_DRIVE_KEYS; i++) {
|
for (int i = 0; i < MAX_DRIVE_KEYS; i++) {
|
||||||
_driveKeys[i] = 0.0f;
|
_driveKeys[i] = 0.0f;
|
||||||
}
|
}
|
||||||
_skeletonModel.setEnableShapes(true);
|
|
||||||
// The skeleton is both a PhysicsEntity and Ragdoll, so we add it to the simulation once for each type.
|
|
||||||
_physicsSimulation.setEntity(&_skeletonModel);
|
_physicsSimulation.setEntity(&_skeletonModel);
|
||||||
_physicsSimulation.setRagdoll(&_skeletonModel);
|
|
||||||
|
_skeletonModel.setEnableShapes(true);
|
||||||
|
Ragdoll* ragdoll = _skeletonModel.buildRagdoll();
|
||||||
|
_physicsSimulation.setRagdoll(ragdoll);
|
||||||
}
|
}
|
||||||
|
|
||||||
MyAvatar::~MyAvatar() {
|
MyAvatar::~MyAvatar() {
|
||||||
|
@ -1604,10 +1605,10 @@ void MyAvatar::updateCollisionWithAvatars(float deltaTime) {
|
||||||
if (simulation != &(_physicsSimulation)) {
|
if (simulation != &(_physicsSimulation)) {
|
||||||
skeleton->setEnableShapes(true);
|
skeleton->setEnableShapes(true);
|
||||||
_physicsSimulation.addEntity(skeleton);
|
_physicsSimulation.addEntity(skeleton);
|
||||||
_physicsSimulation.addRagdoll(skeleton);
|
_physicsSimulation.addRagdoll(skeleton->getRagdoll());
|
||||||
}
|
}
|
||||||
} else if (simulation == &(_physicsSimulation)) {
|
} else if (simulation == &(_physicsSimulation)) {
|
||||||
_physicsSimulation.removeRagdoll(skeleton);
|
_physicsSimulation.removeRagdoll(skeleton->getRagdoll());
|
||||||
_physicsSimulation.removeEntity(skeleton);
|
_physicsSimulation.removeEntity(skeleton);
|
||||||
skeleton->setEnableShapes(false);
|
skeleton->setEnableShapes(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,13 +23,21 @@
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
#include "MuscleConstraint.h"
|
#include "MuscleConstraint.h"
|
||||||
#include "SkeletonModel.h"
|
#include "SkeletonModel.h"
|
||||||
|
#include "SkeletonRagdoll.h"
|
||||||
|
|
||||||
SkeletonModel::SkeletonModel(Avatar* owningAvatar, QObject* parent) :
|
SkeletonModel::SkeletonModel(Avatar* owningAvatar, QObject* parent) :
|
||||||
Model(parent),
|
Model(parent),
|
||||||
Ragdoll(),
|
|
||||||
_owningAvatar(owningAvatar),
|
_owningAvatar(owningAvatar),
|
||||||
_boundingShape(),
|
_boundingShape(),
|
||||||
_boundingShapeLocalOffset(0.0f) {
|
_boundingShapeLocalOffset(0.0f),
|
||||||
|
_ragdoll(NULL) {
|
||||||
|
}
|
||||||
|
|
||||||
|
SkeletonModel::~SkeletonModel() {
|
||||||
|
if (_ragdoll) {
|
||||||
|
delete _ragdoll;
|
||||||
|
_ragdoll = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonModel::setJointStates(QVector<JointState> states) {
|
void SkeletonModel::setJointStates(QVector<JointState> states) {
|
||||||
|
@ -155,9 +163,6 @@ void SkeletonModel::getBodyShapes(QVector<const Shape*>& shapes) const {
|
||||||
void SkeletonModel::renderIKConstraints() {
|
void SkeletonModel::renderIKConstraints() {
|
||||||
renderJointConstraints(getRightHandJointIndex());
|
renderJointConstraints(getRightHandJointIndex());
|
||||||
renderJointConstraints(getLeftHandJointIndex());
|
renderJointConstraints(getLeftHandJointIndex());
|
||||||
//if (isActive() && _owningAvatar->isMyAvatar()) {
|
|
||||||
// renderRagdoll();
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class IndexValue {
|
class IndexValue {
|
||||||
|
@ -489,21 +494,25 @@ bool SkeletonModel::getEyePositions(glm::vec3& firstEyePosition, glm::vec3& seco
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonModel::renderRagdoll() {
|
void SkeletonModel::renderRagdoll() {
|
||||||
|
if (!_ragdoll) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const QVector<VerletPoint>& points = _ragdoll->getRagdollPoints();
|
||||||
const int BALL_SUBDIVISIONS = 6;
|
const int BALL_SUBDIVISIONS = 6;
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
glDisable(GL_LIGHTING);
|
glDisable(GL_LIGHTING);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
Application::getInstance()->loadTranslatedViewMatrix(_translation);
|
Application::getInstance()->loadTranslatedViewMatrix(_translation);
|
||||||
int numPoints = _ragdollPoints.size();
|
int numPoints = points.size();
|
||||||
float alpha = 0.3f;
|
float alpha = 0.3f;
|
||||||
float radius1 = 0.008f;
|
float radius1 = 0.008f;
|
||||||
float radius2 = 0.01f;
|
float radius2 = 0.01f;
|
||||||
glm::vec3 simulationTranslation = getTranslationInSimulationFrame();
|
glm::vec3 simulationTranslation = _ragdoll->getTranslationInSimulationFrame();
|
||||||
for (int i = 0; i < numPoints; ++i) {
|
for (int i = 0; i < numPoints; ++i) {
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
// NOTE: ragdollPoints are in simulation-frame but we want them to be model-relative
|
// NOTE: ragdollPoints are in simulation-frame but we want them to be model-relative
|
||||||
glm::vec3 position = _ragdollPoints[i]._position - simulationTranslation;
|
glm::vec3 position = points[i]._position - simulationTranslation;
|
||||||
glTranslatef(position.x, position.y, position.z);
|
glTranslatef(position.x, position.y, position.z);
|
||||||
// draw each point as a yellow hexagon with black border
|
// draw each point as a yellow hexagon with black border
|
||||||
glColor4f(0.0f, 0.0f, 0.0f, alpha);
|
glColor4f(0.0f, 0.0f, 0.0f, alpha);
|
||||||
|
@ -517,109 +526,18 @@ void SkeletonModel::renderRagdoll() {
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// virtual
|
|
||||||
void SkeletonModel::initRagdollPoints() {
|
|
||||||
clearRagdollConstraintsAndPoints();
|
|
||||||
_muscleConstraints.clear();
|
|
||||||
|
|
||||||
initRagdollTransform();
|
|
||||||
// one point for each joint
|
|
||||||
int numStates = _jointStates.size();
|
|
||||||
_ragdollPoints.fill(VerletPoint(), numStates);
|
|
||||||
for (int i = 0; i < numStates; ++i) {
|
|
||||||
const JointState& state = _jointStates.at(i);
|
|
||||||
// _ragdollPoints start in model-frame
|
|
||||||
_ragdollPoints[i].initPosition(state.getPosition());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonModel::buildRagdollConstraints() {
|
|
||||||
// NOTE: the length of DistanceConstraints is computed and locked in at this time
|
|
||||||
// so make sure the ragdoll positions are in a normal configuration before here.
|
|
||||||
const int numPoints = _ragdollPoints.size();
|
|
||||||
assert(numPoints == _jointStates.size());
|
|
||||||
|
|
||||||
float minBone = FLT_MAX;
|
|
||||||
float maxBone = -FLT_MAX;
|
|
||||||
QMultiMap<int, int> families;
|
|
||||||
for (int i = 0; i < numPoints; ++i) {
|
|
||||||
const JointState& state = _jointStates.at(i);
|
|
||||||
int parentIndex = state.getParentIndex();
|
|
||||||
if (parentIndex == -1) {
|
|
||||||
FixedConstraint* anchor = new FixedConstraint(&_translationInSimulationFrame, &(_ragdollPoints[i]));
|
|
||||||
_fixedConstraints.push_back(anchor);
|
|
||||||
} else {
|
|
||||||
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[i]), &(_ragdollPoints[parentIndex]));
|
|
||||||
bone->setDistance(state.getDistanceToParent());
|
|
||||||
_boneConstraints.push_back(bone);
|
|
||||||
families.insert(parentIndex, i);
|
|
||||||
}
|
|
||||||
float boneLength = glm::length(state.getPositionInParentFrame());
|
|
||||||
if (boneLength > maxBone) {
|
|
||||||
maxBone = boneLength;
|
|
||||||
} else if (boneLength < minBone) {
|
|
||||||
minBone = boneLength;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Joints that have multiple children effectively have rigid constraints between the children
|
|
||||||
// in the parent frame, so we add DistanceConstraints between children in the same family.
|
|
||||||
QMultiMap<int, int>::iterator itr = families.begin();
|
|
||||||
while (itr != families.end()) {
|
|
||||||
QList<int> children = families.values(itr.key());
|
|
||||||
int numChildren = children.size();
|
|
||||||
if (numChildren > 1) {
|
|
||||||
for (int i = 1; i < numChildren; ++i) {
|
|
||||||
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[children[i-1]]), &(_ragdollPoints[children[i]]));
|
|
||||||
_boneConstraints.push_back(bone);
|
|
||||||
}
|
|
||||||
if (numChildren > 2) {
|
|
||||||
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[children[numChildren-1]]), &(_ragdollPoints[children[0]]));
|
|
||||||
_boneConstraints.push_back(bone);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
++itr;
|
|
||||||
}
|
|
||||||
|
|
||||||
float MAX_STRENGTH = 0.6f;
|
|
||||||
float MIN_STRENGTH = 0.05f;
|
|
||||||
// each joint gets a MuscleConstraint to its parent
|
|
||||||
for (int i = 1; i < numPoints; ++i) {
|
|
||||||
const JointState& state = _jointStates.at(i);
|
|
||||||
int p = state.getParentIndex();
|
|
||||||
if (p == -1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
MuscleConstraint* constraint = new MuscleConstraint(&(_ragdollPoints[p]), &(_ragdollPoints[i]));
|
|
||||||
_muscleConstraints.push_back(constraint);
|
|
||||||
|
|
||||||
// Short joints are more susceptible to wiggle so we modulate the strength based on the joint's length:
|
|
||||||
// long = weak and short = strong.
|
|
||||||
constraint->setIndices(p, i);
|
|
||||||
float boneLength = glm::length(state.getPositionInParentFrame());
|
|
||||||
|
|
||||||
float strength = MIN_STRENGTH + (MAX_STRENGTH - MIN_STRENGTH) * (maxBone - boneLength) / (maxBone - minBone);
|
|
||||||
if (!families.contains(i)) {
|
|
||||||
// Although muscles only pull on the children not parents, nevertheless those joints that have
|
|
||||||
// parents AND children are more stable than joints at the end such as fingers. For such joints we
|
|
||||||
// bestow maximum strength which helps reduce wiggle.
|
|
||||||
strength = MAX_MUSCLE_STRENGTH;
|
|
||||||
}
|
|
||||||
constraint->setStrength(strength);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonModel::updateVisibleJointStates() {
|
void SkeletonModel::updateVisibleJointStates() {
|
||||||
if (_showTrueJointTransforms) {
|
if (_showTrueJointTransforms || !_ragdoll) {
|
||||||
// no need to update visible transforms
|
// no need to update visible transforms
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const QVector<VerletPoint>& ragdollPoints = _ragdoll->getRagdollPoints();
|
||||||
QVector<glm::vec3> points;
|
QVector<glm::vec3> points;
|
||||||
points.reserve(_jointStates.size());
|
points.reserve(_jointStates.size());
|
||||||
glm::quat invRotation = glm::inverse(_rotation);
|
glm::quat invRotation = glm::inverse(_rotation);
|
||||||
for (int i = 0; i < _jointStates.size(); i++) {
|
for (int i = 0; i < _jointStates.size(); i++) {
|
||||||
JointState& state = _jointStates[i];
|
JointState& state = _jointStates[i];
|
||||||
points.push_back(_ragdollPoints[i]._position);
|
points.push_back(ragdollPoints[i]._position);
|
||||||
|
|
||||||
// get the parent state (this is the state that we want to rotate)
|
// get the parent state (this is the state that we want to rotate)
|
||||||
int parentIndex = state.getParentIndex();
|
int parentIndex = state.getParentIndex();
|
||||||
|
@ -653,15 +571,14 @@ void SkeletonModel::updateVisibleJointStates() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// virtual
|
SkeletonRagdoll* SkeletonModel::buildRagdoll() {
|
||||||
void SkeletonModel::stepRagdollForward(float deltaTime) {
|
if (!_ragdoll) {
|
||||||
setRagdollTransform(_translation, _rotation);
|
_ragdoll = new SkeletonRagdoll(this);
|
||||||
Ragdoll::stepRagdollForward(deltaTime);
|
if (_enableShapes) {
|
||||||
updateMuscles();
|
buildShapes();
|
||||||
int numConstraints = _muscleConstraints.size();
|
}
|
||||||
for (int i = 0; i < numConstraints; ++i) {
|
|
||||||
_muscleConstraints[i]->enforce();
|
|
||||||
}
|
}
|
||||||
|
return _ragdoll;
|
||||||
}
|
}
|
||||||
|
|
||||||
float DENSITY_OF_WATER = 1000.0f; // kg/m^3
|
float DENSITY_OF_WATER = 1000.0f; // kg/m^3
|
||||||
|
@ -679,8 +596,13 @@ void SkeletonModel::buildShapes() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
initRagdollPoints();
|
if (!_ragdoll) {
|
||||||
float massScale = getMassScale();
|
_ragdoll = new SkeletonRagdoll(this);
|
||||||
|
}
|
||||||
|
_ragdoll->initRagdollPoints();
|
||||||
|
QVector<VerletPoint>& points = _ragdoll->getRagdollPoints();
|
||||||
|
|
||||||
|
float massScale = _ragdoll->getMassScale();
|
||||||
|
|
||||||
float uniformScale = extractUniformScale(_scale);
|
float uniformScale = extractUniformScale(_scale);
|
||||||
const int numStates = _jointStates.size();
|
const int numStates = _jointStates.size();
|
||||||
|
@ -700,14 +622,14 @@ void SkeletonModel::buildShapes() {
|
||||||
Shape* shape = NULL;
|
Shape* shape = NULL;
|
||||||
int parentIndex = joint.parentIndex;
|
int parentIndex = joint.parentIndex;
|
||||||
if (type == Shape::SPHERE_SHAPE) {
|
if (type == Shape::SPHERE_SHAPE) {
|
||||||
shape = new VerletSphereShape(radius, &(_ragdollPoints[i]));
|
shape = new VerletSphereShape(radius, &(points[i]));
|
||||||
shape->setEntity(this);
|
shape->setEntity(this);
|
||||||
_ragdollPoints[i].setMass(massScale * glm::max(MIN_JOINT_MASS, DENSITY_OF_WATER * shape->getVolume()));
|
points[i].setMass(massScale * glm::max(MIN_JOINT_MASS, DENSITY_OF_WATER * shape->getVolume()));
|
||||||
} else if (type == Shape::CAPSULE_SHAPE) {
|
} else if (type == Shape::CAPSULE_SHAPE) {
|
||||||
assert(parentIndex != -1);
|
assert(parentIndex != -1);
|
||||||
shape = new VerletCapsuleShape(radius, &(_ragdollPoints[parentIndex]), &(_ragdollPoints[i]));
|
shape = new VerletCapsuleShape(radius, &(points[parentIndex]), &(points[i]));
|
||||||
shape->setEntity(this);
|
shape->setEntity(this);
|
||||||
_ragdollPoints[i].setMass(massScale * glm::max(MIN_JOINT_MASS, DENSITY_OF_WATER * shape->getVolume()));
|
points[i].setMass(massScale * glm::max(MIN_JOINT_MASS, DENSITY_OF_WATER * shape->getVolume()));
|
||||||
}
|
}
|
||||||
if (parentIndex != -1) {
|
if (parentIndex != -1) {
|
||||||
// always disable collisions between joint and its parent
|
// always disable collisions between joint and its parent
|
||||||
|
@ -717,7 +639,7 @@ void SkeletonModel::buildShapes() {
|
||||||
} else {
|
} else {
|
||||||
// give the base joint a very large mass since it doesn't actually move
|
// give the base joint a very large mass since it doesn't actually move
|
||||||
// in the local-frame simulation (it defines the origin)
|
// in the local-frame simulation (it defines the origin)
|
||||||
_ragdollPoints[i].setMass(VERY_BIG_MASS);
|
points[i].setMass(VERY_BIG_MASS);
|
||||||
}
|
}
|
||||||
_shapes.push_back(shape);
|
_shapes.push_back(shape);
|
||||||
}
|
}
|
||||||
|
@ -729,17 +651,11 @@ void SkeletonModel::buildShapes() {
|
||||||
// joints that are currently colliding.
|
// joints that are currently colliding.
|
||||||
disableCurrentSelfCollisions();
|
disableCurrentSelfCollisions();
|
||||||
|
|
||||||
buildRagdollConstraints();
|
_ragdoll->buildRagdollConstraints();
|
||||||
|
|
||||||
// ... then move shapes back to current joint positions
|
// ... then move shapes back to current joint positions
|
||||||
if (_ragdollPoints.size() == numStates) {
|
_ragdoll->slamPointPositions();
|
||||||
int numStates = _jointStates.size();
|
_ragdoll->enforceRagdollConstraints();
|
||||||
for (int i = 0; i < numStates; ++i) {
|
|
||||||
// ragdollPoints start in model-frame
|
|
||||||
_ragdollPoints[i].initPosition(_jointStates.at(i).getPosition());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
enforceRagdollConstraints();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonModel::moveShapesTowardJoints(float deltaTime) {
|
void SkeletonModel::moveShapesTowardJoints(float deltaTime) {
|
||||||
|
@ -747,8 +663,9 @@ void SkeletonModel::moveShapesTowardJoints(float deltaTime) {
|
||||||
// unravel a skelton that has become tangled in its constraints. So let's keep this
|
// unravel a skelton that has become tangled in its constraints. So let's keep this
|
||||||
// around for a while just in case.
|
// around for a while just in case.
|
||||||
const int numStates = _jointStates.size();
|
const int numStates = _jointStates.size();
|
||||||
assert(_jointStates.size() == _ragdollPoints.size());
|
QVector<VerletPoint>& ragdollPoints = _ragdoll->getRagdollPoints();
|
||||||
if (_ragdollPoints.size() != numStates) {
|
assert(_jointStates.size() == ragdollPoints.size());
|
||||||
|
if (ragdollPoints.size() != numStates) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -757,32 +674,22 @@ void SkeletonModel::moveShapesTowardJoints(float deltaTime) {
|
||||||
float fraction = glm::clamp(deltaTime / RAGDOLL_FOLLOWS_JOINTS_TIMESCALE, 0.0f, 1.0f);
|
float fraction = glm::clamp(deltaTime / RAGDOLL_FOLLOWS_JOINTS_TIMESCALE, 0.0f, 1.0f);
|
||||||
|
|
||||||
float oneMinusFraction = 1.0f - fraction;
|
float oneMinusFraction = 1.0f - fraction;
|
||||||
glm::vec3 simulationTranslation = getTranslationInSimulationFrame();
|
glm::vec3 simulationTranslation = _ragdoll->getTranslationInSimulationFrame();
|
||||||
for (int i = 0; i < numStates; ++i) {
|
for (int i = 0; i < numStates; ++i) {
|
||||||
// ragdollPoints are in simulation-frame but jointStates are in model-frame
|
// ragdollPoints are in simulation-frame but jointStates are in model-frame
|
||||||
_ragdollPoints[i].initPosition(oneMinusFraction * _ragdollPoints[i]._position +
|
ragdollPoints[i].initPosition(oneMinusFraction * ragdollPoints[i]._position +
|
||||||
fraction * (simulationTranslation + _rotation * (_jointStates.at(i).getPosition())));
|
fraction * (simulationTranslation + _rotation * (_jointStates.at(i).getPosition())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonModel::updateMuscles() {
|
|
||||||
int numConstraints = _muscleConstraints.size();
|
|
||||||
for (int i = 0; i < numConstraints; ++i) {
|
|
||||||
MuscleConstraint* constraint = _muscleConstraints[i];
|
|
||||||
int j = constraint->getParentIndex();
|
|
||||||
int k = constraint->getChildIndex();
|
|
||||||
assert(j != -1 && k != -1);
|
|
||||||
// ragdollPoints are in simulation-frame but jointStates are in model-frame
|
|
||||||
constraint->setChildOffset(_rotation * (_jointStates.at(k).getPosition() - _jointStates.at(j).getPosition()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
|
void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
|
||||||
// compute default joint transforms
|
// compute default joint transforms
|
||||||
int numStates = _jointStates.size();
|
int numStates = _jointStates.size();
|
||||||
QVector<glm::mat4> transforms;
|
QVector<glm::mat4> transforms;
|
||||||
transforms.fill(glm::mat4(), numStates);
|
transforms.fill(glm::mat4(), numStates);
|
||||||
|
|
||||||
|
QVector<VerletPoint>& ragdollPoints = _ragdoll->getRagdollPoints();
|
||||||
|
|
||||||
// compute the default transforms and slam the ragdoll positions accordingly
|
// compute the default transforms and slam the ragdoll positions accordingly
|
||||||
// (which puts the shapes where we want them)
|
// (which puts the shapes where we want them)
|
||||||
for (int i = 0; i < numStates; i++) {
|
for (int i = 0; i < numStates; i++) {
|
||||||
|
@ -791,7 +698,7 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
|
||||||
int parentIndex = joint.parentIndex;
|
int parentIndex = joint.parentIndex;
|
||||||
if (parentIndex == -1) {
|
if (parentIndex == -1) {
|
||||||
transforms[i] = _jointStates[i].getTransform();
|
transforms[i] = _jointStates[i].getTransform();
|
||||||
_ragdollPoints[i].initPosition(extractTranslation(transforms[i]));
|
ragdollPoints[i].initPosition(extractTranslation(transforms[i]));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -799,7 +706,7 @@ void SkeletonModel::computeBoundingShape(const FBXGeometry& geometry) {
|
||||||
transforms[i] = transforms[parentIndex] * glm::translate(joint.translation)
|
transforms[i] = transforms[parentIndex] * glm::translate(joint.translation)
|
||||||
* joint.preTransform * glm::mat4_cast(modifiedRotation) * joint.postTransform;
|
* joint.preTransform * glm::mat4_cast(modifiedRotation) * joint.postTransform;
|
||||||
// setting the ragdollPoints here slams the VerletShapes into their default positions
|
// setting the ragdollPoints here slams the VerletShapes into their default positions
|
||||||
_ragdollPoints[i].initPosition(extractTranslation(transforms[i]));
|
ragdollPoints[i].initPosition(extractTranslation(transforms[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute bounding box that encloses all shapes
|
// compute bounding box that encloses all shapes
|
||||||
|
@ -918,9 +825,12 @@ const int BALL_SUBDIVISIONS = 10;
|
||||||
|
|
||||||
// virtual
|
// virtual
|
||||||
void SkeletonModel::renderJointCollisionShapes(float alpha) {
|
void SkeletonModel::renderJointCollisionShapes(float alpha) {
|
||||||
|
if (!_ragdoll) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
Application::getInstance()->loadTranslatedViewMatrix(_translation);
|
Application::getInstance()->loadTranslatedViewMatrix(_translation);
|
||||||
glm::vec3 simulationTranslation = getTranslationInSimulationFrame();
|
glm::vec3 simulationTranslation = _ragdoll->getTranslationInSimulationFrame();
|
||||||
for (int i = 0; i < _shapes.size(); i++) {
|
for (int i = 0; i < _shapes.size(); i++) {
|
||||||
Shape* shape = _shapes[i];
|
Shape* shape = _shapes[i];
|
||||||
if (!shape) {
|
if (!shape) {
|
||||||
|
|
|
@ -15,18 +15,20 @@
|
||||||
#include "renderer/Model.h"
|
#include "renderer/Model.h"
|
||||||
|
|
||||||
#include <CapsuleShape.h>
|
#include <CapsuleShape.h>
|
||||||
#include <Ragdoll.h>
|
#include "SkeletonRagdoll.h"
|
||||||
|
|
||||||
class Avatar;
|
class Avatar;
|
||||||
class MuscleConstraint;
|
class MuscleConstraint;
|
||||||
|
class SkeletonRagdoll;
|
||||||
|
|
||||||
/// A skeleton loaded from a model.
|
/// A skeleton loaded from a model.
|
||||||
class SkeletonModel : public Model, public Ragdoll {
|
class SkeletonModel : public Model {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SkeletonModel(Avatar* owningAvatar, QObject* parent = NULL);
|
SkeletonModel(Avatar* owningAvatar, QObject* parent = NULL);
|
||||||
|
~SkeletonModel();
|
||||||
|
|
||||||
void setJointStates(QVector<JointState> states);
|
void setJointStates(QVector<JointState> states);
|
||||||
|
|
||||||
|
@ -96,12 +98,11 @@ public:
|
||||||
bool getEyePositions(glm::vec3& firstEyePosition, glm::vec3& secondEyePosition) const;
|
bool getEyePositions(glm::vec3& firstEyePosition, glm::vec3& secondEyePosition) const;
|
||||||
|
|
||||||
virtual void updateVisibleJointStates();
|
virtual void updateVisibleJointStates();
|
||||||
|
|
||||||
// virtual overrride from Ragdoll
|
|
||||||
virtual void stepRagdollForward(float deltaTime);
|
|
||||||
|
|
||||||
|
SkeletonRagdoll* buildRagdoll();
|
||||||
|
SkeletonRagdoll* getRagdoll() { return _ragdoll; }
|
||||||
|
|
||||||
void moveShapesTowardJoints(float fraction);
|
void moveShapesTowardJoints(float fraction);
|
||||||
void updateMuscles();
|
|
||||||
|
|
||||||
void computeBoundingShape(const FBXGeometry& geometry);
|
void computeBoundingShape(const FBXGeometry& geometry);
|
||||||
void renderBoundingCollisionShapes(float alpha);
|
void renderBoundingCollisionShapes(float alpha);
|
||||||
|
@ -115,10 +116,6 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
// virtual overrrides from Ragdoll
|
|
||||||
void initRagdollPoints();
|
|
||||||
void buildRagdollConstraints();
|
|
||||||
|
|
||||||
void buildShapes();
|
void buildShapes();
|
||||||
|
|
||||||
/// \param jointIndex index of joint in model
|
/// \param jointIndex index of joint in model
|
||||||
|
@ -147,7 +144,7 @@ private:
|
||||||
|
|
||||||
CapsuleShape _boundingShape;
|
CapsuleShape _boundingShape;
|
||||||
glm::vec3 _boundingShapeLocalOffset;
|
glm::vec3 _boundingShapeLocalOffset;
|
||||||
QVector<MuscleConstraint*> _muscleConstraints;
|
SkeletonRagdoll* _ragdoll;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_SkeletonModel_h
|
#endif // hifi_SkeletonModel_h
|
||||||
|
|
148
interface/src/avatar/SkeletonRagdoll.cpp
Normal file
148
interface/src/avatar/SkeletonRagdoll.cpp
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
//
|
||||||
|
// SkeletonRagdoll.cpp
|
||||||
|
// interface/src/avatar
|
||||||
|
//
|
||||||
|
// Created by Andrew Meadows 2014.08.14
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <DistanceConstraint.h>
|
||||||
|
#include <FixedConstraint.h>
|
||||||
|
|
||||||
|
#include "SkeletonRagdoll.h"
|
||||||
|
#include "MuscleConstraint.h"
|
||||||
|
#include "../renderer/Model.h"
|
||||||
|
|
||||||
|
SkeletonRagdoll::SkeletonRagdoll(Model* model) : Ragdoll(), _model(model) {
|
||||||
|
assert(_model);
|
||||||
|
}
|
||||||
|
|
||||||
|
SkeletonRagdoll::~SkeletonRagdoll() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtual
|
||||||
|
void SkeletonRagdoll::stepRagdollForward(float deltaTime) {
|
||||||
|
setRagdollTransform(_model->getTranslation(), _model->getRotation());
|
||||||
|
Ragdoll::stepRagdollForward(deltaTime);
|
||||||
|
updateMuscles();
|
||||||
|
int numConstraints = _muscleConstraints.size();
|
||||||
|
for (int i = 0; i < numConstraints; ++i) {
|
||||||
|
_muscleConstraints[i]->enforce();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkeletonRagdoll::slamPointPositions() {
|
||||||
|
QVector<JointState>& jointStates = _model->getJointStates();
|
||||||
|
int numStates = jointStates.size();
|
||||||
|
for (int i = 0; i < numStates; ++i) {
|
||||||
|
_ragdollPoints[i].initPosition(jointStates.at(i).getPosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtual
|
||||||
|
void SkeletonRagdoll::initRagdollPoints() {
|
||||||
|
clearRagdollConstraintsAndPoints();
|
||||||
|
_muscleConstraints.clear();
|
||||||
|
|
||||||
|
initRagdollTransform();
|
||||||
|
// one point for each joint
|
||||||
|
QVector<JointState>& jointStates = _model->getJointStates();
|
||||||
|
int numStates = jointStates.size();
|
||||||
|
_ragdollPoints.fill(VerletPoint(), numStates);
|
||||||
|
slamPointPositions();
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtual
|
||||||
|
void SkeletonRagdoll::buildRagdollConstraints() {
|
||||||
|
QVector<JointState>& jointStates = _model->getJointStates();
|
||||||
|
|
||||||
|
// NOTE: the length of DistanceConstraints is computed and locked in at this time
|
||||||
|
// so make sure the ragdoll positions are in a normal configuration before here.
|
||||||
|
const int numPoints = _ragdollPoints.size();
|
||||||
|
assert(numPoints == jointStates.size());
|
||||||
|
|
||||||
|
float minBone = FLT_MAX;
|
||||||
|
float maxBone = -FLT_MAX;
|
||||||
|
QMultiMap<int, int> families;
|
||||||
|
for (int i = 0; i < numPoints; ++i) {
|
||||||
|
const JointState& state = jointStates.at(i);
|
||||||
|
int parentIndex = state.getParentIndex();
|
||||||
|
if (parentIndex == -1) {
|
||||||
|
FixedConstraint* anchor = new FixedConstraint(&_translationInSimulationFrame, &(_ragdollPoints[i]));
|
||||||
|
_fixedConstraints.push_back(anchor);
|
||||||
|
} else {
|
||||||
|
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[i]), &(_ragdollPoints[parentIndex]));
|
||||||
|
bone->setDistance(state.getDistanceToParent());
|
||||||
|
_boneConstraints.push_back(bone);
|
||||||
|
families.insert(parentIndex, i);
|
||||||
|
}
|
||||||
|
float boneLength = glm::length(state.getPositionInParentFrame());
|
||||||
|
if (boneLength > maxBone) {
|
||||||
|
maxBone = boneLength;
|
||||||
|
} else if (boneLength < minBone) {
|
||||||
|
minBone = boneLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Joints that have multiple children effectively have rigid constraints between the children
|
||||||
|
// in the parent frame, so we add DistanceConstraints between children in the same family.
|
||||||
|
QMultiMap<int, int>::iterator itr = families.begin();
|
||||||
|
while (itr != families.end()) {
|
||||||
|
QList<int> children = families.values(itr.key());
|
||||||
|
int numChildren = children.size();
|
||||||
|
if (numChildren > 1) {
|
||||||
|
for (int i = 1; i < numChildren; ++i) {
|
||||||
|
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[children[i-1]]), &(_ragdollPoints[children[i]]));
|
||||||
|
_boneConstraints.push_back(bone);
|
||||||
|
}
|
||||||
|
if (numChildren > 2) {
|
||||||
|
DistanceConstraint* bone = new DistanceConstraint(&(_ragdollPoints[children[numChildren-1]]), &(_ragdollPoints[children[0]]));
|
||||||
|
_boneConstraints.push_back(bone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++itr;
|
||||||
|
}
|
||||||
|
|
||||||
|
float MAX_STRENGTH = 0.6f;
|
||||||
|
float MIN_STRENGTH = 0.05f;
|
||||||
|
// each joint gets a MuscleConstraint to its parent
|
||||||
|
for (int i = 1; i < numPoints; ++i) {
|
||||||
|
const JointState& state = jointStates.at(i);
|
||||||
|
int p = state.getParentIndex();
|
||||||
|
if (p == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
MuscleConstraint* constraint = new MuscleConstraint(&(_ragdollPoints[p]), &(_ragdollPoints[i]));
|
||||||
|
_muscleConstraints.push_back(constraint);
|
||||||
|
|
||||||
|
// Short joints are more susceptible to wiggle so we modulate the strength based on the joint's length:
|
||||||
|
// long = weak and short = strong.
|
||||||
|
constraint->setIndices(p, i);
|
||||||
|
float boneLength = glm::length(state.getPositionInParentFrame());
|
||||||
|
|
||||||
|
float strength = MIN_STRENGTH + (MAX_STRENGTH - MIN_STRENGTH) * (maxBone - boneLength) / (maxBone - minBone);
|
||||||
|
if (!families.contains(i)) {
|
||||||
|
// Although muscles only pull on the children not parents, nevertheless those joints that have
|
||||||
|
// parents AND children are more stable than joints at the end such as fingers. For such joints we
|
||||||
|
// bestow maximum strength which helps reduce wiggle.
|
||||||
|
strength = MAX_MUSCLE_STRENGTH;
|
||||||
|
}
|
||||||
|
constraint->setStrength(strength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkeletonRagdoll::updateMuscles() {
|
||||||
|
QVector<JointState>& jointStates = _model->getJointStates();
|
||||||
|
int numConstraints = _muscleConstraints.size();
|
||||||
|
glm::quat rotation = _model->getRotation();
|
||||||
|
for (int i = 0; i < numConstraints; ++i) {
|
||||||
|
MuscleConstraint* constraint = _muscleConstraints[i];
|
||||||
|
int j = constraint->getParentIndex();
|
||||||
|
int k = constraint->getChildIndex();
|
||||||
|
assert(j != -1 && k != -1);
|
||||||
|
// ragdollPoints are in simulation-frame but jointStates are in model-frame
|
||||||
|
constraint->setChildOffset(rotation * (jointStates.at(k).getPosition() - jointStates.at(j).getPosition()));
|
||||||
|
}
|
||||||
|
}
|
42
interface/src/avatar/SkeletonRagdoll.h
Normal file
42
interface/src/avatar/SkeletonRagdoll.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
//
|
||||||
|
// SkeletonkRagdoll.h
|
||||||
|
// interface/src/avatar
|
||||||
|
//
|
||||||
|
// Created by Andrew Meadows 2014.08.14
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_SkeletonRagdoll_h
|
||||||
|
#define hifi_SkeletonRagdoll_h
|
||||||
|
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
|
#include <Ragdoll.h>
|
||||||
|
|
||||||
|
#include "../renderer/JointState.h"
|
||||||
|
|
||||||
|
class MuscleConstraint;
|
||||||
|
class Model;
|
||||||
|
|
||||||
|
class SkeletonRagdoll : public Ragdoll {
|
||||||
|
public:
|
||||||
|
|
||||||
|
SkeletonRagdoll(Model* model);
|
||||||
|
virtual ~SkeletonRagdoll();
|
||||||
|
|
||||||
|
void slamPointPositions();
|
||||||
|
virtual void stepRagdollForward(float deltaTime);
|
||||||
|
|
||||||
|
virtual void initRagdollPoints();
|
||||||
|
virtual void buildRagdollConstraints();
|
||||||
|
|
||||||
|
void updateMuscles();
|
||||||
|
private:
|
||||||
|
Model* _model;
|
||||||
|
QVector<MuscleConstraint*> _muscleConstraints;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // hifi_SkeletonRagdoll_h
|
|
@ -164,6 +164,9 @@ public:
|
||||||
const QVector<LocalLight>& getLocalLights() const { return _localLights; }
|
const QVector<LocalLight>& getLocalLights() const { return _localLights; }
|
||||||
|
|
||||||
void setShowTrueJointTransforms(bool show) { _showTrueJointTransforms = show; }
|
void setShowTrueJointTransforms(bool show) { _showTrueJointTransforms = show; }
|
||||||
|
|
||||||
|
QVector<JointState>& getJointStates() { return _jointStates; }
|
||||||
|
const QVector<JointState>& getJointStates() const { return _jointStates; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QSharedPointer<NetworkGeometry> _geometry;
|
QSharedPointer<NetworkGeometry> _geometry;
|
||||||
|
|
|
@ -52,11 +52,11 @@ public:
|
||||||
void setMassScale(float scale);
|
void setMassScale(float scale);
|
||||||
float getMassScale() const { return _massScale; }
|
float getMassScale() const { return _massScale; }
|
||||||
|
|
||||||
protected:
|
|
||||||
void clearRagdollConstraintsAndPoints();
|
void clearRagdollConstraintsAndPoints();
|
||||||
virtual void initRagdollPoints() = 0;
|
virtual void initRagdollPoints() = 0;
|
||||||
virtual void buildRagdollConstraints() = 0;
|
virtual void buildRagdollConstraints() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
float _massScale;
|
float _massScale;
|
||||||
glm::vec3 _ragdollTranslation; // world-frame
|
glm::vec3 _ragdollTranslation; // world-frame
|
||||||
glm::quat _ragdollRotation; // world-frame
|
glm::quat _ragdollRotation; // world-frame
|
||||||
|
|
Loading…
Reference in a new issue