SFML community forums

General => SFML projects => Topic started by: zorexx on March 20, 2012, 06:19:00 pm

Title: SolidShaper + Box2D + SFML
Post by: zorexx on March 20, 2012, 06:19:00 pm
So, I wrote an application a while ago (SolidShaper: http://www.zorexxlkl.com/solidshaper (http://www.zorexxlkl.com/solidshaper)), and I'm currently working on a sample for it which uses SFML and Box2D.

Basically it's an application that allows you to create shapes for use in collision detection/physics engines. In other words, with this application, you can create shapes directly for your physics engine without using some workarounds like loading from svg files.

This sample demonstrates loading shape from an ssf file into Box2D bodies and displaying it with SFML shapes.
Properties for bodies and shapes can be set from SolidShaper directly, including density, friction, color, etc.

Here's a video of it in action:
http://youtu.be/MKFExir0X-Q (http://youtu.be/MKFExir0X-Q)

Source code available here:
http://www.zorexxlkl.com/files/downloads/SSFBox2DTest-1.0.1.zip (http://www.zorexxlkl.com/files/downloads/SSFBox2DTest-1.0.1.zip)
Title: [WIP] SolidShaper + Box2D + SFML
Post by: julen26 on March 20, 2012, 08:34:57 pm
Nice! I was thinking to do something like this merged to my generic map editor. But this could be my solution.

I've detected some interface errors, but it's Ok. I like the idea of making maps with collision shapes.

Good luck!

By the way, wich libary do you use for the GUI?
Title: [WIP] SolidShaper + Box2D + SFML
Post by: zorexx on March 21, 2012, 02:29:15 am
Thanks. Hope you'll like it.

May I know what interface errors you're talking about?

SolidShaper is written in C# .NET with WinForms.
Title: [WIP] SolidShaper + Box2D + SFML
Post by: julen26 on March 21, 2012, 03:58:54 pm
Yes for example:

 - Create a polygon and then select it.
 - Then select "Extend" radio button and see how the new point moves with mouse.
 - Then come back to the menu and select another radio button.
 - Then clic anywhere.

I get something like this.
https://legacy.sfmluploads.org/cache/pics/231_ss.png

It's difficult to get these errors, but could happen.
Cheers.
Title: [WIP] SolidShaper + Box2D + SFML
Post by: zorexx on March 21, 2012, 04:55:20 pm
Ahh, thanks for reporting.

Fixed it, will be available in the next version.

If you spot any other errors/problems (even minor ones), please let me know, thanks!  :D
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on March 25, 2012, 07:30:05 am
Completed the sample.
Here's the latest video:
http://youtu.be/MKFExir0X-Q (http://youtu.be/MKFExir0X-Q)

and the source code:
http://www.zorexxlkl.com/files/downloads/SSFBox2DTest-1.0.0.zip (http://www.zorexxlkl.com/files/downloads/SSFBox2DTest-1.0.0.zip)

Be warned though, the code isn't very tidy, if you want to use the codes, you'll probably have to modify it to suit your needs.
Title: Re: SolidShaper + Box2D + SFML
Post by: Astrof on March 26, 2012, 07:13:24 pm
This is cool! I haven't gotten a chance to test it yet (having some Visual Studio trouble as I haven't messed with it in about a year) but I was wondering, could I potentially use this to create/draw shapes for my game? One of my game ideas revolves around all the sprites to use shapes for drawing/collision and the only reason I haven't gotten to actually doing it is I thought it would be easier to have some kind of map editor to draw and save the shapes but I've been lazy and never got around to it. 
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on March 27, 2012, 04:26:37 am
could I potentially use this to create/draw shapes for my game?

Sure! That's what it's made for.
Good luck on your game!  ;)
Title: Re: SolidShaper + Box2D + SFML
Post by: Bigz on March 27, 2012, 02:10:48 pm
Interesting project ! I'll try it when I'll get time for.

I did something a bit similar, based on SVG files, but it's mostly a parser than something more sophisticated as you did. I posted it on the french part of the forum, (here -> http://www.sfml-dev.org/old-forum-fr/viewtopic.php?t=4178 ) but maybe I should also post it here.
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on March 27, 2012, 02:49:09 pm
Cool, too bad I don't understand French.

Hope you'll like it (and find it useful).  :)
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on March 30, 2012, 02:30:22 pm
Just a minor update on SSFile:

SSFile for Java and Python are released.

Downloads available on the project page: http://www.zorexxlkl.com/solidshaper (http://www.zorexxlkl.com/solidshaper)

Edit:
SolidShaper 1.0.1 released.
This release contains several minor bugfixes and improvements.
Title: Re: SolidShaper + Box2D + SFML
Post by: Astrof on April 05, 2012, 04:18:14 am
I've been messing with Solid Shaper and have a couple questions:
First: When I'm drawing a shape, what happens when things overlap?

