SFML community forums

Help => General => Topic started by: Finn on August 27, 2010, 02:42:32 pm

Title: Using the Collision-Detection
Post 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:
Code: [Select]

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
Title: Using the Collision-Detection
Post by: Mindiell on August 27, 2010, 04:37:22 pm
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 ;)
Title: Using the Collision-Detection
Post by: Finn on August 27, 2010, 06:14:57 pm
THX! :D Works without any problems :D
This is how I solved it together with backgroundscrolling:

Code: [Select]

   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
Title: Using the Collision-Detection
Post by: Finn on August 27, 2010, 06:35:21 pm
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?
Title: Using the Collision-Detection
Post by: Finn on August 28, 2010, 03:59:00 pm
No answers?
Title: Using the Collision-Detection
Post by: TheMagician on August 28, 2010, 04:52:27 pm
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.
Title: Using the Collision-Detection
Post by: Finn on August 29, 2010, 11:40:58 pm
Yes. That's how I solved it ;) Thanks anyway