Hi..
I can not seem to manage collisions in my program. I have a character that I move with the mouse, and I have another square that has a fixed position.
I managed to handle collision for the right and left side of the character, but when I try to manage those at the top and bottom, my program is still doing collision for the left and right .. This is hard to explain for a beginner and a French like me so I did a little simple video:
http://videobam.com/JnqjKThis is my code :
class Player : public sf::RectangleShape {
private:
sf::Texture m_playerTexture;
float m_playerSpeed;
public:
Player();
void update(sf::Vector2i mousePosition, Level *level);
};
void Player::update(sf::Vector2i mousePosition, Level *level) {
sf::Vector2f position = getPosition();
m_delta.x = mousePosition.x - position.x;
m_delta.y = mousePosition.y - position.y;
move(m_delta.x * m_playerSpeed, m_delta.y * m_playerSpeed);
sf::FloatRect playerRect = getGlobalBounds();
sf::FloatRect levelTemp;
levelTemp.top = 10 * CELL_SIZE;
levelTemp.left = 10 * CELL_SIZE;
levelTemp.width = levelTemp.height = CELL_SIZE;
sf::FloatRect intersection;
if (playerRect.intersects(levelTemp, intersection)) {
if (playerRect.left + PLAYER_SIZE / 2 > levelTemp.left + CELL_SIZE / 2) {
move(intersection.width, 0);
}
else if (playerRect.left + PLAYER_SIZE / 2 < levelTemp.left + CELL_SIZE / 2) {
move(-intersection.width, 0);
}
else if (playerRect.top + PLAYER_SIZE / 2 > levelTemp.top + CELL_SIZE / 2) {
move(0, intersection.height);
}
else if (playerRect.top + PLAYER_SIZE / 2 < levelTemp.top + CELL_SIZE / 2) {
move(0, -intersection.height);
}
}
}
I try to do it with a single square before doing so for the full levelI hope you can help me. I tried and tried but I can not find a solution.
Thank you in advance!