1
Graphics / Re: Collision with intersects
« on: June 21, 2014, 03:50:24 am »
Finally : http://videobam.com/JsqXw
Now I'm trying to do it with more square. But it does not work as expected. The player behaves oddly when in collision with multiple tiles. There's a video : http://videobam.com/sNYzG
Collisions are only possible with the 8 tiles surrounding the player
Now I'm trying to do it with more square. But it does not work as expected. The player behaves oddly when in collision with multiple tiles. There's a video : http://videobam.com/sNYzG
void Player::update(sf::Vector2i mousePosition, Level *level) {
m_position = getPosition();
m_delta.x = mousePosition.x - m_position.x;
m_delta.y = mousePosition.y - m_position.y;
move(m_delta.x * m_playerSpeed, m_delta.y * m_playerSpeed);
sf::FloatRect playerRect = getGlobalBounds();
int startX = (int)(m_position.x / CELL_SIZE - 1);
int startY = (int)(m_position.y / CELL_SIZE - 1);
int endX = startX + 2;
int endY = startY + 2;
sf::FloatRect levelTemp(0, 0, CELL_SIZE, CELL_SIZE);
sf::FloatRect area;
for (int y = startY; y <= endY; y++) {
for (int x = startX; x <= endX; x++) {
if (level->get(x,y) == 1) {
levelTemp.left = x * CELL_SIZE;
levelTemp.top = y * CELL_SIZE;
if (playerRect.intersects(levelTemp, area)) {
if (area.width > area.height) {
if (playerRect.top < levelTemp.top + levelTemp.width &&
playerRect.top > levelTemp.top) {
move(0, area.height);
}
else if (playerRect.top + playerRect.height > levelTemp.top &&
playerRect.top + playerRect.height < levelTemp.top + levelTemp.height) {
move(0, -area.height);
}
}
else if (area.width < area.height) {
if (playerRect.left < levelTemp.left + levelTemp.width &&
playerRect.left > levelTemp.left) {
move(area.width, 0);
}
else if (playerRect.left + playerRect.width > levelTemp.left &&
playerRect.left + playerRect.width < levelTemp.left + levelTemp.width) {
move(-area.width, 0);
}
}
}
}
}
}
}
m_position = getPosition();
m_delta.x = mousePosition.x - m_position.x;
m_delta.y = mousePosition.y - m_position.y;
move(m_delta.x * m_playerSpeed, m_delta.y * m_playerSpeed);
sf::FloatRect playerRect = getGlobalBounds();
int startX = (int)(m_position.x / CELL_SIZE - 1);
int startY = (int)(m_position.y / CELL_SIZE - 1);
int endX = startX + 2;
int endY = startY + 2;
sf::FloatRect levelTemp(0, 0, CELL_SIZE, CELL_SIZE);
sf::FloatRect area;
for (int y = startY; y <= endY; y++) {
for (int x = startX; x <= endX; x++) {
if (level->get(x,y) == 1) {
levelTemp.left = x * CELL_SIZE;
levelTemp.top = y * CELL_SIZE;
if (playerRect.intersects(levelTemp, area)) {
if (area.width > area.height) {
if (playerRect.top < levelTemp.top + levelTemp.width &&
playerRect.top > levelTemp.top) {
move(0, area.height);
}
else if (playerRect.top + playerRect.height > levelTemp.top &&
playerRect.top + playerRect.height < levelTemp.top + levelTemp.height) {
move(0, -area.height);
}
}
else if (area.width < area.height) {
if (playerRect.left < levelTemp.left + levelTemp.width &&
playerRect.left > levelTemp.left) {
move(area.width, 0);
}
else if (playerRect.left + playerRect.width > levelTemp.left &&
playerRect.left + playerRect.width < levelTemp.left + levelTemp.width) {
move(-area.width, 0);
}
}
}
}
}
}
}
Collisions are only possible with the 8 tiles surrounding the player