SFML community forums
Help => General => Topic started by: Loopback on April 26, 2014, 01:27:03 pm
-
Hi there! I'm having a bit of trouble trying to detect collisions (sprite with tmx::objects or other sprites). I know how to detect if its colliding but what I'd like to know is if f.e my character's sprite is colliding with an object at his right, left, bottom or down. How can I get it done? is there any way of doing it?
thank you very much for your help and hope I've explained my self properly
-
When a collision happens, get the centers of the two colliding objects (I'm assuming you know their positions and sizes), and compute the angle between them. This will require basic trigonometry. Standard functions like sin/cos are probably enough, though I think Thor has some useful vector arithmetic methods.
-
Well, I think I can know its size by using the getAABB() function and position using getPosition();
I will give it a shot to see what can I get done
-
There are a number of ways to do it.
- Rectangular collision: use sf::Rect::intersects
- Circular collision: compute the distance between the centers of the object, if it's smaller than the sum of the radius of both circles, they collide
- Pixel perfect collisions, not recommended since it's slow and probably overkill for most situations
The most difficult part is actually comparing rectangles with circles (or I've missed something). Since most objects can be reduced to a rectangle and circle (or combinations) it not too hard.
If you are working with tiles you can simply flag a tile as 'collidable' and when anything tries to move to its location, handle the collision.
Or you could use a library. Here (http://stackoverflow.com/questions/14915772/which-game-physics-engine-should-i-use) are a couple of them listed.