keep track of objectDynamics per rigidBody

This commit is contained in:
Seth Alves 2017-04-14 06:08:58 -07:00
parent afa8fc161b
commit 52aee63e05
6 changed files with 57 additions and 27 deletions

View file

@ -35,6 +35,19 @@ ObjectConstraintHinge::~ObjectConstraintHinge() {
#endif #endif
} }
QList<btRigidBody*> ObjectConstraintHinge::getRigidBodies() {
QList<btRigidBody*> result;
result += getRigidBody();
QUuid otherEntityID;
withReadLock([&]{
otherEntityID = _otherEntityID;
});
if (!otherEntityID.isNull()) {
result += getOtherRigidBody(otherEntityID);
}
return result;
}
btTypedConstraint* ObjectConstraintHinge::getConstraint() { btTypedConstraint* ObjectConstraintHinge::getConstraint() {
btHingeConstraint* constraint { nullptr }; btHingeConstraint* constraint { nullptr };
QUuid otherEntityID; QUuid otherEntityID;
@ -65,20 +78,7 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
if (!otherEntityID.isNull()) { if (!otherEntityID.isNull()) {
// This hinge is between two entities... find the other rigid body. // This hinge is between two entities... find the other rigid body.
EntityItemPointer otherEntity = getEntityByID(otherEntityID); btRigidBody* rigidBodyB = getOtherRigidBody(otherEntityID);
if (!otherEntity) {
return nullptr;
}
void* otherPhysicsInfo = otherEntity->getPhysicsInfo();
if (!otherPhysicsInfo) {
return nullptr;
}
ObjectMotionState* otherMotionState = static_cast<ObjectMotionState*>(otherPhysicsInfo);
if (!otherMotionState) {
return nullptr;
}
btRigidBody* rigidBodyB = otherMotionState->getRigidBody();
if (!rigidBodyB) { if (!rigidBodyB) {
return nullptr; return nullptr;
} }
@ -94,7 +94,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
constraint->setAxis(bulletAxisInA); constraint->setAxis(bulletAxisInA);
withWriteLock([&]{ withWriteLock([&]{
_otherEntity = otherEntity; // save weak-pointer to the other entity
_constraint = constraint; _constraint = constraint;
}); });
@ -110,7 +109,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
constraint->setAxis(bulletAxisInA); constraint->setAxis(bulletAxisInA);
withWriteLock([&]{ withWriteLock([&]{
_otherEntity.reset();
_constraint = constraint; _constraint = constraint;
}); });
return constraint; return constraint;

View file

@ -27,6 +27,7 @@ public:
virtual QByteArray serialize() const override; virtual QByteArray serialize() const override;
virtual void deserialize(QByteArray serializedArguments) override; virtual void deserialize(QByteArray serializedArguments) override;
virtual QList<btRigidBody*> getRigidBodies() override;
virtual btTypedConstraint* getConstraint() override; virtual btTypedConstraint* getConstraint() override;
protected: protected:
@ -35,8 +36,7 @@ protected:
glm::vec3 _pivotInA; glm::vec3 _pivotInA;
glm::vec3 _axisInA; glm::vec3 _axisInA;
QUuid _otherEntityID; EntityItemID _otherEntityID;
EntityItemWeakPointer _otherEntity;
glm::vec3 _pivotInB; glm::vec3 _pivotInB;
glm::vec3 _axisInB; glm::vec3 _axisInB;
}; };

View file

@ -271,3 +271,28 @@ quint64 ObjectDynamic::serverTimeToLocalTime(quint64 timeValue) const {
return timeValue - serverClockSkew; return timeValue - serverClockSkew;
} }
btRigidBody* ObjectDynamic::getOtherRigidBody(EntityItemID otherEntityID) {
EntityItemPointer otherEntity = getEntityByID(otherEntityID);
if (!otherEntity) {
return nullptr;
}
void* otherPhysicsInfo = otherEntity->getPhysicsInfo();
if (!otherPhysicsInfo) {
return nullptr;
}
ObjectMotionState* otherMotionState = static_cast<ObjectMotionState*>(otherPhysicsInfo);
if (!otherMotionState) {
return nullptr;
}
return otherMotionState->getRigidBody();
}
QList<btRigidBody*> ObjectDynamic::getRigidBodies() {
QList<btRigidBody*> result;
result += getRigidBody();
return result;
}

View file

@ -43,10 +43,13 @@ public:
virtual bool lifetimeIsOver() override; virtual bool lifetimeIsOver() override;
virtual quint64 getExpires() override { return _expires; } virtual quint64 getExpires() override { return _expires; }
virtual QList<btRigidBody*> getRigidBodies();
protected: protected:
quint64 localTimeToServerTime(quint64 timeValue) const; quint64 localTimeToServerTime(quint64 timeValue) const;
quint64 serverTimeToLocalTime(quint64 timeValue) const; quint64 serverTimeToLocalTime(quint64 timeValue) const;
btRigidBody* getOtherRigidBody(EntityItemID otherEntityID);
EntityItemPointer getEntityByID(EntityItemID entityID) const; EntityItemPointer getEntityByID(EntityItemID entityID) const;
virtual btRigidBody* getRigidBody(); virtual btRigidBody* getRigidBody();
virtual glm::vec3 getPosition() override; virtual glm::vec3 getPosition() override;

View file

@ -570,25 +570,27 @@ bool PhysicsEngine::addDynamic(EntityDynamicPointer dynamic) {
removeDynamic(dynamic->getID()); removeDynamic(dynamic->getID());
} }
_objectDynamics[dynamicID] = dynamic; bool success { false };
// bullet needs a pointer to the dynamic, but it doesn't use shared pointers.
// is there a way to bump the reference count?
if (dynamic->isAction()) { if (dynamic->isAction()) {
ObjectAction* objectAction = static_cast<ObjectAction*>(dynamic.get()); ObjectAction* objectAction = static_cast<ObjectAction*>(dynamic.get());
_dynamicsWorld->addAction(objectAction); _dynamicsWorld->addAction(objectAction);
return true; success = true;
} else if (dynamic->isConstraint()) { } else if (dynamic->isConstraint()) {
ObjectConstraint* objectConstraint = static_cast<ObjectConstraint*>(dynamic.get()); ObjectConstraint* objectConstraint = static_cast<ObjectConstraint*>(dynamic.get());
btTypedConstraint* constraint = objectConstraint->getConstraint(); btTypedConstraint* constraint = objectConstraint->getConstraint();
if (constraint) { if (constraint) {
_dynamicsWorld->addConstraint(constraint); _dynamicsWorld->addConstraint(constraint);
return true; success = true;
} else { } // else perhaps not all the rigid bodies are available, yet
// perhaps not all the rigid bodies are available, yet }
return false;
if (success) {
_objectDynamics[dynamicID] = dynamic;
foreach(btRigidBody* rigidBody, std::static_pointer_cast<ObjectDynamic>(dynamic)->getRigidBodies()) {
_objectDynamicsByBody[rigidBody] = dynamic;
} }
} }
return success;
} }
void PhysicsEngine::removeDynamic(const QUuid dynamicID) { void PhysicsEngine::removeDynamic(const QUuid dynamicID) {

View file

@ -112,6 +112,7 @@ private:
ContactMap _contactMap; ContactMap _contactMap;
CollisionEvents _collisionEvents; CollisionEvents _collisionEvents;
QHash<QUuid, EntityDynamicPointer> _objectDynamics; QHash<QUuid, EntityDynamicPointer> _objectDynamics;
QHash<btRigidBody*, EntityDynamicPointer> _objectDynamicsByBody;
std::vector<btRigidBody*> _activeStaticBodies; std::vector<btRigidBody*> _activeStaticBodies;
glm::vec3 _originOffset; glm::vec3 _originOffset;
@ -123,6 +124,7 @@ private:
bool _dumpNextStats = false; bool _dumpNextStats = false;
bool _hasOutgoingChanges = false; bool _hasOutgoingChanges = false;
}; };
typedef std::shared_ptr<PhysicsEngine> PhysicsEnginePointer; typedef std::shared_ptr<PhysicsEngine> PhysicsEnginePointer;