SFML community forums

Help => General => Topic started by: aNewHobby on July 21, 2012, 04:28:34 am

Title: Collisions Fail at higher speed....
Post by: aNewHobby on July 21, 2012, 04:28:34 am
I'm using a bunch of collision tests. Everything works great.. but if I am going to fast then the collisions simply fail and nothing happens. I am trying to make a pacman clone so I would like pac-man to move pretty fast by the end of a long game.



So yeah.. I would love to hear any ideas on this!

Thanks in advance

Example of Collision as it is now...

 if ( _rectSwitch.getGlobalBounds().contains( PacMan.getPosition().x, PacMan.getPosition().y ) )
Title: Re: Collisions Fail at higher speed....
Post by: Canvas on July 21, 2012, 05:03:59 am
There isnt really enough code to see exactly what is going on, but one thing I did see in my code some time ago was make sure you have your collision detection check before the movement of the "object", if you move the character then check you will go through anything if they have the correct speed. if you check because the player moves then it will see the collision and stop, that might not be the correct way, but it did help me once apon a time :)
Title: Re: Collisions Fail at higher speed....
Post by: eXpl0it3r on July 21, 2012, 09:13:44 am
Unfortunatly that's a normal problem with collision detection. There a few solution for it. The easiest is to increase the collision box to a size where you can guarantee that one movement step can't just simply pass the collision box (e.g. the box is 20px and one movement is 30px, thus the sprite just 'jumps' over the collision rectangle).
Another solution is not just to check intersection, but to actually calculate when the collision would happen. This can be quite tricky though.
Title: Re: Collisions Fail at higher speed....
Post by: WiltedChameleon on July 24, 2012, 04:56:17 pm
Expanding on exploiter's post you could do a rectangle where X is the sprite's X position, minus any negative horizontal velocity, Y is the sprite's Y position minus any negative vertical velocity, the width is the sprite's width add any positive horizontal velocity and the height is the sprite's height add any horizontal velocity.

It's not overly efficient, and it requires a bit more math (Although god help your computer if a few basic math expressions make a difference), but in the end of the day it's a simpler solution than some, especially if you're just dealing with regular rectangles like in a pacman game.