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

Author Topic: Collision shapes for SFML  (Read 11805 times)

0 Members and 1 Guest are viewing this topic.

greeniekin

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Collision shapes for SFML
« 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



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.
« Last Edit: November 07, 2012, 05:00:53 pm by greeniekin »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Collision shapes for SFML
« Reply #1 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, 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

greeniekin

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Collision shapes for SFML
« Reply #2 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;
« Last Edit: November 07, 2012, 07:24:33 pm by greeniekin »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision shapes for SFML
« Reply #3 on: November 07, 2012, 06:44:21 pm »
You are talking about the dot product, aren't you? :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

greeniekin

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Collision shapes for SFML
« Reply #4 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.
« Last Edit: November 07, 2012, 07:38:26 pm by greeniekin »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Collision shapes for SFML
« Reply #5 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 ;)

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Collision shapes for SFML
« Reply #6 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 data structure can help
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Collision shapes for SFML
« Reply #7 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.
I have many ideas but need the help of others to find way to make use of them.

 

anything