b2Fixture.cpp
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty.  In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Dynamics/b2Fixture.h>
  19. #include <Box2D/Dynamics/Contacts/b2Contact.h>
  20. #include <Box2D/Collision/Shapes/b2CircleShape.h>
  21. #include <Box2D/Collision/Shapes/b2PolygonShape.h>
  22. #include <Box2D/Collision/b2BroadPhase.h>
  23. #include <Box2D/Collision/b2Collision.h>
  24. #include <Box2D/Common/b2BlockAllocator.h>
  25. b2Fixture::b2Fixture()
  26. {
  27. m_userData = NULL;
  28. m_body = NULL;
  29. m_next = NULL;
  30. m_proxyId = b2BroadPhase::e_nullProxy;
  31. m_shape = NULL;
  32. m_density = 0.0f;
  33. }
  34. b2Fixture::~b2Fixture()
  35. {
  36. b2Assert(m_shape == NULL);
  37. b2Assert(m_proxyId == b2BroadPhase::e_nullProxy);
  38. }
  39. void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
  40. {
  41. m_userData = def->userData;
  42. m_friction = def->friction;
  43. m_restitution = def->restitution;
  44. m_body = body;
  45. m_next = NULL;
  46. m_filter = def->filter;
  47. m_isSensor = def->isSensor;
  48. m_shape = def->shape->Clone(allocator);
  49. m_density = def->density;
  50. }
  51. void b2Fixture::Destroy(b2BlockAllocator* allocator)
  52. {
  53. // The proxy must be destroyed before calling this.
  54. b2Assert(m_proxyId == b2BroadPhase::e_nullProxy);
  55. // Free the child shape.
  56. switch (m_shape->m_type)
  57. {
  58. case b2Shape::e_circle:
  59. {
  60. b2CircleShape* s = (b2CircleShape*)m_shape;
  61. s->~b2CircleShape();
  62. allocator->Free(s, sizeof(b2CircleShape));
  63. }
  64. break;
  65. case b2Shape::e_polygon:
  66. {
  67. b2PolygonShape* s = (b2PolygonShape*)m_shape;
  68. s->~b2PolygonShape();
  69. allocator->Free(s, sizeof(b2PolygonShape));
  70. }
  71. break;
  72. default:
  73. b2Assert(false);
  74. break;
  75. }
  76. m_shape = NULL;
  77. }
  78. void b2Fixture::CreateProxy(b2BroadPhase* broadPhase, const b2Transform& xf)
  79. {
  80. b2Assert(m_proxyId == b2BroadPhase::e_nullProxy);
  81. // Create proxy in the broad-phase.
  82. m_shape->ComputeAABB(&m_aabb, xf);
  83. m_proxyId = broadPhase->CreateProxy(m_aabb, this);
  84. }
  85. void b2Fixture::DestroyProxy(b2BroadPhase* broadPhase)
  86. {
  87. if (m_proxyId == b2BroadPhase::e_nullProxy)
  88. {
  89. return;
  90. }
  91. // Destroy proxy in the broad-phase.
  92. broadPhase->DestroyProxy(m_proxyId);
  93. m_proxyId = b2BroadPhase::e_nullProxy;
  94. }
  95. void b2Fixture::Synchronize(b2BroadPhase* broadPhase, const b2Transform& transform1, const b2Transform& transform2)
  96. {
  97. if (m_proxyId == b2BroadPhase::e_nullProxy)
  98. {
  99. return;
  100. }
  101. // Compute an AABB that covers the swept shape (may miss some rotation effect).
  102. b2AABB aabb1, aabb2;
  103. m_shape->ComputeAABB(&aabb1, transform1);
  104. m_shape->ComputeAABB(&aabb2, transform2);
  105. m_aabb.Combine(aabb1, aabb2);
  106. b2Vec2 displacement = transform2.position - transform1.position;
  107. broadPhase->MoveProxy(m_proxyId, m_aabb, displacement);
  108. }
  109. void b2Fixture::SetFilterData(const b2Filter& filter)
  110. {
  111. m_filter = filter;
  112. if (m_body == NULL)
  113. {
  114. return;
  115. }
  116. // Flag associated contacts for filtering.
  117. b2ContactEdge* edge = m_body->GetContactList();
  118. while (edge)
  119. {
  120. b2Contact* contact = edge->contact;
  121. b2Fixture* fixtureA = contact->GetFixtureA();
  122. b2Fixture* fixtureB = contact->GetFixtureB();
  123. if (fixtureA == this || fixtureB == this)
  124. {
  125. contact->FlagForFiltering();
  126. }
  127. edge = edge->next;
  128. }
  129. }
  130. void b2Fixture::SetSensor(bool sensor)
  131. {
  132. m_isSensor = sensor;
  133. }