/* Copyright (c) 2015 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "collision/CollisionPairQueue.h" #include "collision/HashTableCollisionObjectPairKey.h" #include #define stemobject_implementation CollisionPairQueue stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); CollisionPairQueue * CollisionPairQueue_create() { stemobject_create_implementation(init) } bool CollisionPairQueue_init(CollisionPairQueue * self) { call_super(init, self); self->pairCount = 0; self->private_ivar(pairAllocatedCount) = 64; self->pairs = malloc(self->private_ivar(pairAllocatedCount) * sizeof(*self->pairs)); self->nextPairCount = 0; self->private_ivar(nextPairAllocatedCount) = 64; self->nextPairs = malloc(self->private_ivar(nextPairAllocatedCount) * sizeof(*self->nextPairs)); self->nextObjectCount = 0; self->private_ivar(nextObjectAllocatedCount) = 64; self->nextObjects = malloc(self->private_ivar(nextObjectAllocatedCount) * sizeof(*self->nextObjects)); self->private_ivar(pairHashTable) = HashTable_create(0); self->queuePosition = 0; return true; } void CollisionPairQueue_dispose(CollisionPairQueue * self) { free(self->pairs); free(self->nextPairs); free(self->nextObjects); HashTable_dispose(self->private_ivar(pairHashTable)); call_super(dispose, self); } static void addPairInternal(CollisionPairQueue * self, CollisionObject * object1, CollisionObject * object2) { if (self->private_ivar(pairAllocatedCount) <= self->pairCount) { self->private_ivar(pairAllocatedCount) *= 2; self->pairs = realloc(self->pairs, self->private_ivar(pairAllocatedCount) * sizeof(*self->pairs)); } self->pairs[self->pairCount++] = (struct collisionObjectPair) {object1, object2}; HashTableCollisionObjectPairKey key; stemobject_assign_vtable(key, HashTableCollisionObjectPairKey); HashTableCollisionObjectPairKey_init(&key, object1, object2); HashTable_set(self->private_ivar(pairHashTable), HashTable_objectKey(&key), NULL); } static void addPair(CollisionPairQueue * self, CollisionObject * object1, CollisionObject * object2) { if (call_virtual(isStationary, object1) && call_virtual(isStationary, object2)) { return; } if (!Box6x_intersectsBox6x(call_virtual(getCollisionBounds, object1), call_virtual(getCollisionBounds, object2))) { return; } HashTableCollisionObjectPairKey key; stemobject_assign_vtable(key, HashTableCollisionObjectPairKey); HashTableCollisionObjectPairKey_init(&key, object1, object2); if (HashTable_get(self->private_ivar(pairHashTable), HashTable_objectKey(&key)) != NULL) { return; } addPairInternal(self, object1, object2); } void CollisionPairQueue_addInitialPairs(CollisionPairQueue * self, CollisionObject ** movingObjects, size_t movingObjectCount, CollisionObject ** staticObjects, size_t staticObjectCount) { self->pairCount = self->queuePosition = 0; for (size_t movingObjectIndex = 0; movingObjectIndex < movingObjectCount; movingObjectIndex++) { for (size_t movingObjectIndex2 = movingObjectIndex + 1; movingObjectIndex2 < movingObjectCount; movingObjectIndex2++) { addPairInternal(self, movingObjects[movingObjectIndex], movingObjects[movingObjectIndex2]); } for (size_t staticObjectIndex = 0; staticObjectIndex < staticObjectCount; staticObjectIndex++) { addPairInternal(self, movingObjects[movingObjectIndex], staticObjects[staticObjectIndex]); } } } void CollisionPairQueue_addNextPair(CollisionPairQueue * self, CollisionObject * object1, CollisionObject * object2) { if (self->private_ivar(nextPairAllocatedCount) <= self->nextPairCount) { self->private_ivar(nextPairAllocatedCount) *= 2; self->nextPairs = realloc(self->nextPairs, self->private_ivar(nextPairAllocatedCount) * sizeof(*self->nextPairs)); } self->nextPairs[self->nextPairCount++] = (struct collisionObjectPair) {object1, object2}; } void CollisionPairQueue_addNextObject(CollisionPairQueue * self, CollisionObject * object) { for (unsigned int objectIndex = 0; objectIndex < self->nextObjectCount; objectIndex++) { if (self->nextObjects[objectIndex] == object) { return; } } if (self->private_ivar(nextObjectAllocatedCount) <= self->nextObjectCount) { self->private_ivar(nextObjectAllocatedCount) *= 2; self->nextObjects = realloc(self->nextObjects, self->private_ivar(nextObjectAllocatedCount) * sizeof(*self->nextObjects)); } self->nextObjects[self->nextObjectCount++] = object; } bool CollisionPairQueue_nextPair(CollisionPairQueue * self, CollisionObject ** outObject1, CollisionObject ** outObject2) { if (self->queuePosition >= self->pairCount) { return false; } *outObject1 = self->pairs[self->queuePosition].object1; *outObject2 = self->pairs[self->queuePosition].object2; self->queuePosition++; return true; } bool CollisionPairQueue_nextIteration(CollisionPairQueue * self, CollisionObject ** movingObjects, size_t movingObjectCount, CollisionObject ** staticObjects, size_t staticObjectCount) { self->pairCount = self->queuePosition = 0; HashTable_deleteAll(self->private_ivar(pairHashTable)); if (self->private_ivar(pairAllocatedCount) < self->private_ivar(nextPairAllocatedCount)) { self->private_ivar(pairAllocatedCount) = self->private_ivar(nextPairAllocatedCount); self->pairs = realloc(self->pairs, self->private_ivar(pairAllocatedCount) * sizeof(*self->pairs)); } memcpy(self->pairs, self->nextPairs, sizeof(*self->pairs) * self->nextPairCount); self->pairCount = self->nextPairCount; self->nextPairCount = 0; for (unsigned int nextObjectIndex = 0; nextObjectIndex < self->nextObjectCount; nextObjectIndex++) { for (size_t movingObjectIndex = 0; movingObjectIndex < movingObjectCount; movingObjectIndex++) { if (movingObjects[movingObjectIndex] == self->nextObjects[nextObjectIndex]) { continue; } addPair(self, self->nextObjects[nextObjectIndex], movingObjects[movingObjectIndex]); } if (!self->nextObjects[nextObjectIndex]->alwaysStatic && !call_virtual(isStationary, self->nextObjects[nextObjectIndex])) { for (size_t staticObjectIndex = 0; staticObjectIndex < staticObjectCount; staticObjectIndex++) { addPair(self, self->nextObjects[nextObjectIndex], staticObjects[staticObjectIndex]); } } } self->nextObjectCount = 0; return self->pairCount > 0; }