From 9346171695830c30442ffdd2dc2758e44828aed7 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 4 Mar 2016 16:47:30 -0800 Subject: [PATCH] if entity registration isn't default, adjust physics shapes to match --- .../src/RenderableModelEntityItem.cpp | 2 ++ .../src/RenderablePolyVoxEntityItem.cpp | 1 + libraries/entities/src/EntityItem.cpp | 22 +++++++++++++++++-- libraries/entities/src/EntityItem.h | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index e3870705c9..1577cac10c 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -598,6 +598,7 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) { if (type != SHAPE_TYPE_COMPOUND) { ModelEntityItem::computeShapeInfo(info); info.setParams(type, 0.5f * getDimensions()); + adjustShapeInfoByRegistration(info); } else { const QSharedPointer collisionNetworkGeometry = _model->getCollisionGeometry(); @@ -701,6 +702,7 @@ void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) { glm::vec3 collisionModelDimensions = box.getDimensions(); info.setParams(type, collisionModelDimensions, _compoundShapeURL); info.setConvexHulls(_points); + adjustShapeInfoByRegistration(info); } } diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index ef777df403..8054ed9a8b 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -1228,6 +1228,7 @@ void RenderablePolyVoxEntityItem::computeShapeInfoWorkerAsync() { _shapeInfoLock.lockForWrite(); _shapeInfo.setParams(SHAPE_TYPE_COMPOUND, collisionModelDimensions, QString(b64)); _shapeInfo.setConvexHulls(points); + adjustShapeInfoByRegistration(_shapeInfo); _shapeInfoLock.unlock(); _meshLock.lockForWrite(); diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 04ca7559a0..c421f3826d 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -677,7 +677,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef READ_ENTITY_PROPERTY(PROP_LIFETIME, float, updateLifetime); READ_ENTITY_PROPERTY(PROP_SCRIPT, QString, setScript); READ_ENTITY_PROPERTY(PROP_SCRIPT_TIMESTAMP, quint64, setScriptTimestamp); - READ_ENTITY_PROPERTY(PROP_REGISTRATION_POINT, glm::vec3, setRegistrationPoint); + READ_ENTITY_PROPERTY(PROP_REGISTRATION_POINT, glm::vec3, updateRegistrationPoint); READ_ENTITY_PROPERTY(PROP_ANGULAR_DAMPING, float, updateAngularDamping); READ_ENTITY_PROPERTY(PROP_VISIBLE, bool, setVisible); @@ -1120,7 +1120,7 @@ bool EntityItem::setProperties(const EntityItemProperties& properties) { // these (along with "position" above) affect tree structure SET_ENTITY_PROPERTY_FROM_PROPERTIES(dimensions, updateDimensions); - SET_ENTITY_PROPERTY_FROM_PROPERTIES(registrationPoint, setRegistrationPoint); + SET_ENTITY_PROPERTY_FROM_PROPERTIES(registrationPoint, updateRegistrationPoint); // these (along with all properties above) affect the simulation SET_ENTITY_PROPERTY_FROM_PROPERTIES(density, updateDensity); @@ -1340,6 +1340,15 @@ float EntityItem::getRadius() const { return 0.5f * glm::length(getDimensions()); } +void EntityItem::adjustShapeInfoByRegistration(ShapeInfo& info) const { + if (_registrationPoint != ENTITY_ITEM_DEFAULT_REGISTRATION_POINT) { + glm::mat4 scale = glm::scale(getDimensions()); + glm::mat4 registration = scale * glm::translate(ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint()); + glm::vec3 regTransVec = glm::vec3(registration[3]); // extract position component from matrix + info.setOffset(regTransVec); + } +} + bool EntityItem::contains(const glm::vec3& point) const { if (getShapeType() == SHAPE_TYPE_COMPOUND) { bool success; @@ -1348,12 +1357,21 @@ bool EntityItem::contains(const glm::vec3& point) const { } else { ShapeInfo info; info.setParams(getShapeType(), glm::vec3(0.5f)); + adjustShapeInfoByRegistration(info); return info.contains(worldToEntity(point)); } } void EntityItem::computeShapeInfo(ShapeInfo& info) { info.setParams(getShapeType(), 0.5f * getDimensions()); + adjustShapeInfoByRegistration(info); +} + +void EntityItem::updateRegistrationPoint(const glm::vec3& value) { + if (value != _registrationPoint) { + setRegistrationPoint(value); + _dirtyFlags |= Simulation::DIRTY_SHAPE; + } } void EntityItem::updatePosition(const glm::vec3& value) { diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 975f571eb3..d327bd9004 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -305,6 +305,7 @@ public: // TODO: get rid of users of getRadius()... float getRadius() const; + virtual void adjustShapeInfoByRegistration(ShapeInfo& info) const; virtual bool contains(const glm::vec3& point) const; virtual bool isReadyToComputeShape() { return !isDead(); } @@ -319,6 +320,7 @@ public: virtual void setRotation(glm::quat orientation) { setOrientation(orientation); } // updateFoo() methods to be used when changes need to be accumulated in the _dirtyFlags + void updateRegistrationPoint(const glm::vec3& value); void updatePosition(const glm::vec3& value); void updatePositionFromNetwork(const glm::vec3& value); void updateDimensions(const glm::vec3& value);