Warning, file /education/gcompris/external/qml-box2d/Box2D/Dynamics/b2Fixture.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #include <Box2D/Dynamics/b2Fixture.h>
0020 #include <Box2D/Dynamics/Contacts/b2Contact.h>
0021 #include <Box2D/Dynamics/b2World.h>
0022 #include <Box2D/Collision/Shapes/b2CircleShape.h>
0023 #include <Box2D/Collision/Shapes/b2EdgeShape.h>
0024 #include <Box2D/Collision/Shapes/b2PolygonShape.h>
0025 #include <Box2D/Collision/Shapes/b2ChainShape.h>
0026 #include <Box2D/Collision/b2BroadPhase.h>
0027 #include <Box2D/Collision/b2Collision.h>
0028 #include <Box2D/Common/b2BlockAllocator.h>
0029 
0030 b2Fixture::b2Fixture()
0031 {
0032     m_userData = NULL;
0033     m_body = NULL;
0034     m_next = NULL;
0035     m_proxies = NULL;
0036     m_proxyCount = 0;
0037     m_shape = NULL;
0038     m_density = 0.0f;
0039 }
0040 
0041 void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
0042 {
0043     m_userData = def->userData;
0044     m_friction = def->friction;
0045     m_restitution = def->restitution;
0046 
0047     m_body = body;
0048     m_next = NULL;
0049 
0050     m_filter = def->filter;
0051 
0052     m_isSensor = def->isSensor;
0053 
0054     m_shape = def->shape->Clone(allocator);
0055 
0056     // Reserve proxy space
0057     int32 childCount = m_shape->GetChildCount();
0058     m_proxies = (b2FixtureProxy*)allocator->Allocate(childCount * sizeof(b2FixtureProxy));
0059     for (int32 i = 0; i < childCount; ++i)
0060     {
0061         m_proxies[i].fixture = NULL;
0062         m_proxies[i].proxyId = b2BroadPhase::e_nullProxy;
0063     }
0064     m_proxyCount = 0;
0065 
0066     m_density = def->density;
0067 }
0068 
0069 void b2Fixture::Destroy(b2BlockAllocator* allocator)
0070 {
0071     // The proxies must be destroyed before calling this.
0072     b2Assert(m_proxyCount == 0);
0073 
0074     // Free the proxy array.
0075     int32 childCount = m_shape->GetChildCount();
0076     allocator->Free(m_proxies, childCount * sizeof(b2FixtureProxy));
0077     m_proxies = NULL;
0078 
0079     // Free the child shape.
0080     switch (m_shape->m_type)
0081     {
0082     case b2Shape::e_circle:
0083         {
0084             b2CircleShape* s = (b2CircleShape*)m_shape;
0085             s->~b2CircleShape();
0086             allocator->Free(s, sizeof(b2CircleShape));
0087         }
0088         break;
0089 
0090     case b2Shape::e_edge:
0091         {
0092             b2EdgeShape* s = (b2EdgeShape*)m_shape;
0093             s->~b2EdgeShape();
0094             allocator->Free(s, sizeof(b2EdgeShape));
0095         }
0096         break;
0097 
0098     case b2Shape::e_polygon:
0099         {
0100             b2PolygonShape* s = (b2PolygonShape*)m_shape;
0101             s->~b2PolygonShape();
0102             allocator->Free(s, sizeof(b2PolygonShape));
0103         }
0104         break;
0105 
0106     case b2Shape::e_chain:
0107         {
0108             b2ChainShape* s = (b2ChainShape*)m_shape;
0109             s->~b2ChainShape();
0110             allocator->Free(s, sizeof(b2ChainShape));
0111         }
0112         break;
0113 
0114     default:
0115         b2Assert(false);
0116         break;
0117     }
0118 
0119     m_shape = NULL;
0120 }
0121 
0122 void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf)
0123 {
0124     b2Assert(m_proxyCount == 0);
0125 
0126     // Create proxies in the broad-phase.
0127     m_proxyCount = m_shape->GetChildCount();
0128 
0129     for (int32 i = 0; i < m_proxyCount; ++i)
0130     {
0131         b2FixtureProxy* proxy = m_proxies + i;
0132         m_shape->ComputeAABB(&proxy->aabb, xf, i);
0133         proxy->proxyId = broadPhase->CreateProxy(proxy->aabb, proxy);
0134         proxy->fixture = this;
0135         proxy->childIndex = i;
0136     }
0137 }
0138 
0139 void b2Fixture::DestroyProxies(b2BroadPhase* broadPhase)
0140 {
0141     // Destroy proxies in the broad-phase.
0142     for (int32 i = 0; i < m_proxyCount; ++i)
0143     {
0144         b2FixtureProxy* proxy = m_proxies + i;
0145         broadPhase->DestroyProxy(proxy->proxyId);
0146         proxy->proxyId = b2BroadPhase::e_nullProxy;
0147     }
0148 
0149     m_proxyCount = 0;
0150 }
0151 
0152 void b2Fixture::Synchronize(b2BroadPhase* broadPhase, const b2Transform& transform1, const b2Transform& transform2)
0153 {
0154     if (m_proxyCount == 0)
0155     {   
0156         return;
0157     }
0158 
0159     for (int32 i = 0; i < m_proxyCount; ++i)
0160     {
0161         b2FixtureProxy* proxy = m_proxies + i;
0162 
0163         // Compute an AABB that covers the swept shape (may miss some rotation effect).
0164         b2AABB aabb1, aabb2;
0165         m_shape->ComputeAABB(&aabb1, transform1, proxy->childIndex);
0166         m_shape->ComputeAABB(&aabb2, transform2, proxy->childIndex);
0167     
0168         proxy->aabb.Combine(aabb1, aabb2);
0169 
0170         b2Vec2 displacement = transform2.p - transform1.p;
0171 
0172         broadPhase->MoveProxy(proxy->proxyId, proxy->aabb, displacement);
0173     }
0174 }
0175 
0176 void b2Fixture::SetFilterData(const b2Filter& filter)
0177 {
0178     m_filter = filter;
0179 
0180     Refilter();
0181 }
0182 
0183 void b2Fixture::Refilter()
0184 {
0185     if (m_body == NULL)
0186     {
0187         return;
0188     }
0189 
0190     // Flag associated contacts for filtering.
0191     b2ContactEdge* edge = m_body->GetContactList();
0192     while (edge)
0193     {
0194         b2Contact* contact = edge->contact;
0195         b2Fixture* fixtureA = contact->GetFixtureA();
0196         b2Fixture* fixtureB = contact->GetFixtureB();
0197         if (fixtureA == this || fixtureB == this)
0198         {
0199             contact->FlagForFiltering();
0200         }
0201 
0202         edge = edge->next;
0203     }
0204 
0205     b2World* world = m_body->GetWorld();
0206 
0207     if (world == NULL)
0208     {
0209         return;
0210     }
0211 
0212     // Touch each proxy so that new pairs may be created
0213     b2BroadPhase* broadPhase = &world->m_contactManager.m_broadPhase;
0214     for (int32 i = 0; i < m_proxyCount; ++i)
0215     {
0216         broadPhase->TouchProxy(m_proxies[i].proxyId);
0217     }
0218 }
0219 
0220 void b2Fixture::SetSensor(bool sensor)
0221 {
0222     if (sensor != m_isSensor)
0223     {
0224         m_body->SetAwake(true);
0225         m_isSensor = sensor;
0226     }
0227 }
0228 
0229 void b2Fixture::Dump(int32 bodyIndex)
0230 {
0231     b2Log("    b2FixtureDef fd;\n");
0232     b2Log("    fd.friction = %.15lef;\n", m_friction);
0233     b2Log("    fd.restitution = %.15lef;\n", m_restitution);
0234     b2Log("    fd.density = %.15lef;\n", m_density);
0235     b2Log("    fd.isSensor = bool(%d);\n", m_isSensor);
0236     b2Log("    fd.filter.categoryBits = uint16(%d);\n", m_filter.categoryBits);
0237     b2Log("    fd.filter.maskBits = uint16(%d);\n", m_filter.maskBits);
0238     b2Log("    fd.filter.groupIndex = int16(%d);\n", m_filter.groupIndex);
0239 
0240     switch (m_shape->m_type)
0241     {
0242     case b2Shape::e_circle:
0243         {
0244             b2CircleShape* s = (b2CircleShape*)m_shape;
0245             b2Log("    b2CircleShape shape;\n");
0246             b2Log("    shape.m_radius = %.15lef;\n", s->m_radius);
0247             b2Log("    shape.m_p.Set(%.15lef, %.15lef);\n", s->m_p.x, s->m_p.y);
0248         }
0249         break;
0250 
0251     case b2Shape::e_edge:
0252         {
0253             b2EdgeShape* s = (b2EdgeShape*)m_shape;
0254             b2Log("    b2EdgeShape shape;\n");
0255             b2Log("    shape.m_radius = %.15lef;\n", s->m_radius);
0256             b2Log("    shape.m_vertex0.Set(%.15lef, %.15lef);\n", s->m_vertex0.x, s->m_vertex0.y);
0257             b2Log("    shape.m_vertex1.Set(%.15lef, %.15lef);\n", s->m_vertex1.x, s->m_vertex1.y);
0258             b2Log("    shape.m_vertex2.Set(%.15lef, %.15lef);\n", s->m_vertex2.x, s->m_vertex2.y);
0259             b2Log("    shape.m_vertex3.Set(%.15lef, %.15lef);\n", s->m_vertex3.x, s->m_vertex3.y);
0260             b2Log("    shape.m_hasVertex0 = bool(%d);\n", s->m_hasVertex0);
0261             b2Log("    shape.m_hasVertex3 = bool(%d);\n", s->m_hasVertex3);
0262         }
0263         break;
0264 
0265     case b2Shape::e_polygon:
0266         {
0267             b2PolygonShape* s = (b2PolygonShape*)m_shape;
0268             b2Log("    b2PolygonShape shape;\n");
0269             b2Log("    b2Vec2 vs[%d];\n", b2_maxPolygonVertices);
0270             for (int32 i = 0; i < s->m_count; ++i)
0271             {
0272                 b2Log("    vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
0273             }
0274             b2Log("    shape.Set(vs, %d);\n", s->m_count);
0275         }
0276         break;
0277 
0278     case b2Shape::e_chain:
0279         {
0280             b2ChainShape* s = (b2ChainShape*)m_shape;
0281             b2Log("    b2ChainShape shape;\n");
0282             b2Log("    b2Vec2 vs[%d];\n", s->m_count);
0283             for (int32 i = 0; i < s->m_count; ++i)
0284             {
0285                 b2Log("    vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
0286             }
0287             b2Log("    shape.CreateChain(vs, %d);\n", s->m_count);
0288             b2Log("    shape.m_prevVertex.Set(%.15lef, %.15lef);\n", s->m_prevVertex.x, s->m_prevVertex.y);
0289             b2Log("    shape.m_nextVertex.Set(%.15lef, %.15lef);\n", s->m_nextVertex.x, s->m_nextVertex.y);
0290             b2Log("    shape.m_hasPrevVertex = bool(%d);\n", s->m_hasPrevVertex);
0291             b2Log("    shape.m_hasNextVertex = bool(%d);\n", s->m_hasNextVertex);
0292         }
0293         break;
0294 
0295     default:
0296         return;
0297     }
0298 
0299     b2Log("\n");
0300     b2Log("    fd.shape = &shape;\n");
0301     b2Log("\n");
0302     b2Log("    bodies[%d]->CreateFixture(&fd);\n", bodyIndex);
0303 }