SFML community forums

General => SFML projects => Topic started by: greeniekin on November 07, 2012, 04:59:06 pm

Title: Collision shapes for SFML
Post by: greeniekin on November 07, 2012, 04:59:06 pm
With the talk of the SFML books I was thinking about collision detection and a simple solution.
I have created a function at the moment which accepts sfml shape objects. So you can also draw the shapes your using for debugging or in the actual game.
It works with all the shapes transformations. Such as translation, scale and rotation.
The actual code for this is about 100 lines
This is an example of an person collides with another object it will keep the object out of the wall.
    sf::ConvexShape polygon;
    sf::CircleShape person;
   ....
    sf::Vector3f intersect;
    sf::Vector2f pos;
    if(collision(person,polygon,intersect,pos))//order of shapes is important as intersect direction will be reversed
    {
         person.move(intersect.x*intersect.z,intersect.y*intersect.z);
    }
 

Here is a little demo I was testing. Warning the physics is not even an attempt at proper physics. Though the collision detection works well and you could expand on this with whatever logic you want.

http://www.youtube.com/watch?v=qz_-AqdDK5I&feature=youtu.be (http://www.youtube.com/watch?v=qz_-AqdDK5I&feature=youtu.be)



I am not sure what the plan should be next. Should I create Classes for these and that way classes can contain multiple convexShapes to make more usefull collision objects. Should I go as far as some lightweight physics? In any case I am not going to have it do any scene management.
Title: Re: Collision shapes for SFML
Post by: eXpl0it3r on November 07, 2012, 05:53:13 pm
I am not sure what the plan should be next. Should I create Classes for these and that way classes can contain multiple convexShapes to make more usefull collision objects. Should I go as far as some lightweight physics? In any case I am not going to have it do any scene management.
Well it's your code, you should decide what you want to do with it. ;D

It would be quite nice if you'd clean up the classes and put them on the official wiki (https://github.com/SFML/SFML/wiki), so others could use it too. :)

What collision detection are you using in the back?

Btw. how did the talk about the book motivate you to create this collision class?
Title: Re: Collision shapes for SFML
Post by: greeniekin on November 07, 2012, 06:27:13 pm
Well the talk of the book motivated me because I was thinking what would be good things to teach. Powerful yet learn-able.

The method I am using is very simple. The idea is pretty much the idea ofgetting down low and looking across the top of a table to see if there is any distance between the 2 objects.
And this is done for every single face of an object. The shapes have to be convex, as the check for distance from the line or plane is to infinity.

this is basically used to convert each points position to height from line/plane.
h = normal.x * point.x + normal.y *  point.y;//+d could be added so 0 is the actual position of the plane.

I am not describing it well. I need pictures and I can not remember the name.

Essentially the idea is changing the perspective to face down the line. Then you only need to worry about using one axis.

I originally saw it with 3d planes using
h = normal.x *  point.x + normal.y *  point.y + normal.z *  point.z+d;
Title: Re: Collision shapes for SFML
Post by: Nexus on November 07, 2012, 06:44:21 pm
You are talking about the dot product, aren't you? :)
Title: Re: Collision shapes for SFML
Post by: greeniekin on November 07, 2012, 07:35:46 pm
well yes. Though I feel that does not have much meaning.  In my mind this makes more sense. Though my mind is a scary place. Though I am not wrong either.
Title: Re: Collision shapes for SFML
Post by: didii on November 11, 2012, 02:04:51 am
Very nice! When the tutorials of 2.0 are updated I want to try to make this too :) I don't know enough of SFML to be able to implement this I think.
Problem I have with this implementation is that if I had lots of moving objects in my world, it seems highly inefficient to check all collisions (object+1 = length²), because I have the feeling that a collision checker is a heavy operation. Is it?

Btw, if you want some help with physics, don't hesitate to ask. That's my field of study ;)
Title: Re: Collision shapes for SFML
Post by: mateandmetal on November 11, 2012, 08:52:14 pm
Problem I have with this implementation is that if I had lots of moving objects in my world, it seems highly inefficient to check all collisions (object+1 = length²), because I have the feeling that a collision checker is a heavy operation. Is it?

Maybe a Quadtree (http://en.wikipedia.org/wiki/Quadtree) data structure can help
Title: Re: Collision shapes for SFML
Post by: StormWingDelta on November 11, 2012, 11:54:06 pm
I think I still have an old collision data making lib from back when I used to use Digipen's Fun Editor for making games.


It might help speed things along for you since it has most of what you're trying to do already laid out.

I can dig it up if you want.