Game Development Reference
In-Depth Information
Time for action - adding collision
detection
Let's see how that translates to code:
1. Still in Terrain.cpp :
void Terrain::checkCollision (Player * player) {
if (player->getState() == kPlayerDying) return;
bool inAir = true;
for (auto block : _blocks) {
if (block->getType() == kBlockGap) continue;
//if within x, check y (bottom collision)
if (player->right() >= this->getPositionX() +
block->left() && player->left() <=
this->getPositionX() + block->right()) {
if (player->bottom() >= block->top() &&
player->next_bottom() <= block->top() && player->top()
> block->top()) {
player->setNextPosition(Vec2(player->getNextPosition().x,
block->top() + player->getHeight()));
player->setVector (
Vec2(player->getVector().x, 0) );
player->setRotation(0.0);
inAir = false;
break;
}
}
}
First we state that the _player object is currently falling with inAir =
true; we'll let the collision check determine if this will remain true or not.
Search WWH ::




Custom Search