bool Physics::willObjectsCollide(CollidableObject* coA, CollidableObject* coB) { PhysicalProperties* pp = coA->getPhysicalProperties(); BoundingVolume* bv = coA->getBoundingVolume(); float widthA = bv->getWidth(); float heightA = bv->getHeight(); float xA = pp->getX() + bv->getX(); float yA = pp->getY() + bv->getY(); float velXA = pp->getVelocityX(); float velYA = pp->getVelocityY(); float minXA = xA-(widthA/2); float maxXA = xA+(widthA/2); float minYA = yA-(heightA/2); float maxYA = yA+(heightA/2); if(velXA >= 0) maxXA += velXA; if(velXA < 0) minXA += velXA; if(velYA >= 0) maxYA += velYA; if(velYA < 0) minYA += velYA; pp = coB->getPhysicalProperties(); bv = coB->getBoundingVolume(); float widthB = bv->getWidth(); float heightB = bv->getHeight(); float xB = pp->getX() + bv->getX(); float yB = pp->getY() + bv->getY(); float velXB = pp->getVelocityX(); float velYB = pp->getVelocityY(); float minXB = xB-(widthB/2); float maxXB = xB+(widthB/2); float minYB = yB-(heightB/2); float maxYB = yB+(heightB/2); if(velXB >= 0) maxXB += velXB; if(velXB < 0) minXB += velXB; if(velYB >= 0) maxYB += velYB; if(velYB < 0) minYB += velYB; if( !(maxXB < minXA || minXB > maxXA || maxYB < minYA || minYB > maxYA)) return true; return false; }
/* clone - this method makes another BackAndForthBot object, but does not completely initialize it with similar data to this. Most of the object, like velocity and position, are left uninitialized. */ Bot* BackAndForthBot::clone() { BackAndForthBot *botClone = new BackAndForthBot(); PhysicalProperties* pp = this->getPhysicalProperties(); PhysicalProperties* bp = botClone->getPhysicalProperties(); BoundingVolume* pV = this->getBoundingVolume(); BoundingVolume* bV = botClone->getBoundingVolume(); botClone->setEnemy(enemy); botClone->setItem(item); botClone->setPortal(portal); botClone->setSpriteType(this->getSpriteType()); botClone->setAlpha(this->getAlpha()); botClone->setDead(false); bp->setCollidable(pp->isCollidable()); bp->setGravAffected(pp->isGravAffected()); bV->setHeight(pV->getHeight()); bV->setWidth(pV->getWidth()); bV->setX(pV->getX()); bV->setY(pV->getY()); return botClone; }
void Physics::resolveCollision(Game* game, Collision* currentCollision) { CollidableObject* co1 = currentCollision->getCO1(); CollidableObject* co2 = currentCollision->getCO2(); PhysicalProperties* pp; BoundingVolume* bv; AnimatedSprite* player = game->getGSM()->getSpriteManager()->getPlayer(); if(co2->isStaticObject() == true) { pp = co2->getPhysicalProperties(); bv = co2->getBoundingVolume(); float tX = pp->getX(); float tY = pp->getY(); float tXR = tX + bv->getWidth(); float tYB = tY + bv->getHeight(); pp = co1->getPhysicalProperties(); bv = co1->getBoundingVolume(); float x = pp->getX() + bv->getX() - (bv->getWidth()/2); float y = pp->getY() + bv->getY() - (bv->getHeight()/2); float xR = x+bv->getWidth(); float yB = y+bv->getHeight(); //pp->setVelocity(0, 0); /*if(x < tX) pp->setX(pp->getX() - 0.1); if(x > tX) pp->setX(pp->getX() + 0.1);*/ if(x >= tXR) { pp->setX(pp->getX() + 0.1); pp->setVelocity(0, pp->getVelocityY()); } if(xR <= tX) { pp->setX(pp->getX() - 0.1); pp->setVelocity(0, pp->getVelocityY()); } if(y >= tYB) { pp->setY(pp->getY() + 0.1); pp->setVelocity(pp->getVelocityX(), 0); } if(yB <= tY) { pp->setY(pp->getY() - 0.1); if(co1 == player) { pp->setJumped(false); pp->setDoubleJumped(false); pp->setStunned(false); } pp->setVelocity(pp->getVelocityX(), 0); } //if(currentCollision->getTOC() == currentCollision->getSYC()) //{ // /*if(pp->getVelocityY() < 0) // { // pp->setY(pp->getY() + 0.1); // } // else // { // pp->setY(pp->getY() - 0.1); // if(co1 == player) // { // pp->setJumped(false);s // pp->setDoubleJumped(false); // if(player->getCurrentState().compare(L"JUMP_STATE") == 0 // || player->getCurrentState().compare(L"JUMPL_STATE") == 0) // { // player->setCurrentState(L"IDLE_STATE"); // } // } // }*/ // if(y < tY) // { // pp->setY(pp->getY() - 0.1); // if(co1 == player) // { // pp->setJumped(false); // pp->setDoubleJumped(false); // /*if(player->getCurrentState().compare(L"JUMP_STATE") == 0 // || player->getCurrentState().compare(L"JUMPL_STATE") == 0) // { // player->setCurrentState(L"IDLE_STATE"); // }*/ // } // } // if(y > tY) // { // pp->setY(pp->getY() + 0.1); // // } // pp->setVelocity(pp->getVelocityX(), 0); // // //} //else if(currentCollision->getTOC() == currentCollision->getSXC()) //{ // /*if(pp->getVelocityX() < 0) // { // pp->setX(pp->getX() + 0.1); // } // else // { // pp->setX(pp->getX() - 0.1); // }*/ // /*if(x < tX) // pp->setX(pp->getX() - 0.1); // if(x > tX) // pp->setX(pp->getX() + 0.1);*/ // pp->setVelocity(0, pp->getVelocityY()); //} //else //{ // /*if(pp->getVelocityY() < 0) // pp->setY(pp->getY() + 0.1); // else // pp->setY(pp->getY() - 0.1); // if(pp->getVelocityX() < 0) // pp->setX(pp->getX() + 0.1); // else // pp->setX(pp->getX() - 0.1);*/ // pp->setVelocity(0,0); //} } else { pp = co1->getPhysicalProperties(); bv = co1->getBoundingVolume(); float x = pp->getX()+ bv->getX() - (bv->getWidth()/2); float y = pp->getY()+ bv->getY() - (bv->getHeight()/2); float xR = x + bv->getWidth(); float yB = y + bv->getHeight(); PhysicalProperties* p2 = co2->getPhysicalProperties(); bv = co2->getBoundingVolume(); float tX = p2->getX() + bv->getX() - (bv->getWidth()/2); float tY = p2->getY() + bv->getY() - (bv->getHeight()/2); float tXR = tX+bv->getWidth(); float tYB = tY+bv->getHeight(); //pp->setVelocity(0, 0); if(x >= tXR) { if(co1->isMobile()) pp->setX(pp->getX() + 0.1); if(co2->isMobile()) p2->setX(p2->getX() - 0.1); } if(xR <= tX) { if(co1->isMobile()) pp->setX(pp->getX() - 0.1); if(co2->isMobile()) p2->setX(p2->getX() + 0.1); } if(y >= tYB) { if(co1->isMobile()) pp->setY(pp->getY() + 0.1); if(co2->isMobile()) p2->setY(p2->getY() - 0.1); } if(yB <= tY) { if(co1->isMobile()) pp->setY(pp->getY() - 0.1); if(co2->isMobile()) p2->setY(p2->getY() + 0.1); } } }
void Physics::collideTestWithTiles(CollidableObject *c,TiledLayer *tL, list<Collision*> *collisions) { BoundingVolume *bv = c->getBoundingVolume(); float toRight= bv->getWidth()/2; float toBottom = bv->getHeight()/2; PhysicalProperties *pp = c->getPhysicalProperties(); float x = pp->getX()+bv->getX(); float y = pp->getY()+bv->getY(); float xVel = pp->getVelocityX(); float yVel = pp->getVelocityY(); float minX = x - toRight; float maxX = x + toRight; float minY = y - toBottom; float maxY = y + toBottom; if(xVel > 0) maxX += xVel; else minX += xVel; if(yVel > 0) maxY += yVel; else minY += yVel; int tW = tL->getTileWidth(); int tH = tL->getTileHeight(); int firstCol = minX/tW; int lastCol = maxX/tW; int firstRow = minY/tH; int lastRow = maxY/tH; if(firstCol < 0) firstCol = 0; if(firstRow < 0) firstRow = 0; if(lastCol >= tL->getColumns()) lastCol = tL->getColumns() - 1; if(lastRow >= tL->getRows()) lastRow = tL->getRows() - 1; for(int i = firstRow; i <= lastRow; i++) { for(int j = firstCol; j <= lastCol; j++) { Tile* current = tL->getTile(i,j); if(current->collidable == true) { if( !( (i+1)*tH < minY || i*tH > maxY || (j+1)*tW < minX || j*tW > maxX) ) { CollidableObject* tileCO = coStack[coStackCounter]; coStackCounter --; BoundingVolume *bv = tileCO->getBoundingVolume(); bv->setWidth(tW); bv->setHeight(tH); bv->setX(tW/2); bv->setY(tW/2); pp = tileCO->getPhysicalProperties(); pp->setPosition(j*tW,i*tH); /* Collision* currentCollision = collisionStack.top(); collisionStack.pop();*/ Collision* currentCollision = collisionStack[collisionStackCounter]; collisionStackCounter --; currentCollision->setCO1(c); currentCollision->setCO2(tileCO); currentCollision->calculateTimes(); collisions->push_back(currentCollision); } } } } }