From f86c41998827dd64435c7d1fce48b94f9eff5b0c Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 19 May 2015 14:15:26 -0700 Subject: [PATCH] stubbery for collidable avatars --- interface/src/Application.cpp | 9 ++ interface/src/avatar/Avatar.h | 6 +- interface/src/avatar/AvatarManager.cpp | 18 +++ interface/src/avatar/AvatarManager.h | 13 +- interface/src/avatar/AvatarMotionState.cpp | 116 ++++++++++++++++++ interface/src/avatar/AvatarMotionState.h | 67 ++++++++++ libraries/avatars/src/AvatarData.h | 4 +- libraries/physics/src/EntityMotionState.h | 2 +- libraries/physics/src/ObjectMotionState.h | 8 +- .../physics/src/PhysicalEntitySimulation.cpp | 1 - .../physics/src/PhysicalEntitySimulation.h | 3 +- libraries/physics/src/PhysicsEngine.h | 4 +- libraries/physics/src/PhysicsTypedefs.h | 23 ---- .../physics/src/ThreadSafeDynamicsWorld.cpp | 1 - .../physics/src/ThreadSafeDynamicsWorld.h | 2 +- 15 files changed, 238 insertions(+), 39 deletions(-) create mode 100644 interface/src/avatar/AvatarMotionState.cpp create mode 100644 interface/src/avatar/AvatarMotionState.h delete mode 100644 libraries/physics/src/PhysicsTypedefs.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 37ee116f40..3a242a28e0 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2456,6 +2456,11 @@ void Application::update(float deltaTime) { _physicsEngine.changeObjects(_entitySimulation.getObjectsToChange()); _entitySimulation.unlock(); + AvatarManager* avatarManager = DependencyManager::get().data(); + _physicsEngine.deleteObjects(avatarManager->getObjectsToDelete()); + _physicsEngine.addObjects(avatarManager->getObjectsToAdd()); + _physicsEngine.changeObjects(avatarManager->getObjectsToChange()); + _physicsEngine.stepSimulation(); if (_physicsEngine.hasOutgoingChanges()) { @@ -2463,6 +2468,10 @@ void Application::update(float deltaTime) { _entitySimulation.handleOutgoingChanges(_physicsEngine.getOutgoingChanges(), _physicsEngine.getSessionID()); _entitySimulation.handleCollisionEvents(_physicsEngine.getCollisionEvents()); _entitySimulation.unlock(); + + avatarManager->handleOutgoingChanges(_physicsEngine.getOutgoingChanges()); + avatarManager->handleCollisionEvents(_physicsEngine.getCollisionEvents()); + _physicsEngine.dumpStatsIfNecessary(); } } diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index a358e7e58d..6dca1f4a7f 100644 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -146,9 +146,9 @@ public: Q_INVOKABLE glm::vec3 getNeckPosition() const; - Q_INVOKABLE glm::vec3 getAcceleration() const { return _acceleration; } - Q_INVOKABLE glm::vec3 getAngularVelocity() const { return _angularVelocity; } - Q_INVOKABLE glm::vec3 getAngularAcceleration() const { return _angularAcceleration; } + Q_INVOKABLE const glm::vec3& getAcceleration() const { return _acceleration; } + Q_INVOKABLE const glm::vec3& getAngularVelocity() const { return _angularVelocity; } + Q_INVOKABLE const glm::vec3& getAngularAcceleration() const { return _angularAcceleration; } /// Scales a world space position vector relative to the avatar position and scale diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index 38144dfe5f..ed1b211067 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -215,3 +215,21 @@ QVector AvatarManager::getLocalLights() const { return _localLights; } +VectorOfMotionStates& AvatarManager::getObjectsToDelete() { + return _tempMotionStates; +} + +VectorOfMotionStates& AvatarManager::getObjectsToAdd() { + return _tempMotionStates; +} + +VectorOfMotionStates& AvatarManager::getObjectsToChange() { + return _tempMotionStates; +} + +void AvatarManager::handleOutgoingChanges(VectorOfMotionStates& motionStates) { +} + +void AvatarManager::handleCollisionEvents(CollisionEvents& collisionEvents) { +} + diff --git a/interface/src/avatar/AvatarManager.h b/interface/src/avatar/AvatarManager.h index 3c7f7296fe..994b53f712 100644 --- a/interface/src/avatar/AvatarManager.h +++ b/interface/src/avatar/AvatarManager.h @@ -17,8 +17,10 @@ #include #include +#include #include "Avatar.h" +#include "AvatarMotionState.h" class MyAvatar; @@ -51,6 +53,12 @@ public: Q_INVOKABLE void setLocalLights(const QVector& localLights); Q_INVOKABLE QVector getLocalLights() const; + + VectorOfMotionStates& getObjectsToDelete(); + VectorOfMotionStates& getObjectsToAdd(); + VectorOfMotionStates& getObjectsToChange(); + void handleOutgoingChanges(VectorOfMotionStates& motionStates); + void handleCollisionEvents(CollisionEvents& collisionEvents); public slots: void setShouldShowReceiveStats(bool shouldShowReceiveStats) { _shouldShowReceiveStats = shouldShowReceiveStats; } @@ -62,7 +70,7 @@ private: void simulateAvatarFades(float deltaTime); void renderAvatarFades(const glm::vec3& cameraPosition, RenderArgs::RenderMode renderMode); - AvatarSharedPointer newSharedAvatar(); + virtual AvatarSharedPointer newSharedAvatar(); // virtual overrides AvatarHash::iterator erase(const AvatarHash::iterator& iterator); @@ -74,6 +82,9 @@ private: QVector _localLights; bool _shouldShowReceiveStats = false; + + VectorOfAvatarMotionStates _avatarMotionStates; + VectorOfMotionStates _tempMotionStates; }; Q_DECLARE_METATYPE(AvatarManager::LocalLight) diff --git a/interface/src/avatar/AvatarMotionState.cpp b/interface/src/avatar/AvatarMotionState.cpp new file mode 100644 index 0000000000..cc693ecb66 --- /dev/null +++ b/interface/src/avatar/AvatarMotionState.cpp @@ -0,0 +1,116 @@ +// +// AvatarMotionState.cpp +// interface/src/avatar/ +// +// Created by Andrew Meadows 2015.05.14 +// Copyright 2015 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 "Avatar.h" +#include "AvatarMotionState.h" + +AvatarMotionState::AvatarMotionState(Avatar* avatar, btCollisionShape* shape) : ObjectMotionState(shape), _avatar(avatar) { +} + +AvatarMotionState::~AvatarMotionState() { + _avatar = nullptr; +} + +// virtual +void AvatarMotionState::handleEasyChanges(uint32_t flags) { +} + +// virtual +void AvatarMotionState::handleHardAndEasyChanges(uint32_t flags, PhysicsEngine* engine) { +} + +// virtual +void AvatarMotionState::updateBodyMaterialProperties() { +} + +// virtual +void AvatarMotionState::updateBodyVelocities() { +} + +// virtual +uint32_t AvatarMotionState::getAndClearIncomingDirtyFlags() const { + return 0; +} + +// virtual +MotionType AvatarMotionState::computeObjectMotionType() const { + return _motionType; +} + +// virtual +void AvatarMotionState::computeObjectShapeInfo(ShapeInfo& shapeInfo) { +} + +// virtual +bool AvatarMotionState::isMoving() const { + return false; +} + +// These pure virtual methods must be implemented for each MotionState type +// and make it possible to implement more complicated methods in this base class. + +// virtual +float AvatarMotionState::getObjectRestitution() const { + return 0.5f; +} +// virtual +float AvatarMotionState::getObjectFriction() const { + return 0.5f; +} +// virtual +float AvatarMotionState::getObjectLinearDamping() const { + return 0.5f; +} +// virtual +float AvatarMotionState::getObjectAngularDamping() const { + return 0.5f; +} + +// virtual +glm::vec3 AvatarMotionState::getObjectPosition() const { + return glm::vec3(0.0f); +} +// virtual +glm::quat AvatarMotionState::getObjectRotation() const { + return _avatar->getOrientation(); +} +// virtual +const glm::vec3& AvatarMotionState::getObjectLinearVelocity() const { + return _avatar->getVelocity(); +} + +// virtual +const glm::vec3& AvatarMotionState::getObjectAngularVelocity() const { + return _avatar->getAngularVelocity(); +} + +// virtual +const glm::vec3& AvatarMotionState::getObjectGravity() const { + return _avatar->getAcceleration(); +} + +// virtual +const QUuid& AvatarMotionState::getObjectID() const { + return _avatar->getSessionUUID(); +} + +// virtual +QUuid AvatarMotionState::getSimulatorID() const { + return _avatar->getSessionUUID(); +} + +// virtual +void AvatarMotionState::bump() { +} + +// protected, virtual +void AvatarMotionState::setMotionType(MotionType motionType) { +} diff --git a/interface/src/avatar/AvatarMotionState.h b/interface/src/avatar/AvatarMotionState.h new file mode 100644 index 0000000000..ea5dc9ae63 --- /dev/null +++ b/interface/src/avatar/AvatarMotionState.h @@ -0,0 +1,67 @@ +// +// AvatarMotionState.h +// interface/src/avatar/ +// +// Created by Andrew Meadows 2015.05.14 +// Copyright 2015 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_AvatarMotionState_h +#define hifi_AvatarMotionState_h + +#include + +#include + +class Avatar; + +class AvatarMotionState : public ObjectMotionState { +public: + AvatarMotionState(Avatar* avatar, btCollisionShape* shape); + ~AvatarMotionState(); + + virtual void handleEasyChanges(uint32_t flags); + virtual void handleHardAndEasyChanges(uint32_t flags, PhysicsEngine* engine); + + virtual void updateBodyMaterialProperties(); + virtual void updateBodyVelocities(); + + virtual MotionType getMotionType() const { return _motionType; } + + virtual uint32_t getAndClearIncomingDirtyFlags() const = 0; + + virtual MotionType computeObjectMotionType() const = 0; + virtual void computeObjectShapeInfo(ShapeInfo& shapeInfo) = 0; + + virtual bool isMoving() const = 0; + + // These pure virtual methods must be implemented for each MotionState type + // and make it possible to implement more complicated methods in this base class. + + virtual float getObjectRestitution() const = 0; + virtual float getObjectFriction() const = 0; + virtual float getObjectLinearDamping() const = 0; + virtual float getObjectAngularDamping() const = 0; + + virtual glm::vec3 getObjectPosition() const = 0; + virtual glm::quat getObjectRotation() const = 0; + virtual const glm::vec3& getObjectLinearVelocity() const = 0; + virtual const glm::vec3& getObjectAngularVelocity() const = 0; + virtual const glm::vec3& getObjectGravity() const = 0; + + virtual const QUuid& getObjectID() const = 0; + + virtual QUuid getSimulatorID() const = 0; + virtual void bump() = 0; + +protected: + virtual void setMotionType(MotionType motionType); + Avatar* _avatar; +}; + +typedef QVector VectorOfAvatarMotionStates; + +#endif // hifi_AvatarMotionState_h diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 6a11f94cb6..8eacfc0ff7 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -301,8 +301,8 @@ public: int getReceiveRate() const; void setVelocity(const glm::vec3 velocity) { _velocity = velocity; } - Q_INVOKABLE glm::vec3 getVelocity() const { return _velocity; } - glm::vec3 getTargetVelocity() const { return _targetVelocity; } + Q_INVOKABLE const glm::vec3& getVelocity() const { return _velocity; } + const glm::vec3& getTargetVelocity() const { return _targetVelocity; } public slots: void sendAvatarDataPacket(); diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 83b89a5a29..6df3965623 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -62,7 +62,7 @@ public: virtual float getObjectAngularDamping() const { return _entity->getAngularDamping(); } virtual glm::vec3 getObjectPosition() const { return _entity->getPosition() - ObjectMotionState::getWorldOffset(); } - virtual const glm::quat& getObjectRotation() const { return _entity->getRotation(); } + virtual glm::quat getObjectRotation() const { return _entity->getRotation(); } virtual const glm::vec3& getObjectLinearVelocity() const { return _entity->getVelocity(); } virtual const glm::vec3& getObjectAngularVelocity() const { return _entity->getAngularVelocity(); } virtual const glm::vec3& getObjectGravity() const { return _entity->getGravity(); } diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index bfc9310ec6..0af051221d 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -15,6 +15,9 @@ #include #include +#include +#include + #include #include "ContactInfo.h" @@ -109,7 +112,7 @@ public: virtual float getObjectAngularDamping() const = 0; virtual glm::vec3 getObjectPosition() const = 0; - virtual const glm::quat& getObjectRotation() const = 0; + virtual glm::quat getObjectRotation() const = 0; virtual const glm::vec3& getObjectLinearVelocity() const = 0; virtual const glm::vec3& getObjectAngularVelocity() const = 0; virtual const glm::vec3& getObjectGravity() const = 0; @@ -137,4 +140,7 @@ protected: uint32_t _lastKinematicStep; }; +typedef QSet SetOfMotionStates; +typedef QVector VectorOfMotionStates; + #endif // hifi_ObjectMotionState_h diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index 3e43ab7454..4d4be27df9 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -9,7 +9,6 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "EntityMotionState.h" #include "PhysicalEntitySimulation.h" #include "PhysicsHelpers.h" #include "PhysicsLogging.h" diff --git a/libraries/physics/src/PhysicalEntitySimulation.h b/libraries/physics/src/PhysicalEntitySimulation.h index b3ee7af1e1..083f8f4212 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.h +++ b/libraries/physics/src/PhysicalEntitySimulation.h @@ -21,9 +21,8 @@ #include #include "PhysicsEngine.h" -#include "PhysicsTypedefs.h" +#include "EntityMotionState.h" -class EntityMotionState; class ShapeManager; typedef QSet SetOfEntityMotionStates; diff --git a/libraries/physics/src/PhysicsEngine.h b/libraries/physics/src/PhysicsEngine.h index d1dc5bcd79..9ff85c9f11 100644 --- a/libraries/physics/src/PhysicsEngine.h +++ b/libraries/physics/src/PhysicsEngine.h @@ -22,13 +22,11 @@ #include "BulletUtil.h" #include "ContactInfo.h" #include "DynamicCharacterController.h" -#include "PhysicsTypedefs.h" +#include "ObjectMotionState.h" #include "ThreadSafeDynamicsWorld.h" const float HALF_SIMULATION_EXTENT = 512.0f; // meters -class ObjectMotionState; - // simple class for keeping track of contacts class ContactKey { public: diff --git a/libraries/physics/src/PhysicsTypedefs.h b/libraries/physics/src/PhysicsTypedefs.h deleted file mode 100644 index 9d9685a758..0000000000 --- a/libraries/physics/src/PhysicsTypedefs.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// PhysicsTypedefs.h -// libraries/physcis/src -// -// Created by Andrew Meadows 2015.04.29 -// Copyright 2015 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_PhysicsTypedefs_h -#define hifi_PhysicsTypedefs_h - -#include -#include - -class ObjectMotionState; - -typedef QSet SetOfMotionStates; -typedef QVector VectorOfMotionStates; - -#endif //hifi_PhysicsTypedefs_h diff --git a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp index a2d3ee97d3..b59103339c 100644 --- a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp +++ b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp @@ -17,7 +17,6 @@ #include -#include "ObjectMotionState.h" #include "ThreadSafeDynamicsWorld.h" ThreadSafeDynamicsWorld::ThreadSafeDynamicsWorld( diff --git a/libraries/physics/src/ThreadSafeDynamicsWorld.h b/libraries/physics/src/ThreadSafeDynamicsWorld.h index 4a96eae311..eacc8ad74a 100644 --- a/libraries/physics/src/ThreadSafeDynamicsWorld.h +++ b/libraries/physics/src/ThreadSafeDynamicsWorld.h @@ -21,7 +21,7 @@ #include #include -#include "PhysicsTypedefs.h" +#include "ObjectMotionState.h" ATTRIBUTE_ALIGNED16(class) ThreadSafeDynamicsWorld : public btDiscreteDynamicsWorld { public: