Author Message

<  extend  ~  Complex polygons with Box2D

PostPosted: Thu Feb 25, 2010 11:10 am
Joined: Tue Feb 02, 2010 4:11 pmPosts: 4
Hi all!

I'm trying to fix different shapes(triangle) in the same body so to create a complex polygon. Does anybody know how to do it?

Thanks.


Offline Profile
PostPosted: Tue Mar 02, 2010 5:54 am
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
Sounds like you need to work out the delaunay triangulation within the complex polygon.

Vanderlin has done some work with this,

http://toddvanderlin.com/2009/04/delaunay-contour/

L.



_________________
http://julapy.com/blog
Offline Profile
PostPosted: Tue Mar 02, 2010 10:41 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
Hi!

Here is a small addon (with example) that wraps triangle c++ wrapper from Piyush Kumar for openframeworks according to the method described by Todd in his blog. It also uses the great PointInsidePolygon function of Theo! You can use these triangles to reproduce your shape in box2d.

ofxTriangle

hope it helps!

update: memory leak fixed! ^^

Image


Last edited by kikko on Fri Mar 05, 2010 10:42 pm, edited 3 times in total.


_________________
http://www.kikko.fr
Offline Profile
PostPosted: Wed Mar 03, 2010 1:30 pm
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
very cool kikko, thanks for sharing that.

can you think of any way of stabilising the triangles?
looks like on every frame it works out a new contour and re-calculates all the triangles.

just trying to think of ways of smoothing out the triangle transitions...

L.



_________________
http://julapy.com/blog
Offline Profile
PostPosted: Wed Mar 03, 2010 3:11 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
I think you find the triangulation messy because the debug drawing gives a random color for each triangle on every update. It looks way more stable when you only draw the strokes of the triangles!

I'm not sure about the best way to interpolate the triangles instead of replacing on each update. But if your goal is to use the triangles as rigid bodies for human interactions, it's likely that you want them to be accurate as often as possible!



_________________
http://www.kikko.fr
Offline Profile
PostPosted: Thu Mar 04, 2010 12:26 pm
Joined: Tue Feb 02, 2010 4:11 pmPosts: 4
That's good stuff!

Thank you both!


Offline Profile
PostPosted: Fri Mar 05, 2010 12:04 pm
Joined: Tue Feb 02, 2010 4:11 pmPosts: 4
There's still a problem when I want to assemble all these triangles in the same body. Most of the time the program fail.

Here the code, maybe you can see what could be the problem :?

Code:
void init(b2World * b2dworld, float x, float y, float fMass, float fFriction, vector<ofPoint> pTriangleA, vector<ofPoint>  pTriangleB, vector<ofPoint>  pTriangleC)
{
   if(b2dworld == NULL) {
      ofLog(OF_LOG_NOTICE, "- must have a valid world -");
      return;
   }

   world = b2dworld;

   b2BodyDef bodyDef;
   bodyDef.position.Set(x/OFX_BOX2D_SCALE, y/OFX_BOX2D_SCALE);
   body = world->CreateBody(&bodyDef);


        int iCptPoly=0;
   for(int i=0;i<pTriangleA.size();i++)
   {
            b2PolygonDef poly;
            poly.vertexCount = 3;

            poly.density        = fMass;
            poly.restitution     = 0.0;
            poly.friction        = fFriction;

            poly.vertices[0].Set(pTriangleA[i].x, pTriangleA[i].y);
            poly.vertices[1].Set(pTriangleB[i].x, pTriangleB[i].y);
            poly.vertices[2].Set(pTriangleC[i].x, pTriangleC[i].y);

            for(int j=0;j<3;j++)
            {
                poly.vertices[j].x /= OFX_BOX2D_SCALE;
                poly.vertices[j].y /= OFX_BOX2D_SCALE;
            }
         
            body->CreateShape(&poly);  ///--------->IT FAIL THERE<-------
   }

       body->SetMassFromShapes();

   //set the filter data
   b2FilterData data;
   data.categoryBits = 0x0003;
   data.maskBits = 0x1;
   data.groupIndex = -3;
}


Thanks


Offline Profile
PostPosted: Fri Mar 05, 2010 2:05 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
If it's a box2d assertion that make your app crash, it's probably because the points have to be ordered clockwise and the triangles have to fit some requirements :

Quote:
- Edges can't be too close to parallel
- Area must be greater than a minimum value
- Each edge must be more than b2_toiSlop away from the centroid


I haven't figured yet how to perform these checks but I'll give it a try in the next couple of days!



_________________
http://www.kikko.fr
Offline Profile
PostPosted: Fri Mar 05, 2010 10:39 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
well after doing some researches i found out 2 things :

