diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp new file mode 100644 index 0000000000..2eaab25a46 --- /dev/null +++ b/libraries/physics/src/EntityMotionState.cpp @@ -0,0 +1,68 @@ +// +// EntityMotionState.cpp +// libraries/physcis/src +// +// Created by Andrew Meadows 2014.11.05 +// 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 +// + +#ifdef USE_BULLET_PHYSICS + +#include "EntityMotionState.h" + +EntityMotionState::EntityMotionState() : _motionType(MOTION_TYPE_STATIC), + _inertiaDiagLocal(1.0f, 1.0f, 1.0f), _mass(1.0f), + _shape(NULL), _object(NULL) { +} + +/* +void EntityMotionState::getWorldTransform (btTransform &worldTrans) const { +} + +void EntityMotionState::setWorldTransform (const btTransform &worldTrans) { +} + +void EntityMotionState::computeMassProperties() { +} + +void EntityMotionState::getShapeInfo(ShapeInfo& info) { +} +*/ + +bool EntityMotionState::makeStatic() { + if (_motionType == MOTION_TYPE_STATIC) { + return true; + } + if (!_object) { + _motionType = MOTION_TYPE_STATIC; + return true; + } + return false; +} + +bool EntityMotionState::makeDynamic() { + if (_motionType == MOTION_TYPE_DYNAMIC) { + return true; + } + if (!_object) { + _motionType = MOTION_TYPE_DYNAMIC; + return true; + } + return false; +} + +bool EntityMotionState::makeKinematic() { + if (_motionType == MOTION_TYPE_KINEMATIC) { + return true; + } + if (!_object) { + _motionType = MOTION_TYPE_KINEMATIC; + return true; + } + return false; +} + +#endif // USE_BULLET_PHYSICS diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h new file mode 100644 index 0000000000..b891f93ae3 --- /dev/null +++ b/libraries/physics/src/EntityMotionState.h @@ -0,0 +1,57 @@ +// +// EntityMotionState.h +// libraries/physcis/src +// +// Created by Andrew Meadows 2014.11.05 +// 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_EntityMotionState_h +#define hifi_EntityMotionState_h + +#ifdef USE_BULLET_PHYSICS + +#include + +#include "ShapeInfo.h" +#include "UUIDHashKey.h" + +enum MotionType { + MOTION_TYPE_STATIC, // no motion + MOTION_TYPE_DYNAMIC, // motion according to physical laws + MOTION_TYPE_KINEMATIC // keyframed motion +}; + +class EntityMotionState : public btMotionState { +public: + EntityMotionState(); + + //// these override methods of the btMotionState base class + //virtual void getWorldTransform (btTransform &worldTrans) const; + //virtual void setWorldTransform (const btTransform &worldTrans); + + virtual void computeMassProperties() = 0; + virtual void getShapeInfo(ShapeInfo& info) = 0; + + bool makeStatic(); + bool makeDynamic(); + bool makeKinematic(); + + MotionType getMotionType() const { return _motionType; } + +private: + friend class PhysicsWorld; + + //EntityItem* _entity; + MotionType _motionType; + btVector3 _inertiaDiagLocal; + float _mass; + btCollisionShape* _shape; + btCollisionObject* _object; +}; + +#endif // USE_BULLET_PHYSICS +#endif // hifi_EntityMotionState_h diff --git a/libraries/physics/src/PhysicsWorld.cpp b/libraries/physics/src/PhysicsWorld.cpp index faa5757e4a..0d475f0a84 100644 --- a/libraries/physics/src/PhysicsWorld.cpp +++ b/libraries/physics/src/PhysicsWorld.cpp @@ -22,9 +22,6 @@ void PhysicsWorld::init() { _dynamicsWorld = new btDiscreteDynamicsWorld(_collisionDispatcher, _broadphaseFilter, _constraintSolver, _collisionConfig); } -/// \return true if Voxel added -/// \param position the minimum corner of the voxel -/// \param scale the length of the voxel side bool PhysicsWorld::addVoxel(const glm::vec3& position, float scale) { glm::vec3 halfExtents = glm::vec3(0.5f * scale); glm::vec3 center = position + halfExtents; @@ -55,9 +52,6 @@ bool PhysicsWorld::addVoxel(const glm::vec3& position, float scale) { return false; } -/// \return true if Voxel removed -/// \param position the minimum corner of the voxel -/// \param scale the length of voxel side bool PhysicsWorld::removeVoxel(const glm::vec3& position, float scale) { glm::vec3 halfExtents = glm::vec3(0.5f * scale); glm::vec3 center = position + halfExtents; @@ -81,3 +75,24 @@ bool PhysicsWorld::removeVoxel(const glm::vec3& position, float scale) { } return false; } + +bool PhysicsWorld::addEntity(const QUuid& id, EntityMotionState* motionState) { + assert(motionState); + UUIDHashKey key(id); + EntityMotionState** statePtr = _entities.find(key); + if (!statePtr) { + // BOOKMARK: Andrew to implement this + } else { + assert(*statePtr == motionState); + } + return false; +} + +bool PhysicsWorld::removeEntity(const QUuid& id) { + UUIDHashKey key(id); + EntityMotionState** statePtr = _entities.find(key); + if (statePtr) { + // BOOKMARK: Andrew to implement this + } + return false; +} diff --git a/libraries/physics/src/PhysicsWorld.h b/libraries/physics/src/PhysicsWorld.h index ca42113a65..f6728bbd8b 100644 --- a/libraries/physics/src/PhysicsWorld.h +++ b/libraries/physics/src/PhysicsWorld.h @@ -16,37 +16,13 @@ #include -#include "ShapeManager.h" #include "BulletUtil.h" +#include "EntityMotionState.h" #include "PositionHashKey.h" +#include "ShapeManager.h" +#include "UUIDHashKey.h" #include "VoxelObject.h" -#ifdef COLLIDABLE -enum MotionType { - MOTION_TYPE_STATIC, - MOTION_TYPE_DYNAMIC, - MOTION_TYPE_KINEMATIC -}; - -class EntityObject { -public: - EntityObject(); - - bool makeStatic(); - bool makeDynamic(); - bool makeKinematic(); - - MotionType getMotionType() const { return _motionType; } - -private: - btCollisionObject* _object; - btMotionState* _motionState; - MotionType _motionType; - btVector3 _inertiaDiagLocal; - float _mass; -}; -#endif // COLLIDABLE - class PhysicsWorld { public: @@ -67,6 +43,14 @@ public: /// \param scale the length of the voxel side bool removeVoxel(const glm::vec3& position, float scale); + /// \return true if Entity added + /// \param info information about collision shapes to create + bool addEntity(const QUuid& id, EntityMotionState* motionState); + + /// \return true if Entity removed + /// \param id UUID of the entity + bool removeEntity(const QUuid& id); + protected: btDefaultCollisionConfiguration* _collisionConfig; btCollisionDispatcher* _collisionDispatcher; @@ -78,6 +62,7 @@ protected: private: btHashMap _voxels; + btHashMap _entities; }; diff --git a/libraries/physics/src/UUIDHashKey.h b/libraries/physics/src/UUIDHashKey.h new file mode 100644 index 0000000000..fcc6209d5a --- /dev/null +++ b/libraries/physics/src/UUIDHashKey.h @@ -0,0 +1,31 @@ +// +// UUIDHashKey.h +// libraries/physcis/src +// +// Created by Andrew Meadows 2014.11.05 +// 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_UUIDHashKey_h +#define hifi_UUIDHashKey_h + +#include + +class UUIDHashKey { +public: + UUIDHashKey(const QUuid& id) : _hash(0), _id(id) { _hash = (int)(qHash(id)); } + + bool equals(const UUIDHashKey& other) const { + return _hash == other._hash && _id == other._id; + } + + unsigned int getHash() const { return (unsigned int)_hash; } +protected: + int _hash; + QUuid _id; +}; + +#endif // hifi_UUIDHashKey_h