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

Author Topic: A question about collision detection  (Read 2372 times)

0 Members and 1 Guest are viewing this topic.

caelestis

  • Newbie
  • *
  • Posts: 4
    • View Profile
A question about collision detection
« on: January 20, 2010, 10:42:25 pm »
I made my collision detection system so that when the window Draw sprites, it will check if it collided with anything and set a flag to alert that sprite. The sprite is set to move itself every time it is drawn, but sometimes, it will move past an object a small amount. This is because I've set it to move to the spot past the wall and then check to see if it collided afterwards, which is not the effect I wanted.

I'm trying to think about ways to fix this. Should I make it so that it moves only 1 pixel each time and then check collision of the sprite or perhaps it should only start moving by one pixel when it anticipates that the next point it moves to will be a collision? I'm worried about moving one pixel because I think it might be an inefficient and inelegant design. Any suggestions?

Oh, and one more thing. I'm having to calculate a new bounding box each time I want to use the collision detection function, getting it from the offsets+GetSubRect and I think this is also a clunky design.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
A question about collision detection
« Reply #1 on: January 20, 2010, 11:00:57 pm »
Calculating a new bbox every time shouldn't be much of a problem. If you have over 1000 collision checks per frame, or around there, it might cause a little lag. What I do is get the width and height when the sprite is changed, store that, and then only get the x and y positions, add the width and height to those, and subtract 1 to get the right and bottom boundaries.

You will most likely want to do one pixel at a time, especially in platformers. It will reduce the number of possible glitches significantly, and shouldn't cause too much lag, depending on how you check for collisions.
I use the latest build of SFML2

caelestis

  • Newbie
  • *
  • Posts: 4
    • View Profile
A question about collision detection
« Reply #2 on: January 20, 2010, 11:10:54 pm »
I thought of another design in which when the next move will be a possible collision, the bounding box of the wall gets passed to the sprite and it will calculate how much left it has to move. Either that or I do above and iterate moves of one pixel until I reach target destination (which would be bad because it would calculate boxes over and over every move of one pixel)

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
A question about collision detection
« Reply #3 on: January 20, 2010, 11:57:19 pm »
Quote from: "caelestis"
I thought of another design in which when the next move will be a possible collision, the bounding box of the wall gets passed to the sprite and it will calculate how much left it has to move.


That's a good option in my opinion.
Check the move before execute it (specially before drawing it). If there is a wall intersecting the move, move just the distance between the current position and the wall.
You just have to make sure that you never move more pixels than the wall and the sprite bounding boxes together, which should never happen unless your sprite moves really really fast, or they are really really too small! In these situations the 1 pixel move might be a better choice.
Pluma - Plug-in Management Framework

 

anything