1
General / Need help with collisions with player that moves towards mouse
« on: November 11, 2014, 02:45:41 pm »
Hello everyone,
I just finished my first year at uni and am now on a 4 month break, so i thought, what great time to learn SFML and more c++ (have been doing it for about 7 months).
I made a space invaders style game no problem so i thought i would try something a little more challenging and make a top down zombie survival type game. So far it is going quiet well except i am having issues with making the character stop move in a particular direction if they collide with another sprite/shape.
the character moves towards the mouse when the W key is pressed and away from the mouse when the S key is pressed.
i tried this for my collisions:
That code works to an extent, but when the player turns close to the object and the back corner of the player hit the object, they go shooting straight through it as the rotation angle is not correct any more.
this is my movement code and how i set the roation angle
And my movement code
and the code where the above function is called (this is from the player.update())
if anyone could help guide me in the right direction of how i would go about doing a better collision so the character can't pass through and object no matter what, it would be greatly appreciated
I just finished my first year at uni and am now on a 4 month break, so i thought, what great time to learn SFML and more c++ (have been doing it for about 7 months).
I made a space invaders style game no problem so i thought i would try something a little more challenging and make a top down zombie survival type game. So far it is going quiet well except i am having issues with making the character stop move in a particular direction if they collide with another sprite/shape.
the character moves towards the mouse when the W key is pressed and away from the mouse when the S key is pressed.
i tried this for my collisions:
void Player::restrictMovement(sf::RectangleShape & shape){
sf::FloatRect bb1 = playerSprite.getGlobalBounds();
sf::FloatRect bb2 = shape.getGlobalBounds();
if (bb1.intersects(bb2)){
if (rotationAngle >= 0 && rotationAngle < 90){
canMoveForward = false;
playerSprite.move(-8.5, -8.5);
}
else if (rotationAngle > 90 && rotationAngle < 180){
canMoveForward = false;
playerSprite.move(8.5, -8.5);
}
else if (rotationAngle > 180 && rotationAngle < 270){
canMoveForward = false;
playerSprite.move(8.5f, 0.0f);
}
else if (rotationAngle > 270 && rotationAngle < 360){
canMoveForward = false;
playerSprite.move(-8.5, 8.5);
}
}
}
sf::FloatRect bb1 = playerSprite.getGlobalBounds();
sf::FloatRect bb2 = shape.getGlobalBounds();
if (bb1.intersects(bb2)){
if (rotationAngle >= 0 && rotationAngle < 90){
canMoveForward = false;
playerSprite.move(-8.5, -8.5);
}
else if (rotationAngle > 90 && rotationAngle < 180){
canMoveForward = false;
playerSprite.move(8.5, -8.5);
}
else if (rotationAngle > 180 && rotationAngle < 270){
canMoveForward = false;
playerSprite.move(8.5f, 0.0f);
}
else if (rotationAngle > 270 && rotationAngle < 360){
canMoveForward = false;
playerSprite.move(-8.5, 8.5);
}
}
}
That code works to an extent, but when the player turns close to the object and the back corner of the player hit the object, they go shooting straight through it as the rotation angle is not correct any more.
this is my movement code and how i set the roation angle
void Player::setRotation(sf::RenderWindow & w){
sf::Vector2f playerPos = playerSprite.getPosition();
curPos = sf::Mouse::getPosition(w);
float dX = playerPos.x - curPos.x;
float dY = playerPos.y - curPos.y;
rotationAngle = (atan2(dY, dX)) * 180 / PI + 180;
}
sf::Vector2f playerPos = playerSprite.getPosition();
curPos = sf::Mouse::getPosition(w);
float dX = playerPos.x - curPos.x;
float dY = playerPos.y - curPos.y;
rotationAngle = (atan2(dY, dX)) * 180 / PI + 180;
}
And my movement code
if (canMoveForward == true){
playerSprite.move(-cos(val) * movementSpeed, sin(val) *-movementSpeed);
}
if (canMoveBackward == true){
playerSprite.move(cos(val) * movementSpeed / 2, -sin(val) *-movementSpeed / 2);
}
playerSprite.move(-cos(val) * movementSpeed, sin(val) *-movementSpeed);
}
if (canMoveBackward == true){
playerSprite.move(cos(val) * movementSpeed / 2, -sin(val) *-movementSpeed / 2);
}
and the code where the above function is called (this is from the player.update())
float angle = (playerSprite.getRotation() + 180) * PI / 180;
playerSprite.setRotation(rotationAngle);
movement(angle);
playerSprite.setRotation(rotationAngle);
movement(angle);
if anyone could help guide me in the right direction of how i would go about doing a better collision so the character can't pass through and object no matter what, it would be greatly appreciated