From 30958e3ce61ee8127bbb22e2e9507b3752910156 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 22 May 2013 12:49:25 -0700 Subject: [PATCH 1/6] Working on reorientation. --- interface/src/Avatar.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index cdb9040d20..9e72e1ca90 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -6,6 +6,7 @@ // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. #include +#include #include #include #include @@ -324,7 +325,22 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { _bodyRoll *= tiltDecay; //the following will be used to make the avatar upright no matter what gravity is - //float f = angleBetween(_orientation.getUp(), _gravity); + float gravityLength = glm::length(_gravity); + if (gravityLength > 0.0f) { + glm::vec3 targetUp = _gravity / -gravityLength; + const glm::vec3& currentUp = _orientation.getUp(); + float angle = glm::degrees(acosf(glm::dot(currentUp, targetUp))); + if (angle > 0.0f) { + glm::vec3 axis; + if (angle > 180.0f - EPSILON) { // 180 degree rotation; must use another axis + axis = _orientation.getRight(); + } else { + axis = glm::normalize(glm::cross(currentUp, targetUp)); + } + _orientation.rotate(glm::angleAxis(angle, axis)); + + } + } // update position by velocity _position += _velocity * deltaTime; From 365bb10d6b8fdfcb082c56a36023bd25e83a692e Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 22 May 2013 13:30:30 -0700 Subject: [PATCH 2/6] More work on righting based on gravity vector. --- interface/src/Avatar.cpp | 8 ++++++-- libraries/avatars/src/Orientation.h | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index b1f7926973..fdcf3a009e 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -51,6 +51,7 @@ const float HEAD_MIN_YAW = -85; const float PERIPERSONAL_RADIUS = 1.0f; const float AVATAR_BRAKING_STRENGTH = 40.0f; const float JOINT_TOUCH_RANGE = 0.0005f; +const float ANGULAR_RIGHTING_SPEED = 45.0f; float skinColor [] = {1.0, 0.84, 0.66}; float darkSkinColor[] = {0.9, 0.78, 0.63}; @@ -348,8 +349,11 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { } else { axis = glm::normalize(glm::cross(currentUp, targetUp)); } - _orientation.rotate(glm::angleAxis(angle, axis)); - + _orientation.rotate(glm::angleAxis(min(deltaTime * ANGULAR_RIGHTING_SPEED, angle), axis)); + glm::vec3 eulerAngles = glm::eulerAngles(_orientation.getQuat()); + _bodyYaw = eulerAngles.y; + _bodyPitch = eulerAngles.x; + _bodyRoll = eulerAngles.z; } } diff --git a/libraries/avatars/src/Orientation.h b/libraries/avatars/src/Orientation.h index 82d6edc9e5..ecf5f47638 100755 --- a/libraries/avatars/src/Orientation.h +++ b/libraries/avatars/src/Orientation.h @@ -33,6 +33,8 @@ public: void rotate(glm::vec3 EulerAngles); void rotate(glm::quat quaternion); + const glm::quat & getQuat() const {return quat;} + const glm::vec3 & getRight() const {return right;} const glm::vec3 & getUp () const {return up; } const glm::vec3 & getFront() const {return front;} From 200d81e0eb9a59482c128f53e291081d37bb8f7b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 22 May 2013 14:40:01 -0700 Subject: [PATCH 3/6] DRY up resource subdir copying for OS X bundle --- interface/CMakeLists.txt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 35eb70bdfc..d5da2073fb 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -48,14 +48,19 @@ if (APPLE) # set where in the bundle to put the resources file SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/interface.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources) + SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/interface.icns) + # grab the directories in resources and put them in the right spot in Resources - FILE(GLOB INTERFACE_IMAGES resources/images/*) - SET_SOURCE_FILES_PROPERTIES(${INTERFACE_IMAGES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/images) + file(GLOB RESOURCE_SUBDIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/resources ${CMAKE_CURRENT_SOURCE_DIR}/resources/*) + foreach(DIR ${RESOURCE_SUBDIRS}) + if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/resources/${DIR}) + FILE(GLOB DIR_CONTENTS resources/${DIR}/*) + SET_SOURCE_FILES_PROPERTIES(${DIR_CONTENTS} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/${DIR}) + + SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${DIR_CONTENTS}) + endif() + endforeach() - FILE(GLOB INTERFACE_SHADERS resources/shaders/*) - SET_SOURCE_FILES_PROPERTIES(${INTERFACE_SHADERS} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/shaders) - - SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${INTERFACE_IMAGES} ${INTERFACE_SHADERS} ${CMAKE_CURRENT_SOURCE_DIR}/interface.icns) endif (APPLE) find_package(Qt4 REQUIRED QtCore QtGui QtOpenGL) From 53ae8235f365250e609c81e95a5be8231b92d30d Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 22 May 2013 14:43:25 -0700 Subject: [PATCH 4/6] Fixed threading issue with Environment, more work on avatar righting. --- interface/src/Avatar.cpp | 11 ++++------- interface/src/Avatar.h | 1 + interface/src/Environment.cpp | 28 ++++++++++++++++++++++------ interface/src/Environment.h | 9 ++++++--- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index a7cd4ae5e2..5e7aeb3006 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -340,7 +340,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { float gravityLength = glm::length(_gravity); if (gravityLength > 0.0f) { glm::vec3 targetUp = _gravity / -gravityLength; - const glm::vec3& currentUp = _orientation.getUp(); + const glm::vec3& currentUp = _righting * glm::vec3(0.0f, 1.0f, 0.0f); float angle = glm::degrees(acosf(glm::dot(currentUp, targetUp))); if (angle > 0.0f) { glm::vec3 axis; @@ -349,11 +349,7 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { } else { axis = glm::normalize(glm::cross(currentUp, targetUp)); } - _orientation.rotate(glm::angleAxis(min(deltaTime * ANGULAR_RIGHTING_SPEED, angle), axis)); - glm::vec3 eulerAngles = glm::eulerAngles(_orientation.getQuat()); - _bodyYaw = eulerAngles.y; - _bodyPitch = eulerAngles.x; - _bodyRoll = eulerAngles.z; + _righting = glm::angleAxis(min(deltaTime * ANGULAR_RIGHTING_SPEED, angle), axis) * _righting; } } @@ -616,7 +612,7 @@ void Avatar::updateCollisionWithSphere(glm::vec3 position, float radius, float d void Avatar::updateCollisionWithEnvironment() { if (_position.y < _pelvisStandingHeight) { - applyCollisionWithScene(glm::vec3(0.0f, _pelvisStandingHeight - _position.y, 0.0f)); + //applyCollisionWithScene(glm::vec3(0.0f, _pelvisStandingHeight - _position.y, 0.0f)); } float radius = _height * 0.125f; @@ -1005,6 +1001,7 @@ void Avatar::updateSkeleton() { _orientation.yaw (_bodyYaw ); _orientation.pitch(_bodyPitch); _orientation.roll (_bodyRoll ); + _orientation.rotate(_righting); // calculate positions of all bones by traversing the skeleton tree: for (int b = 0; b < NUM_AVATAR_JOINTS; b++) { diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index 8fe4030446..34ed0004ff 100644 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -169,6 +169,7 @@ private: float _speed; float _maxArmLength; Orientation _orientation; + glm::quat _righting; int _driveKeys[MAX_DRIVE_KEYS]; float _pelvisStandingHeight; float _height; diff --git a/interface/src/Environment.cpp b/interface/src/Environment.cpp index 6b5cca5bc4..990dd83698 100644 --- a/interface/src/Environment.cpp +++ b/interface/src/Environment.cpp @@ -6,6 +6,7 @@ // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. #include +#include #include #include @@ -40,6 +41,9 @@ void Environment::init() { } void Environment::renderAtmospheres(Camera& camera) { + // get the lock for the duration of the call + QMutexLocker locker(&_mutex); + foreach (const ServerData& serverData, _data) { foreach (const EnvironmentData& environmentData, serverData) { renderAtmosphere(camera, environmentData); @@ -47,7 +51,10 @@ void Environment::renderAtmospheres(Camera& camera) { } } -glm::vec3 Environment::getGravity (const glm::vec3& position) const { +glm::vec3 Environment::getGravity (const glm::vec3& position) { + // get the lock for the duration of the call + QMutexLocker locker(&_mutex); + glm::vec3 gravity; foreach (const ServerData& serverData, _data) { foreach (const EnvironmentData& environmentData, serverData) { @@ -60,24 +67,30 @@ glm::vec3 Environment::getGravity (const glm::vec3& position) const { return gravity; } -const EnvironmentData& Environment::getClosestData(const glm::vec3& position) const { - const EnvironmentData* closest; +const EnvironmentData Environment::getClosestData(const glm::vec3& position) { + // get the lock for the duration of the call + QMutexLocker locker(&_mutex); + + EnvironmentData closest; float closestDistance = FLT_MAX; foreach (const ServerData& serverData, _data) { foreach (const EnvironmentData& environmentData, serverData) { float distance = glm::distance(position, environmentData.getAtmosphereCenter()) - environmentData.getAtmosphereOuterRadius(); if (distance < closestDistance) { - closest = &environmentData; + closest = environmentData; closestDistance = distance; } } } - return *closest; + return closest; } bool Environment::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, - float radius, glm::vec3& penetration) const { + float radius, glm::vec3& penetration) { + // get the lock for the duration of the call + QMutexLocker locker(&_mutex); + bool found = false; penetration = glm::vec3(0.0f, 0.0f, 0.0f); foreach (const ServerData& serverData, _data) { @@ -98,6 +111,9 @@ int Environment::parseData(sockaddr *senderAddress, unsigned char* sourceBuffer, EnvironmentData newData; int bytesRead = newData.parseData(sourceBuffer, numBytes); + // get the lock for the duration of the call + QMutexLocker locker(&_mutex); + // update the mapping by address/ID _data[*senderAddress][newData.getID()] = newData; diff --git a/interface/src/Environment.h b/interface/src/Environment.h index 95112806e5..f711f0c38c 100644 --- a/interface/src/Environment.h +++ b/interface/src/Environment.h @@ -10,6 +10,7 @@ #define __interface__Environment__ #include +#include #include @@ -25,10 +26,10 @@ public: void init(); void renderAtmospheres(Camera& camera); - glm::vec3 getGravity (const glm::vec3& position) const; - const EnvironmentData& getClosestData(const glm::vec3& position) const; + glm::vec3 getGravity (const glm::vec3& position); + const EnvironmentData getClosestData(const glm::vec3& position); - bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const; + bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration); int parseData(sockaddr *senderAddress, unsigned char* sourceBuffer, int numBytes); @@ -67,6 +68,8 @@ private: typedef QHash ServerData; QHash _data; + + QMutex _mutex; }; #endif /* defined(__interface__Environment__) */ From 7a8aa7c04f5322c29ebfc72e8953993f8bfd51bf Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 22 May 2013 14:50:11 -0700 Subject: [PATCH 5/6] Restore "floor." --- interface/src/Avatar.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 5e7aeb3006..3d47cdad0a 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -612,7 +612,7 @@ void Avatar::updateCollisionWithSphere(glm::vec3 position, float radius, float d void Avatar::updateCollisionWithEnvironment() { if (_position.y < _pelvisStandingHeight) { - //applyCollisionWithScene(glm::vec3(0.0f, _pelvisStandingHeight - _position.y, 0.0f)); + applyCollisionWithScene(glm::vec3(0.0f, _pelvisStandingHeight - _position.y, 0.0f)); } float radius = _height * 0.125f; From c97b4f5e1d481a7cd05035d90d35967bfb966adf Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 22 May 2013 15:22:05 -0700 Subject: [PATCH 6/6] Fix the spacing, and while we're at it, change tabs to spaces. --- libraries/avatars/src/Orientation.h | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libraries/avatars/src/Orientation.h b/libraries/avatars/src/Orientation.h index ecf5f47638..e546a95d60 100755 --- a/libraries/avatars/src/Orientation.h +++ b/libraries/avatars/src/Orientation.h @@ -19,36 +19,36 @@ const glm::vec3 IDENTITY_FRONT = glm::vec3( 0.0f, 0.0f, 1.0f); class Orientation { public: - Orientation(); - - void set(Orientation); - void setToPitchYawRoll(float pitch_change, float yaw_change, float roll_change); - void setToIdentity(); + Orientation(); + + void set(Orientation); + void setToPitchYawRoll(float pitch_change, float yaw_change, float roll_change); + void setToIdentity(); - void pitch(float pitch_change); - void yaw (float yaw_change); - void roll (float roll_change); + void pitch(float pitch_change); + void yaw (float yaw_change); + void roll (float roll_change); void rotate(float pitch, float yaw, float roll); void rotate(glm::vec3 EulerAngles); void rotate(glm::quat quaternion); - const glm::quat & getQuat() const {return quat;} + const glm::quat& getQuat() const {return quat;} - const glm::vec3 & getRight() const {return right;} - const glm::vec3 & getUp () const {return up; } - const glm::vec3 & getFront() const {return front;} + const glm::vec3& getRight() const {return right;} + const glm::vec3& getUp () const {return up; } + const glm::vec3& getFront() const {return front;} - const glm::vec3 & getIdentityRight() const {return IDENTITY_RIGHT;} - const glm::vec3 & getIdentityUp () const {return IDENTITY_UP;} - const glm::vec3 & getIdentityFront() const {return IDENTITY_FRONT;} + const glm::vec3& getIdentityRight() const {return IDENTITY_RIGHT;} + const glm::vec3& getIdentityUp () const {return IDENTITY_UP;} + const glm::vec3& getIdentityFront() const {return IDENTITY_FRONT;} private: glm::quat quat; glm::vec3 right; - glm::vec3 up; - glm::vec3 front; + glm::vec3 up; + glm::vec3 front; void rotateAndGenerateDirections(glm::quat rotation); };