SFML community forums
Help => General => Topic started by: Finn on August 27, 2010, 02:42:32 pm
-
Hi
I'm using this collision-class http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection !
It works perfect! ;)
But now I try to figure out how to use this in the way, that my character can't walk through an other sprite...
I tried something like this:
const sf::Input& Input = window.GetInput();/
float ResetValue = 1.0f;
float XPos = Player.Sprite.GetPosition().x;//Position of the player sprite
float YPos = Player.Sprite.GetPosition().y;
if(Input.IsKeyDown(sf::Key::Right)
if(test.PixelPerfectTest(Player.Sprite,TestBox))//Testbox is a sprite
Player.Sprite.SetPosition(XPos-ResetValue,YPos);
else
Player.Sprite.SetPosition(XPos+Player.Speed,YPos);
But this is ugly. If the player is colliding it shakes back and forward. Also It seems this is a little bit buggy.
Is there a better way to do this?
Greets,
Finn
-
Your ResetValue is bad. You have to test collision and take back your sprite to its old position if there is a collision.
Right now, you are testing the actual position which is not good, because it must never be in collision ;)
-
THX! :D Works without any problems :D
This is how I solved it together with backgroundscrolling:
if(Input.IsKeyDown(sf::Key::Right))
{
Player.ChangeAnim(2);
Player.PlayAnim();
Player.Sprite.SetPosition(XPos+Player.Speed,YPos);
GameView.Move(Player.Speed,0);
if(test.PixelPerfectTest(Player.Sprite,CBox))//if collision
{
Player.Sprite.SetPosition(XPos,YPos);//reset the sprite
GameView.Move(-Player.Speed,0);//reset the map
}
return;
}
same for the other directions...
Thx,
Finn
-
So...to go on with this, how to do this for multiple sprites? For example for every sprite on the gamemap?
Put all the sprites into a vector?
-
No answers?
-
Yeah, you could put all the sprites into a vector or list and where you check for collisions you would iterate through the container and check for collision for each sprite.
-
Yes. That's how I solved it ;) Thanks anyway