1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
void PhysicsRect::updateCollisions(vector<PhysicsObject*> objectVector){
_collision.clear();
for (int i = 0; i < objectVector.size(); i++){
//Collision finding code for 2 rectangles.
if (this != objectVector[i] && objectVector[i]->identify() == ObjectType::rectangle){
//Check if colliding
if (contains(objectVector[i]) && collisionExists(objectVector[i], _collision) == false){
//Determine side of collision
long double leftPenetration = getRight() - objectVector[i]->getLeft();
long double rightPenetration = objectVector[i]->getRight() - getLeft();
long double topPenetration = getBottom() - objectVector[i]->getTop();
long double bottomPenetration = objectVector[i]->getBottom() - getTop();
if (leftPenetration < rightPenetration && leftPenetration < topPenetration && leftPenetration < bottomPenetration){
_collision.push_back(Collision(*this, *objectVector[i], SideOfCollision::right));
if (collisionExists(objectVector[i], _collisionsThisFrame) == false){
_collisionsThisFrame.push_back(Collision(*this, *objectVector[i], SideOfCollision::right));
}
}
else if (rightPenetration < leftPenetration && rightPenetration < topPenetration && rightPenetration < bottomPenetration){
_collision.push_back(Collision(*this, *objectVector[i], SideOfCollision::left));
if (collisionExists(objectVector[i], _collisionsThisFrame) == false){
_collisionsThisFrame.push_back(Collision(*this, *objectVector[i], SideOfCollision::left));
}
}
else if (topPenetration < rightPenetration && topPenetration < leftPenetration && topPenetration < bottomPenetration){
_collision.push_back(Collision(*this, *objectVector[i], SideOfCollision::bottom));
if (collisionExists(objectVector[i], _collisionsThisFrame) == false){
_collisionsThisFrame.push_back(Collision(*this, *objectVector[i], SideOfCollision::bottom));
}
}
else if (bottomPenetration < rightPenetration && bottomPenetration < topPenetration && bottomPenetration < leftPenetration){
_collision.push_back(Collision(*this, *objectVector[i], SideOfCollision::top));
if (collisionExists(objectVector[i], _collisionsThisFrame) == false){
_collisionsThisFrame.push_back(Collision(*this, *objectVector[i], SideOfCollision::top));
}
}
}
}
}
}
| |