- first is that plong0 made a similar addon (ofxDelaunay) a couple of months ago that may be faster than the one i posted!
- second is that Todd Vanderlin already did all the maths of the polygon requierements in the ofxBox2dPolygon.validateShape() method! so for us, validating a triangle is as easy as :
Code:
ofxBox2dPolygon poly;
poly.addVertex(tData->a.x, tData->a.y);
poly.addVertex(tData->b.x, tData->b.y);
poly.addVertex(tData->c.x, tData->c.y);
         
if(poly.validateShape()) {
         
   poly.init(physics.world, 0.0f, 0.0f);
}

awesome! thanks for that Todd!^^

sources of the example below

Image



_________________
http://www.kikko.fr
Offline Profile
PostPosted: Sat Mar 06, 2010 1:04 am
User avatarJoined: Fri Feb 12, 2010 7:06 amPosts: 22Location: Tokyo Japan || HCMC Vietnam
Hi kikko and all.
Thank you all for sharing code and info :)

i did some experiments using ofxTriangle.



Attachment:
The attachment BlobToB2D.zip is no longer available

**edit 20100506**
this is depreciated.
new version is http://www.openframeworks.cc/forum/viewtopic.php?f=10&t=3386&p=20078#p20078


In this experiment, i did some awkward hack to stop box2d assertion.
At b2Settings.h, before #include <assert.h>, place #define NDEBUG.

And how to create custome polygon:
http://www.psyked.co.uk/box2d/simple-box2d-custom-polygon-creation.htm

Code:
void testApp::makeComplexBody(b2Body* body, vector<ofxTriangleData*> triangles) {
   int shapeCnt = 0;
   
   for (int i = 0; i < triangles.size(); i++) {
   
      b2PolygonDef* shapeDef = new b2PolygonDef();
      shapeDef->density = 1;
      shapeDef->friction = 5;
      shapeDef->vertexCount = 3;

      shapeDef->vertices[0].Set(triangles[i]->a.x/OFX_BOX2D_SCALE, triangles[i]->a.y/OFX_BOX2D_SCALE);
      shapeDef->vertices[1].Set(triangles[i]->b.x/OFX_BOX2D_SCALE, triangles[i]->b.y/OFX_BOX2D_SCALE);
      shapeDef->vertices[2].Set(triangles[i]->c.x/OFX_BOX2D_SCALE, triangles[i]->c.y/OFX_BOX2D_SCALE);
      
      body->CreateShape(shapeDef);
      shapeCnt++;
   }
   
   shapeCnts.push_back(shapeCnt);
   body->SetMassFromShapes();
}


cheers
Attachments:
BlobToB2D.zip [29.7 KiB]
Downloaded 115 times



Last edited by Akira_At_Asia on Thu May 06, 2010 12:28 am, edited 2 times in total.

_________________
http://www.ampontang.com/
Offline Profile
PostPosted: Sat Mar 06, 2010 8:13 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
sweet idea and video Akira! love it!



_________________
http://www.kikko.fr
Offline Profile
PostPosted: Tue Mar 09, 2010 9:59 am
Joined: Tue Feb 02, 2010 4:11 pmPosts: 4
Nice sharing!

Thanks you both!


Offline Profile
PostPosted: Tue Mar 09, 2010 11:15 am
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
hi kikko,

im trying to run your example but running into problems, think its do with my version of ofxBox2d.

i got the latest from vanderlin's trunk (rev 73) http://code.google.com/p/vanderlin/ and ofxBox2dPolygon is missing the validateShape() method you call in your code.
not sure why it has been removed.

so i went back a revision to rev 72 and i was able to compile your code but running into this error,

Quote:
Assertion failed: (m_I > 0.0f), function SetMassFromShapes, file /Volumes/STORAGE/OF_0061/apps/forum/3386_ofxTriangle_box2d/../../../addons/ofxBox2d_rev72/scr/lib/Box2D/Source/Dynamics/b2Body.cpp, line 334.
Program received signal: “SIGABRT”.


would you mind posting your version of ofxBox2d you are using for your example?

really keen to get it working.

thx, L.



_________________
http://julapy.com/blog
Offline Profile
PostPosted: Tue Mar 09, 2010 8:35 pm
User avatarJoined: Wed Sep 10, 2008 9:06 pmPosts: 22Location: Paris - France
hi julapy,

my knowledge in box2d is quite basic so I can't tell you why your shape is unable to get a mass even if it has been checked by Todd's function..

here is the version I'm working with hope it helps!
Attachments:
ofxBox2d.zip [195.48 KiB]
Downloaded 99 times


_________________
http://www.kikko.fr
Offline Profile
PostPosted: Wed Mar 10, 2010 1:37 am
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
hey kikko, works great now!
thx for posting.



_________________
http://julapy.com/blog
Offline Profile

Display posts from previous:  Sort by:

All times are UTC
Page 1 of 3
34 posts
Go to page 1, 2, 3  Next
Users browsing this forum: No registered users and 3 guests
Search for:
Post new topic  Reply to topic
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
cron