fix failed lock causing particle collisions to not work

This commit is contained in:
ZappoMan 2014-03-04 15:07:30 -08:00
parent 6d1748e5b9
commit c90d4a9514
3 changed files with 79 additions and 33 deletions

View file

@ -590,16 +590,26 @@ bool findRayIntersectionOp(OctreeElement* node, void* extraData) {
}
bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
OctreeElement*& node, float& distance, BoxFace& face, bool tryLock) {
OctreeElement*& node, float& distance, BoxFace& face, Octree::lockType lockType) {
RayArgs args = { origin / (float)(TREE_SCALE), direction, node, distance, face };
if (!tryLock) {
bool gotLock = false;
if (lockType == Octree::Lock) {
lockForRead();
gotLock = true;
} else if (lockType == Octree::TryLock) {
gotLock = tryLockForRead();
if (!gotLock) {
return args.found; // if we wanted to tryLock, and we couldn't then just bail...
}
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findRayIntersectionOp, &args);
recurseTreeWithOperation(findRayIntersectionOp, &args);
if (gotLock) {
unlock();
}
return args.found;
}
@ -635,7 +645,7 @@ bool findSpherePenetrationOp(OctreeElement* element, void* extraData) {
}
bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration,
void** penetratedObject, bool tryLock) {
void** penetratedObject, Octree::lockType lockType) {
SphereArgs args = {
center / (float)(TREE_SCALE),
@ -644,17 +654,27 @@ bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::v
false,
NULL };
penetration = glm::vec3(0.0f, 0.0f, 0.0f);
if (!tryLock) {
bool gotLock = false;
if (lockType == Octree::Lock) {
lockForRead();
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findSpherePenetrationOp, &args);
if (penetratedObject) {
*penetratedObject = args.penetratedObject;
gotLock = true;
} else if (lockType == Octree::TryLock) {
gotLock = tryLockForRead();
if (!gotLock) {
return args.found; // if we wanted to tryLock, and we couldn't then just bail...
}
}
recurseTreeWithOperation(findSpherePenetrationOp, &args);
if (penetratedObject) {
*penetratedObject = args.penetratedObject;
}
if (gotLock) {
unlock();
}
return args.found;
}
@ -689,7 +709,7 @@ bool findCapsulePenetrationOp(OctreeElement* node, void* extraData) {
}
bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius,
glm::vec3& penetration, bool tryLock) {
glm::vec3& penetration, Octree::lockType lockType) {
CapsuleArgs args = {
start / (float)(TREE_SCALE),
@ -699,11 +719,20 @@ bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end
false };
penetration = glm::vec3(0.0f, 0.0f, 0.0f);
if (!tryLock) {
bool gotLock = false;
if (lockType == Octree::Lock) {
lockForRead();
gotLock = true;
} else if (lockType == Octree::TryLock) {
gotLock = tryLockForRead();
if (!gotLock) {
return args.found; // if we wanted to tryLock, and we couldn't then just bail...
}
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(findCapsulePenetrationOp, &args);
recurseTreeWithOperation(findCapsulePenetrationOp, &args);
if (gotLock) {
unlock();
}
return args.found;
@ -732,18 +761,28 @@ bool getElementEnclosingOperation(OctreeElement* element, void* extraData) {
return true; // keep looking
}
OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, bool tryLock) {
OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType) {
GetElementEnclosingArgs args;
args.point = point;
args.element = NULL;
if (!tryLock) {
bool gotLock = false;
if (lockType == Octree::Lock) {
lockForRead();
gotLock = true;
} else if (lockType == Octree::TryLock) {
gotLock = tryLockForRead();
if (!gotLock) {
return args.element; // if we wanted to tryLock, and we couldn't then just bail...
}
}
if (tryLock && tryLockForRead()) {
recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args);
recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args);
if (gotLock) {
unlock();
}
return args.element;
}

View file

@ -221,16 +221,29 @@ public:
void clearDirtyBit() { _isDirty = false; }
void setDirtyBit() { _isDirty = true; }
// Octree does not currently handle its own locking, caller must use these to lock/unlock
void lockForRead() { _lock.lockForRead(); }
bool tryLockForRead() { return _lock.tryLockForRead(); }
void lockForWrite() { _lock.lockForWrite(); }
bool tryLockForWrite() { return _lock.tryLockForWrite(); }
void unlock() { _lock.unlock(); }
// output hints from the encode process
typedef enum {
Lock,
TryLock,
NoLock
} lockType;
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
OctreeElement*& node, float& distance, BoxFace& face, bool tryLock = true);
OctreeElement*& node, float& distance, BoxFace& face, Octree::lockType lockType = Octree::TryLock);
bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration,
void** penetratedObject = NULL, bool tryLock = true);
void** penetratedObject = NULL, Octree::lockType lockType = Octree::TryLock);
bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius,
glm::vec3& penetration, bool tryLock = true);
glm::vec3& penetration, Octree::lockType lockType = Octree::TryLock);
OctreeElement* getElementEnclosingPoint(const glm::vec3& point, bool tryLock = true);
OctreeElement* getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType = Octree::TryLock);
// Note: this assumes the fileFormat is the HIO individual voxels code files
void loadOctreeFile(const char* fileName, bool wantColorRandomizer);
@ -238,13 +251,7 @@ public:
// these will read/write files that match the wireformat, excluding the 'V' leading
void writeToSVOFile(const char* filename, OctreeElement* node = NULL);
bool readFromSVOFile(const char* filename);
// Octree does not currently handle its own locking, caller must use these to lock/unlock
void lockForRead() { _lock.lockForRead(); }
bool tryLockForRead() { return _lock.tryLockForRead(); }
void lockForWrite() { _lock.lockForWrite(); }
bool tryLockForWrite() { return _lock.tryLockForWrite(); }
void unlock() { _lock.unlock(); }
unsigned long getOctreeElementsCount();

View file

@ -58,7 +58,7 @@ bool ParticleCollisionSystem::updateOperation(OctreeElement* element, void* extr
void ParticleCollisionSystem::update() {
// update all particles
if (_particles->tryLockForWrite()) {
if (_particles->tryLockForRead()) {
_particles->recurseTreeWithOperation(updateOperation, this);
_particles->unlock();
}
@ -117,7 +117,7 @@ void ParticleCollisionSystem::updateCollisionWithParticles(Particle* particleA)
const float COLLISION_FREQUENCY = 0.5f;
glm::vec3 penetration;
Particle* particleB;
if (_particles->findSpherePenetration(center, radius, penetration, (void**)&particleB)) {
if (_particles->findSpherePenetration(center, radius, penetration, (void**)&particleB, Octree::NoLock)) {
// NOTE: 'penetration' is the depth that 'particleA' overlaps 'particleB'.
// That is, it points from A into B.