CollisionShape2D.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Scene/Node.h"
  26. #include "../Scene/Scene.h"
  27. #include "../Atomic2D/CollisionShape2D.h"
  28. #include "../Atomic2D/PhysicsUtils2D.h"
  29. #include "../Atomic2D/RigidBody2D.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. extern const char* ATOMIC2D_CATEGORY;
  34. CollisionShape2D::CollisionShape2D(Context* context) :
  35. Component(context),
  36. fixture_(0),
  37. cachedWorldScale_(Vector3::ONE)
  38. {
  39. }
  40. CollisionShape2D::~CollisionShape2D()
  41. {
  42. if (rigidBody_)
  43. rigidBody_->RemoveCollisionShape2D(this);
  44. ReleaseFixture();
  45. }
  46. void CollisionShape2D::RegisterObject(Context* context)
  47. {
  48. ACCESSOR_ATTRIBUTE("Trigger", IsTrigger, SetTrigger, bool, false, AM_DEFAULT);
  49. ACCESSOR_ATTRIBUTE("Category Bits", GetCategoryBits, SetCategoryBits, int, 0, AM_DEFAULT);
  50. ACCESSOR_ATTRIBUTE("Mask Bits", GetMaskBits, SetMaskBits, int, 0, AM_DEFAULT);
  51. ACCESSOR_ATTRIBUTE("Group Index", GetGroupIndex, SetGroupIndex, int, 0, AM_DEFAULT);
  52. ACCESSOR_ATTRIBUTE("Density", GetDensity, SetDensity, float, 0.0f, AM_DEFAULT);
  53. ACCESSOR_ATTRIBUTE("Friction", GetFriction, SetFriction, float, 0.2f, AM_DEFAULT);
  54. ACCESSOR_ATTRIBUTE("Restitution", GetRestitution, SetRestitution, float, 0.0f, AM_DEFAULT);
  55. }
  56. void CollisionShape2D::OnSetEnabled()
  57. {
  58. if (IsEnabledEffective())
  59. {
  60. CreateFixture();
  61. if (rigidBody_)
  62. rigidBody_->AddCollisionShape2D(this);
  63. }
  64. else
  65. {
  66. if (rigidBody_)
  67. rigidBody_->RemoveCollisionShape2D(this);
  68. ReleaseFixture();
  69. }
  70. }
  71. void CollisionShape2D::SetTrigger(bool trigger)
  72. {
  73. if (fixtureDef_.isSensor == trigger)
  74. return;
  75. fixtureDef_.isSensor = trigger;
  76. if (fixture_)
  77. fixture_->SetSensor(trigger);
  78. MarkNetworkUpdate();
  79. }
  80. void CollisionShape2D::SetCategoryBits(int categoryBits)
  81. {
  82. if (fixtureDef_.filter.categoryBits == categoryBits)
  83. return;
  84. fixtureDef_.filter.categoryBits = (uint16)categoryBits;
  85. if (fixture_)
  86. fixture_->SetFilterData(fixtureDef_.filter);
  87. MarkNetworkUpdate();
  88. }
  89. void CollisionShape2D::SetMaskBits(int maskBits)
  90. {
  91. if (fixtureDef_.filter.maskBits == maskBits)
  92. return;
  93. fixtureDef_.filter.maskBits = (uint16)maskBits;
  94. if (fixture_)
  95. fixture_->SetFilterData(fixtureDef_.filter);
  96. MarkNetworkUpdate();
  97. }
  98. void CollisionShape2D::SetGroupIndex(int groupIndex)
  99. {
  100. if (fixtureDef_.filter.groupIndex == groupIndex)
  101. return;
  102. fixtureDef_.filter.groupIndex = (int16)groupIndex;
  103. if (fixture_)
  104. fixture_->SetFilterData(fixtureDef_.filter);
  105. MarkNetworkUpdate();
  106. }
  107. void CollisionShape2D::SetDensity(float density)
  108. {
  109. if (fixtureDef_.density == density)
  110. return;
  111. fixtureDef_.density = density;
  112. if (fixture_)
  113. {
  114. // This will not automatically adjust the mass of the body
  115. fixture_->SetDensity(density);
  116. if (rigidBody_->GetUseFixtureMass())
  117. rigidBody_->GetBody()->ResetMassData();
  118. }
  119. MarkNetworkUpdate();
  120. }
  121. void CollisionShape2D::SetFriction(float friction)
  122. {
  123. if (fixtureDef_.friction == friction)
  124. return;
  125. fixtureDef_.friction = friction;
  126. if (fixture_)
  127. {
  128. // This will not change the friction of existing contacts
  129. fixture_->SetFriction(friction);
  130. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  131. while (contractEdge)
  132. {
  133. b2Contact* contact = contractEdge->contact;
  134. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  135. contractEdge->contact->ResetFriction();
  136. contractEdge = contractEdge->next;
  137. }
  138. }
  139. MarkNetworkUpdate();
  140. }
  141. void CollisionShape2D::SetRestitution(float restitution)
  142. {
  143. if (fixtureDef_.restitution == restitution)
  144. return;
  145. fixtureDef_.restitution = restitution;
  146. if (fixture_)
  147. {
  148. // This will not change the restitution of existing contacts
  149. fixture_->SetRestitution(restitution);
  150. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  151. while (contractEdge)
  152. {
  153. b2Contact* contact = contractEdge->contact;
  154. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  155. contractEdge->contact->ResetRestitution();
  156. contractEdge = contractEdge->next;
  157. }
  158. }
  159. MarkNetworkUpdate();
  160. }
  161. void CollisionShape2D::CreateFixture()
  162. {
  163. if (fixture_)
  164. return;
  165. if (!fixtureDef_.shape)
  166. return;
  167. if (!rigidBody_)
  168. return;
  169. b2Body* body = rigidBody_->GetBody();
  170. if (!body)
  171. return;
  172. fixture_ = body->CreateFixture(&fixtureDef_);
  173. fixture_->SetUserData(this);
  174. }
  175. void CollisionShape2D::ReleaseFixture()
  176. {
  177. if (!fixture_)
  178. return;
  179. if (!rigidBody_)
  180. return;
  181. b2Body* body = rigidBody_->GetBody();
  182. if (!body)
  183. return;
  184. body->DestroyFixture(fixture_);
  185. fixture_ = 0;
  186. }
  187. float CollisionShape2D::GetMass() const
  188. {
  189. if (!fixture_)
  190. return 0.0f;
  191. b2MassData massData;
  192. fixture_->GetMassData(&massData);
  193. return massData.mass;
  194. }
  195. float CollisionShape2D::GetInertia() const
  196. {
  197. if (!fixture_)
  198. return 0.0f;
  199. b2MassData massData;
  200. fixture_->GetMassData(&massData);
  201. return massData.I;
  202. }
  203. Vector2 CollisionShape2D::GetMassCenter() const
  204. {
  205. if (!fixture_)
  206. return Vector2::ZERO;
  207. b2MassData massData;
  208. fixture_->GetMassData(&massData);
  209. return ToVector2(massData.center);
  210. }
  211. void CollisionShape2D::OnNodeSet(Node* node)
  212. {
  213. Component::OnNodeSet(node);
  214. if (node)
  215. {
  216. node->AddListener(this);
  217. rigidBody_ = node->GetComponent<RigidBody2D>();
  218. if (rigidBody_)
  219. {
  220. CreateFixture();
  221. rigidBody_->AddCollisionShape2D(this);
  222. }
  223. else
  224. LOGERROR("No rigid body component in node, can not create collision shape");
  225. }
  226. }
  227. void CollisionShape2D::OnMarkedDirty(Node* node)
  228. {
  229. Vector3 newWorldScale = node_->GetWorldScale();
  230. Vector3 delta = newWorldScale - cachedWorldScale_;
  231. if (delta.DotProduct(delta) < 0.01f)
  232. return;
  233. // Physics operations are not safe from worker threads
  234. Scene* scene = GetScene();
  235. if (scene && scene->IsThreadedUpdate())
  236. {
  237. scene->DelayedMarkedDirty(this);
  238. return;
  239. }
  240. cachedWorldScale_ = newWorldScale;
  241. if (fixture_)
  242. ApplyNodeWorldScale();
  243. }
  244. }