mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 11:45:36 +02:00
keep track of objectDynamics per rigidBody
This commit is contained in:
parent
afa8fc161b
commit
52aee63e05
6 changed files with 57 additions and 27 deletions
|
@ -35,6 +35,19 @@ ObjectConstraintHinge::~ObjectConstraintHinge() {
|
|||
#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() {
|
||||
btHingeConstraint* constraint { nullptr };
|
||||
QUuid otherEntityID;
|
||||
|
@ -65,20 +78,7 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
|
|||
|
||||
if (!otherEntityID.isNull()) {
|
||||
// This hinge is between two entities... find the other rigid body.
|
||||
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;
|
||||
}
|
||||
btRigidBody* rigidBodyB = otherMotionState->getRigidBody();
|
||||
btRigidBody* rigidBodyB = getOtherRigidBody(otherEntityID);
|
||||
if (!rigidBodyB) {
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -94,7 +94,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
|
|||
constraint->setAxis(bulletAxisInA);
|
||||
|
||||
withWriteLock([&]{
|
||||
_otherEntity = otherEntity; // save weak-pointer to the other entity
|
||||
_constraint = constraint;
|
||||
});
|
||||
|
||||
|
@ -110,7 +109,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
|
|||
constraint->setAxis(bulletAxisInA);
|
||||
|
||||
withWriteLock([&]{
|
||||
_otherEntity.reset();
|
||||
_constraint = constraint;
|
||||
});
|
||||
return constraint;
|
||||
|
|
|
@ -27,6 +27,7 @@ public:
|
|||
virtual QByteArray serialize() const override;
|
||||
virtual void deserialize(QByteArray serializedArguments) override;
|
||||
|
||||
virtual QList<btRigidBody*> getRigidBodies() override;
|
||||
virtual btTypedConstraint* getConstraint() override;
|
||||
|
||||
protected:
|
||||
|
@ -35,8 +36,7 @@ protected:
|
|||
glm::vec3 _pivotInA;
|
||||
glm::vec3 _axisInA;
|
||||
|
||||
QUuid _otherEntityID;
|
||||
EntityItemWeakPointer _otherEntity;
|
||||
EntityItemID _otherEntityID;
|
||||
glm::vec3 _pivotInB;
|
||||
glm::vec3 _axisInB;
|
||||
};
|
||||
|
|
|
@ -271,3 +271,28 @@ quint64 ObjectDynamic::serverTimeToLocalTime(quint64 timeValue) const {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -43,10 +43,13 @@ public:
|
|||
virtual bool lifetimeIsOver() override;
|
||||
virtual quint64 getExpires() override { return _expires; }
|
||||
|
||||
virtual QList<btRigidBody*> getRigidBodies();
|
||||
|
||||
protected:
|
||||
quint64 localTimeToServerTime(quint64 timeValue) const;
|
||||
quint64 serverTimeToLocalTime(quint64 timeValue) const;
|
||||
|
||||
btRigidBody* getOtherRigidBody(EntityItemID otherEntityID);
|
||||
EntityItemPointer getEntityByID(EntityItemID entityID) const;
|
||||
virtual btRigidBody* getRigidBody();
|
||||
virtual glm::vec3 getPosition() override;
|
||||
|
|
|
@ -570,25 +570,27 @@ bool PhysicsEngine::addDynamic(EntityDynamicPointer dynamic) {
|
|||
removeDynamic(dynamic->getID());
|
||||
}
|
||||
|
||||
_objectDynamics[dynamicID] = dynamic;
|
||||
|
||||
// bullet needs a pointer to the dynamic, but it doesn't use shared pointers.
|
||||
// is there a way to bump the reference count?
|
||||
bool success { false };
|
||||
if (dynamic->isAction()) {
|
||||
ObjectAction* objectAction = static_cast<ObjectAction*>(dynamic.get());
|
||||
_dynamicsWorld->addAction(objectAction);
|
||||
return true;
|
||||
success = true;
|
||||
} else if (dynamic->isConstraint()) {
|
||||
ObjectConstraint* objectConstraint = static_cast<ObjectConstraint*>(dynamic.get());
|
||||
btTypedConstraint* constraint = objectConstraint->getConstraint();
|
||||
if (constraint) {
|
||||
_dynamicsWorld->addConstraint(constraint);
|
||||
return true;
|
||||
} else {
|
||||
// perhaps not all the rigid bodies are available, yet
|
||||
return false;
|
||||
success = true;
|
||||
} // else perhaps not all the rigid bodies are available, yet
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -112,6 +112,7 @@ private:
|
|||
ContactMap _contactMap;
|
||||
CollisionEvents _collisionEvents;
|
||||
QHash<QUuid, EntityDynamicPointer> _objectDynamics;
|
||||
QHash<btRigidBody*, EntityDynamicPointer> _objectDynamicsByBody;
|
||||
std::vector<btRigidBody*> _activeStaticBodies;
|
||||
|
||||
glm::vec3 _originOffset;
|
||||
|
@ -123,6 +124,7 @@ private:
|
|||
|
||||
bool _dumpNextStats = false;
|
||||
bool _hasOutgoingChanges = false;
|
||||
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<PhysicsEngine> PhysicsEnginePointer;
|
||||
|
|
Loading…
Reference in a new issue