Created
April 22, 2010 00:08
-
-
Save cpetzold/374618 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool Player::IsColliding(Map * m) { | |
bool topFirst = this->vel.y < 0; | |
bool leftFirst = this->vel.x < 0; | |
int width = this->mask->GetWidth(); | |
int height = this->mask->GetHeight(); | |
sf::IntRect maskAABB = this->GetAABB(this->prov); | |
sf::IntRect mapAABB = m->GetAABB(); | |
if (maskAABB.Intersects(mapAABB)) { | |
sf::Vector2f mapV; | |
int x = leftFirst ? 0 : width-1; | |
int y = topFirst ? 0 : height-1; | |
for (int i = maskAABB.Left; i < maskAABB.Right; i++) { | |
for (int j = maskAABB.Top; j < maskAABB.Bottom; j++) { | |
//cout << "x: " << x << " y: " << y << endl; | |
mapV = ((sf::Sprite *)m)->TransformToLocal(sf::Vector2f(i, j)); | |
//If both sprites have opaque pixels at the same point we've got a hit | |
if ((((sf::Sprite *)m)->GetPixel(mapV.x, mapV.y).a != sf::Color(0,0,0,0).a) && | |
(this->mask->GetPixel(x, y).a != sf::Color(0,0,0,0).a)) { | |
this->last.x = x; | |
this->last.y = y; | |
return true; | |
} | |
y = topFirst ? y+1 : y-1; | |
} | |
y = topFirst ? 0 : height-1; | |
x = leftFirst ? x+1 : x-1; | |
} | |
return false; | |
} | |
return false; | |
} | |
void Player::HandleCollision() { | |
//cout << "collision at (" << this->last.x << ", " << this->last.y << ")" << endl; | |
//cout << "mask pos (" << this->pos.x + this->off.x << ", " << this->pos.y + this->off.y << ")" << endl; | |
//sf::IntRect maskAABB = this->GetAABB(this->prov); | |
this->vel.y = 0; | |
this->vel.x *= this->fric; | |
this->pos += (this->prov + this->off) - this->last; | |
this->SetPosition(this->pos.x, this->pos.y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment