From fb7f5707c87a8ec573b4a32debf395c86fc9a1dc Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 12 Jun 2014 18:15:20 -0700 Subject: [PATCH] add SimulationEngine, MyAvatar has one. --- interface/src/avatar/MyAvatar.cpp | 3 +- interface/src/avatar/MyAvatar.h | 3 + libraries/shared/src/SimulationEngine.cpp | 77 +++++++++++++++++++++++ libraries/shared/src/SimulationEngine.h | 44 +++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 libraries/shared/src/SimulationEngine.cpp create mode 100644 libraries/shared/src/SimulationEngine.h diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index bebe2fbec7..842308ecbb 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -76,7 +76,8 @@ MyAvatar::MyAvatar() : _lastFloorContactPoint(0.0f), _lookAtTargetAvatar(), _shouldRender(true), - _billboardValid(false) + _billboardValid(false), + _simulationEngine() { for (int i = 0; i < MAX_DRIVE_KEYS; i++) { _driveKeys[i] = 0.0f; diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index d99102c356..b2b6dee7fd 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -14,6 +14,8 @@ #include +#include + #include "Avatar.h" enum AvatarHandState @@ -172,6 +174,7 @@ private: float _oculusYawOffset; QList _animationHandles; + SimulationEngine _simulationEngine; // private methods float computeDistanceToFloor(const glm::vec3& startPoint); diff --git a/libraries/shared/src/SimulationEngine.cpp b/libraries/shared/src/SimulationEngine.cpp new file mode 100644 index 0000000000..e21364b17c --- /dev/null +++ b/libraries/shared/src/SimulationEngine.cpp @@ -0,0 +1,77 @@ +// +// SimulationEngine.cpp +// interface/src/avatar +// +// Created by Andrew Meadows 2014.06.06 +// 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 + +#include "SimulationEngine.h" + +int MAX_DOLLS_PER_ENGINE = 32; +int MAX_COLLISIONS_PER_ENGINE = 256; + +const int NUM_SHAPE_BITS = 6; +const int SHAPE_INDEX_MASK = (1 << (NUM_SHAPE_BITS + 1)) - 1; + +SimulationEngine::SimulationEngine() : _collisionList(MAX_COLLISIONS_PER_ENGINE) { +} + +SimulationEngine::~SimulationEngine() { + _dolls.clear(); +} + +bool SimulationEngine::addRagDoll(RagDoll* doll) { + int numDolls = _dolls.size(); + if (numDolls < MAX_DOLLS_PER_ENGINE) { + for (int i = 0; i < numDolls; ++i) { + if (doll == _dolls[i]) { + // already in list + return true; + } + } + _dolls.push_back(doll); + return true; + } + return false; +} + +void SimulationEngine::removeRagDoll(RagDoll* doll) { + int numDolls = _dolls.size(); + for (int i = 0; i < numDolls; ++i) { + if (doll == _dolls[i]) { + if (i == numDolls - 1) { + // remove it + _dolls.pop_back(); + } else { + // swap the last for this one + RagDoll* lastDoll = _dolls[numDolls - 1]; + _dolls.pop_back(); + _dolls[i] = lastDoll; + } + break; + } + } +} + +float SimulationEngine::enforceConstraints() { + float maxMovement = 0.0f; + int numDolls = _dolls.size(); + for (int i = 0; i < numDolls; ++i) { + maxMovement = glm::max(maxMovement, _dolls[i]->enforceConstraints()); + } + return maxMovement; +} + +int SimulationEngine::computeCollisions() { + return 0.0f; +} + +void SimulationEngine::processCollisions() { +} + diff --git a/libraries/shared/src/SimulationEngine.h b/libraries/shared/src/SimulationEngine.h new file mode 100644 index 0000000000..6b6a225542 --- /dev/null +++ b/libraries/shared/src/SimulationEngine.h @@ -0,0 +1,44 @@ +// +// SimulationEngine.h +// interface/src/avatar +// +// Created by Andrew Meadows 2014.06.06 +// 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_SimulationEngine_h +#define hifi_SimulationEngine_h + +#include + +#include "CollisionInfo.h" +#include "RagDoll.h" + +class SimulationEngine { +public: + + SimulationEngine(); + ~SimulationEngine(); + + /// \return true if doll was added to, or already in the list + bool addRagDoll(RagDoll* doll); + + void removeRagDoll(RagDoll* doll); + + /// \return distance of largest movement + float enforceConstraints(); + + /// \return number of collisions + int computeCollisions(); + + void processCollisions(); + +private: + CollisionList _collisionList; + QVector _dolls; +}; + +#endif // hifi_SimulationEngine_h