bool OrthographicGridPathfinder::hasReachedNode(AnimatedSprite *sprite, PathNode destination) { AABB *spriteAABB = sprite->getBoundingVolume(); float xDiff = fabs(spriteAABB->getCenterX() - getColumnCenterX(destination.column)); float yDiff = fabs(spriteAABB->getCenterY() - getRowCenterY(destination.row)); bool xReached = xDiff < GRID_EPSILON; bool yReached = yDiff < GRID_EPSILON; return xReached && yReached; }
/* sweptSpaceTiles */ void TiledLayer::findTileCollisionsForSprite( Physics *physics, CollidableObject *dynamicObject) { int startColumn, endColumn, startRow, endRow; AABB *aabb = dynamicObject->getSweptShape(); initOverlappingCellRange(aabb, startColumn, endColumn, startRow, endRow); if (aabb->getCenterX() >= 288) cout << "ERROR"; for (int i = startColumn; i <= endColumn; i++) { for (int j = startRow; j <= endRow; j++) { Tile *testTile = getTile(j,i); if (testTile->collidable) { float tileX = (float)(i * tileWidth); float tileY = (float)(j * tileHeight); physics->addTileCollision(dynamicObject, testTile, tileX, tileY, (float)tileWidth, (float)tileHeight); } } } }
void Physics::getCollisionsSpriteSprite( World *world, CollidableObject *spriteA, CollidableObject *spriteB, float percentageOfFrameRemaining) { //vector<WorldLayer*> *layers = world->getLayers(); AABB *boundingVolumeA = spriteA->getBoundingVolume(); AABB *boundingVolumeB = spriteB->getBoundingVolume(); PhysicalProperties *ppA = spriteA->getPhysicalProperties(); PhysicalProperties *ppB = spriteB->getPhysicalProperties(); int centerXA = boundingVolumeA->getCenterX(); int centerXB = boundingVolumeB->getCenterX(); int centerYA = boundingVolumeA->getCenterY(); int centerYB = boundingVolumeB->getCenterY(); int widthA = boundingVolumeA->getWidth(); int widthB = boundingVolumeB->getWidth(); int heightA = boundingVolumeA->getHeight(); int heightB = boundingVolumeB->getHeight(); int vxA = ppA->getVelocityX(); int vxB = ppB->getVelocityX(); int vyA = ppA->getVelocityY(); int vyB = ppB->getVelocityY(); // A - B if ((centerXB - (widthB/2)) > (centerXA + (widthA/2))) { //if(!((vxA - vxB) == 0)) { // if(boundingVolumeA->overlapsX(boundingVolumeB)){ if(!((vxA - vxB) == 0)) { int tx_first_contact = abs(((centerXB - (widthB/2)) - (centerXA + (widthA/2)))/(vxA - vxB)); int tx_last_contact = abs(((centerXB + (widthB/2)) - (centerXA - (widthA/2)))/(vxA - vxB)); if(tx_first_contact > percentageOfFrameRemaining) { //there`s no collision } else { if(!(vyA - vyB) == 0) { //there`s contact on X axis, so let`s see if there`s contact on y axis int ty_first_contact = abs(((centerYB - (heightB/2)) - (centerYA + (heightA/2)))/(vyA - vyB)); //int ty_first_contact = boundingVolumeA->overlapsY(boundingVolumeB); int ty_last_contact = abs(((centerYB + (heightB/2)) - (centerYA - (heightA/2)))/(vyA - vyB)); if(ty_first_contact > percentageOfFrameRemaining) { //there`s no collision } else { //there`s collision! //add collision to vector addCollisionSpriteSprite(spriteA, spriteB); } } else { //none of them are not going anywhere up, so they must collide on x axis //if they collide on y //if(((centerYB + heightB/2) - (centerYA - heightA/2)) >= 0) if(boundingVolumeA->overlapsY(boundingVolumeB)) {addCollisionSpriteSprite(spriteA, spriteB);} } // } } } else { //the situation here is kind of special //they could be stopped in X, or they could be walking in the same velocity //in the second situation, we are cool because they wouldn`t collide anyway //in the firt situation, they could colide (one on top of the other) //because of this, we have to make sure that one is on top of the other //and that they can collide on y axis //boundingVolumeB->setWidth(boundingVolumeB->getWidth() *2); if(boundingVolumeA->myOverlapsX(boundingVolumeB))// && !(boundingVolumeA->overlapsY(boundingVolumeB))) { //boundingVolumeB->setWidth(boundingVolumeB->getWidth()/2); addCollisionSpriteSprite(spriteA, spriteB); } } } // B - A else { if(!((vxB - vxA) == 0)) { int tx_first_contact = abs(((centerXA - (widthA/2)) - (centerXB + (widthB/2)))/(vxB - vxA)); //int tx_first_contact = boundingVolumeB->overlapsX(boundingVolumeA); int tx_last_contact = abs(((centerXA + (widthA/2)) - (centerXB - (widthB/2)))/(vxB - vxA)); if(tx_first_contact > percentageOfFrameRemaining) { //there`s no collision } else { if(!(vyB - vyA) == 0) { //there`s contact on X axis, so let`s see if there`s contact on y axis int ty_first_contact = abs(((centerYA - (heightA/2)) - (centerYB + (heightB/2)))/(vyB - vyA)); //int ty_first_contact = boundingVolumeB->overlapsY(boundingVolumeA); int ty_last_contact = abs(((centerYA + (heightA/2)) - (centerYB - (heightB/2)))/(vyB - vyA)); if(ty_first_contact > percentageOfFrameRemaining) { //there`s no collision } else { //there`s collision! //add collision to vector addCollisionSpriteSprite(spriteA, spriteB); } } else { //none of them are not going anywhere up, so they must collide on x axis //if they collide on y //if(((centerYA - heightA/2) - (centerYB + heightB/2)) <= 0 ) if(boundingVolumeB->overlapsY(boundingVolumeA)) {addCollisionSpriteSprite(spriteA, spriteB);} } } } else { //there`s no movement , so they won`t collide // boundingVolumeA->setWidth(boundingVolumeA->getWidth() *2); if(boundingVolumeB->myOverlapsX(boundingVolumeA))//&& !(boundingVolumeB->overlapsY(boundingVolumeA))) { addCollisionSpriteSprite(spriteA, spriteB); } } } // physics->addTileCollision(dynamicObject, testTile, tileX, tileY, (float)tileWidth, (float)tileHeight); }