/* * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ /* Testbed example showing deformable and breakable bodies using the soft * b2DistanceJoint and a small,liteweight triangle mesher. * 2008-05-09 / nimodo */ #ifndef BREAKABLE_BODY_H #define BREAKABLE_BODY_H #include "TriangleMesh.h" /// utility macro #define H(x) (x)/2.0f #define N_MAXVERTEX 256 class BreakableBody : public Test { public: BreakableBody() { /// geometries float32 gx = 100.0f, gy = 1.0f, dx = 34.0f, br = 0.3f; float32 sx=-dx-H(dx), sy = 30.f; /// break joint, if the reactionforce exceeds: maxAllowableForce = 100.0f; m_drawMode = m_staticBodies = false; m_drawCount = 0; /// ground { b2PolygonDef sd; b2BodyDef bd; b2Body* ground; bd.position.Set(0.0f, 0.0f); ground = m_world->CreateBody(&bd); /// bottom sd.SetAsBox( H(gx), H(gy) ); ground->CreateFixture(&sd); sd.SetAsBox( H(dx), H(gy), b2Vec2(-dx,sy-1.0f), 0.0f ); ground->CreateFixture(&sd); } /// dyn bodies { b2PolygonDef pd; b2DistanceJointDef dj; dj.dampingRatio = 0.0f; dj.collideConnected = true; ExampleData('B'); dj.frequencyHz = 20.f; pd.density = 1.0f/70.0f; pd.friction = 0.4f; pd.restitution = 0.01f; CreateSoftBody( b2Vec2(sx,sy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; ExampleData('@'); dj.frequencyHz = 20.f; pd.density = 1.0f/36.0f; pd.friction = 0.1f; pd.restitution = 0.5f; CreateSoftBody( b2Vec2(sx+6.f,sy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; ExampleData('x'); dj.frequencyHz = 20.0f; pd.density = 1.0f/60.0f; pd.friction = 0.6f; pd.restitution = 0.0f; CreateSoftBody( b2Vec2(sx+13.f,sy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; ExampleData('2'); pd.density = 0.01f; pd.friction = 0.3f; pd.restitution = 0.3f; CreateSoftBody( b2Vec2(sx+20.f,sy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; ExampleData('D'); CreateSoftBody( b2Vec2(sx+28.f,sy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; ExampleData('b'); dj.frequencyHz = 10.0f; dj.dampingRatio = 20.0f; pd.friction = 0.9f; pd.restitution = 0.01f; pd.density = 0.01f; CreateSoftBody( b2Vec2(-5.f,5.f*gy), 0, 0, pd, dj, nodes,n_nodes, segments,n_segments, holes,n_holes) ; b2CircleDef cd; b2BodyDef bd; b2Body* b; cd.radius = br; cd.density= 0.001f; bd.position.Set(0.0f,10.0f*gy); for (int32 i=0; i<60; i++ ) { b = m_world->CreateBody(&bd); b->CreateFixture (&cd); b->SetMassFromShapes(); } } } /// Create compound (soft) body using a triangle mesh /// If meshDensity is 0, a minimal grid is generated. /// Actually pd and dj define the behaviour for all triangles void CreateSoftBody(b2Vec2 pos, int32 meshDensity,int32 options, b2PolygonDef pd, b2DistanceJointDef dj, tmVertex* nodes,int32 n_nodes, tmSegmentId *segments=NULL, int32 n_segments=0, tmVertex* holes=NULL, int32 n_holes=0) { int32 i; /// TriangleMesh defs tmTriangle *triangles; TriangleMesh md; /// box2d defs b2BodyDef bd; b2Body *b; /// in case of meshDensit>3 ... md.SetMaxVertexCount(meshDensity); if (options>0) md.SetOptions(options); /// triangulator main md.Mesh( nodes, n_nodes, segments,n_segments, holes, n_holes ); md.PrintData(); /// bodies (triangles) triangles = md.GetTriangles(); if ( triangles==NULL ) return; pd.vertexCount = 3; for ( i=0; i b2PolygonDef pd.vertices[0].Set(triangles[i].v[0]->x, triangles[i].v[0]->y); pd.vertices[1].Set(triangles[i].v[1]->x, triangles[i].v[1]->y); pd.vertices[2].Set(triangles[i].v[2]->x, triangles[i].v[2]->y); bd.position.Set(pos.x,pos.y); b = m_world->CreateBody(&bd); b->CreateFixture(&pd); b->SetMassFromShapes(); /// we need the body pointer in the triangles for the joints later triangles[i].userData = (void *)b; } } /// joints /// for each triangle-pair in edges, connect with a distance joint tmEdge *edges; tmTriangle *t0,*t1; b2Body *b1,*b2; edges = md.GetEdges(); for ( i=0; iinside==false) || (t1->inside==false) ) continue; /// Get bodies b1 = (b2Body*)t0->userData; b2 = (b2Body*)t1->userData; if ( b1==NULL || b2==NULL ) continue; dj.Initialize( b1,b2, b1->GetWorldCenter(), b2->GetWorldCenter()); m_world->CreateJoint(&dj); } /// clean TriangleMesh md.FreeMemory(); } /// maybe here to check for maximal reaction forces to break a body void Step(Settings* settings) { b2Joint *jStressed=NULL; float32 F=0.0f, tmp; Test::Step(settings); for (b2Joint* j = m_world->GetJointList(); j; j = j->GetNext()) { tmp = j->GetReactionForce(settings->hz).Length(); if ( tmp>F ) { F = tmp; jStressed = j; } } if ( jStressed && (F>maxAllowableForce) ) { m_world->DestroyJoint(jStressed); } m_debugDraw.DrawString(1, m_textLine,"max.reactionforce=%.0f allowable=%.0f change:-+", (float)F,(float)maxAllowableForce); m_textLine += 12; m_debugDraw.DrawString(1, m_textLine,"drawmode(%s):d mesh:m static(%s):s", (m_drawMode)?"on":"off", (m_staticBodies)?"on":"off"); m_textLine += 12; for ( int32 i=0; i0 ) { b2PolygonDef pd; b2DistanceJointDef dj; dj.collideConnected = true; dj.frequencyHz = 20.f; dj.dampingRatio = 10.0f; pd.density = (m_staticBodies) ? 0.0f : 1.0f/32.0f; pd.friction = 0.99f; pd.restitution = 0.01f; CreateSoftBody( b2Vec2(0.0f,0.0f), 0, tmO_SEGMENTBOUNDARY|tmO_GRADING, pd, dj, m_drawVertices, m_drawCount) ; m_drawCount = 0; m_drawMode = false; } break; } } void MouseDown(const b2Vec2& p) { if ( m_drawMode && (m_drawCount