Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using the Collision-Detection  (Read 3556 times)

0 Members and 1 Guest are viewing this topic.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Using the Collision-Detection
« 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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Using the Collision-Detection
« Reply #1 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 ;)
Mindiell
----

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Using the Collision-Detection
« Reply #2 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

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Using the Collision-Detection
« Reply #3 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?

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Using the Collision-Detection
« Reply #4 on: August 28, 2010, 03:59:00 pm »
No answers?

TheMagician

  • Newbie
  • *
  • Posts: 7
    • View Profile
Using the Collision-Detection
« Reply #5 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.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Using the Collision-Detection
« Reply #6 on: August 29, 2010, 11:40:58 pm »
Yes. That's how I solved it ;) Thanks anyway