Second: Can I request a feature? Would it be possible to make a function that takes a set of points and draws a triangle strip? I tried to outline one of my sprites and it has a lot of concave sections.  Basically the function would take a list of points in the order they were drawn and figures out a triangle shape pattern.  I'm not sure how complicated that would be but it would be a perfect feature to outline sprites (the current way I have to draw it just right to get a proper outline). 
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on April 05, 2012, 07:10:04 am
First: When I'm drawing a shape, what happens when things overlap?

The last shape drawn or selected will be at the top, use right click to select if you want to select anything below that, this will first select the topmost, then the one below that, and so on.

Second: Can I request a feature? Would it be possible to make a function that takes a set of points and draws a triangle strip? I tried to outline one of my sprites and it has a lot of concave sections.  Basically the function would take a list of points in the order they were drawn and figures out a triangle shape pattern.  I'm not sure how complicated that would be but it would be a perfect feature to outline sprites (the current way I have to draw it just right to get a proper outline). 

That's called triangulation.
It's possible to break polygons into triangles with triangulation, but not into triangle strip.
Since SolidShaper does not have any object-like feature to group shapes together, if I included a triangulation feature, it will break it down to triangles, but then you cannot move them as a set anymore, you'll end up moving each triangle individually.
With that in mind, I would suggest you implement the triangulation in your game itself instead.
I'm pretty sure Nexus's Thor library can do that, you may want to take a look at that:
http://en.sfml-dev.org/forums/index.php?topic=7329.0 (http://en.sfml-dev.org/forums/index.php?topic=7329.0)

On the other hand, if I do find a good way to implement triangulation (probably by introducing a grouping feature or so), I will definitely implement it.  ;)
Title: Re: SolidShaper + Box2D + SFML
Post by: Astrof on April 05, 2012, 07:40:50 am
First: When I'm drawing a shape, what happens when things overlap?

The last shape drawn or selected will be at the top, use right click to select if you want to select anything below that, this will first select the topmost, then the one below that, and so on.

Ah sorry I wasn't specific; I meant while drawing using triangle strip and parts of it overlap how does that draw a shape? 

That's called triangulation.
It's possible to break polygons into triangles with triangulation, but not into triangle strip.
Since SolidShaper does not have any object-like feature to group shapes together, if I included a triangulation feature, it will break it down to triangles, but then you cannot move them as a set anymore, you'll end up moving each triangle individually.
With that in mind, I would suggest you implement the triangulation in your game itself instead.
I'm pretty sure Nexus's Thor library can do that, you may want to take a look at that:
http://en.sfml-dev.org/forums/index.php?topic=7329.0 (http://en.sfml-dev.org/forums/index.php?topic=7329.0)

On the other hand, if I do find a good way to implement triangulation (probably by introducing a grouping feature or so), I will definitely implement it.  ;)

I'm actually planning on using Thor for particles, etc. but haven't thought about it for triangulation.  I was more looking for an easy fix for my shape drawing/collision problem (most games I try to do die when I get to the physics part or messing with box2d). 


