Author Topic: Collisions?  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.

Loopback

• Newbie
• Posts: 10
Collisions?
« 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

Ixrec

• Hero Member
• Posts: 1241
Re: Collisions?
« Reply #1 on: April 26, 2014, 01:45:29 pm »
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.

Loopback

• Newbie
• Posts: 10
Re: Collisions?
« Reply #2 on: April 26, 2014, 07:11:32 pm »
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

didii

• Full Member
• Posts: 122
Re: Collisions?
« Reply #3 on: April 28, 2014, 01:06:05 pm »
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 are a couple of them listed.

anything