And I'm a bit confused about the triangle strip/triangulation thing.  When drawing with triangle strip doesn't it break everything into triangles anyway (or that's what it looks like)? Aren't triangle strips just connected triangles? I can eventually outline my sprite using the triangle strip if I do it just right but it takes a while.  Maybe it's just wishful thinking but I think there should be a way to split it up into a strip since everything still does share vertices.  Or maybe have everything share a body and have the shapes grouped?
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on April 05, 2012, 08:03:45 am
Ah sorry I wasn't specific; I meant while drawing using triangle strip and parts of it overlap how does that draw a shape? 

Triangle strips are pretty much just, well, triangles, so if there's parts overlapped, it's actually just triangles overlapped, like how other shapes overlap, how it's handled will have to depend on your game.
In Box2D, if you set it as a static body, there should be no problems with overlapping shape (since static shapes won't collide with each other).

And I'm a bit confused about the triangle strip/triangulation thing.  When drawing with triangle strip doesn't it break everything into triangles anyway (or that's what it looks like)? Aren't triangle strips just connected triangles? I can eventually outline my sprite using the triangle strip if I do it just right but it takes a while.  Maybe it's just wishful thinking but I think there should be a way to split it up into a strip since everything still does share vertices.  Or maybe have everything share a body and have the shapes grouped?

Well, the difference between triangulation and triangle strips here is, in triangulation, you break them into many triangles, 3 triangles mean 3 shapes, however, triangle strip is considered a single shape, since it's basically just a list of points which are "converted" into triangles when being drawn (in SolidShaper).

After loading triangle strips, what you get is a list of points, then, you can decide to convert it into a list of triangles to be added to Box2D as shapes, or if you're just drawing it, you can just convert them in your draw functions.
However, if it's a triangulated polygon, it will be loaded as a list of triangles instead, the original information of it being from the same shape is lost the moment you triangulate it.

Also, once you triangulate a polygon, the triangles no longer share any vertices, since they are already separate shapes. Meaning that, triangulation is an irreversible process, even if I introduce a grouping system, and you can move the triangulated polygon, they will still not share any vertices.
Title: Re: SolidShaper + Box2D + SFML
Post by: Astrof on April 05, 2012, 11:27:25 pm
Well, the difference between triangulation and triangle strips here is, in triangulation, you break them into many triangles, 3 triangles mean 3 shapes, however, triangle strip is considered a single shape, since it's basically just a list of points which are "converted" into triangles when being drawn (in SolidShaper).

After loading triangle strips, what you get is a list of points, then, you can decide to convert it into a list of triangles to be added to Box2D as shapes, or if you're just drawing it, you can just convert them in your draw functions.
However, if it's a triangulated polygon, it will be loaded as a list of triangles instead, the original information of it being from the same shape is lost the moment you triangulate it.

Also, once you triangulate a polygon, the triangles no longer share any vertices, since they are already separate shapes. Meaning that, triangulation is an irreversible process, even if I introduce a grouping system, and you can move the triangulated polygon, they will still not share any vertices.

Quote from: box2d Tutorial
Triangle strips are useful for creating “concave” shapes for collision detection/physics engines that cannot detect intersection/collision of concave shapes.

So again (sorry for beating a dead horse) shouldn't triangle strips help to create concave shapes (in box2d)? Is there not a triangulation method to turn things into triangle strips? I don't want separate shapes connected to a body (wouldn't that be inefficient?). 

I found this link http://stackoverflow.com/questions/8980379/polygon-triangulation-into-triangle-strips-for-opengl-es (http://stackoverflow.com/questions/8980379/polygon-triangulation-into-triangle-strips-for-opengl-es)
it mentions splitting into triangles and feeding it into something back into triangle strips. 

Ideally I'd like to have one library/method to create and use shapes for collision and drawing. 
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on April 06, 2012, 04:24:58 am
Yes, triangle strips does help create concave shapes.
However, you have to realize that not all concave polygons can be drawn with triangle strips (without overlapping).

In the link you provided, it says:
Quote
this algorithm works ok but the problem is it returns simple triangles which you can't draw with GL_TRIANGLE_STRIP, you need to use GL_TRIANGLES which isn't very efficient on a large number of vertices.

You said you don't want separate shapes connected to a body, however, that is not possible with concave shapes.
Same goes to triangle strips, what triangle strip does is simply break a list of points down into a list of triangles, meaning that if it's 3 triangles, 3 triangles (meaning 3 polygon shapes) will be added to box2d's body.
It isn't inefficient, however, too much of that may of course, slow things down. However, you shouldn't let that bother you, since that's how things are done anyway.

So what I'm saying is, whether you use triangle strips in SolidShaper, or do triangulation in your game engine, the result is the same, you'll still end up having multiple shapes in one body (which is really totally fine).

Ideally I'd like to have one library/method to create and use shapes for collision and drawing. 

I will try to implement triangulation in SSFile, since it's still manageable if I do it there, but since I can't estimate when it will be done, and since you said you're already using Thor in your project, why don't you try (or temporarily use) the triangulation function there until I've implemented it in SSFile?
Title: Re: SolidShaper + Box2D + SFML
Post by: Astrof on April 06, 2012, 08:03:09 am
Thanks! If it's too much work/not useful you don't have to; I was just wondering if it existed/if it was possible.

If not a triangulation method a simple grouping method would work too (where one would outline a sprite using shapes and then just group them all together).  Thanks for all your work!
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on April 06, 2012, 08:29:17 am
Implementing grouping in SolidShaper will be much more complex than triangulation in SSFile, since it requires changing how shapes are managed in SolidShaper, as well as the file specification as well, so I'd like to avoid that if possible.

However, how SolidShaper is designed to work isn't by putting all the shapes used in your game in 1 file and have different objects use different shape.

You should use SolidShaper to create 1 file for 1 object (characters, etc). This way, you're already grouping shapes as a single object. You also don't have to reload all the shapes if you just want one object, and it's easier to manage as well.

You can, however, put all the static objects in 1 file, like the terrain and other fixed objects.

In other word, if you have a character sprite, just load it as a reference in SolidShaper, outline it with shapes, and save it as a file (i.e. sprite.ssf), then there you go, the collision map for that sprite is done.

You can also achieve concave polygons if you can snap the vertices to the grid, since the only difference between that and triangulation is that, by using polygons, you'll have to manually make sure the vertices are at the same position, so that there isn't any holes (sometimes this doesn't even matter), while with triangulation or triangle strips, this is done automatically.
Title: Re: SolidShaper + Box2D + SFML
Post by: N1ghtly on April 09, 2012, 03:08:21 pm
That's pretty damn amazing!
Good work, keep it up!
Title: Re: SolidShaper + Box2D + SFML
Post by: zorexx on April 09, 2012, 03:19:11 pm
That's pretty damn amazing!
Good work, keep it up!

Thanks for the feedback!