CollisionShape2D.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // Copyright (c) 2008-2016 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 "../Urho2D/CollisionShape2D.h"
  28. #include "../Urho2D/PhysicsUtils2D.h"
  29. #include "../Urho2D/RigidBody2D.h"
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. CollisionShape2D::CollisionShape2D(Context* context) :
  34. Component(context),
  35. fixture_(0),
  36. cachedWorldScale_(Vector3::ONE)
  37. {
  38. }
  39. CollisionShape2D::~CollisionShape2D()
  40. {
  41. if (rigidBody_)
  42. rigidBody_->RemoveCollisionShape2D(this);
  43. ReleaseFixture();
  44. }
  45. void CollisionShape2D::RegisterObject(Context* context)
  46. {
  47. URHO3D_ACCESSOR_ATTRIBUTE("Trigger", IsTrigger, SetTrigger, bool, false, AM_DEFAULT);
  48. URHO3D_ACCESSOR_ATTRIBUTE("Category Bits", GetCategoryBits, SetCategoryBits, int, 0, AM_DEFAULT);
  49. URHO3D_ACCESSOR_ATTRIBUTE("Mask Bits", GetMaskBits, SetMaskBits, int, 0, AM_DEFAULT);
  50. URHO3D_ACCESSOR_ATTRIBUTE("Group Index", GetGroupIndex, SetGroupIndex, int, 0, AM_DEFAULT);
  51. URHO3D_ACCESSOR_ATTRIBUTE("Density", GetDensity, SetDensity, float, 0.0f, AM_DEFAULT);
  52. URHO3D_ACCESSOR_ATTRIBUTE("Friction", GetFriction, SetFriction, float, 0.2f, AM_DEFAULT);
  53. URHO3D_ACCESSOR_ATTRIBUTE("Restitution", GetRestitution, SetRestitution, float, 0.0f, AM_DEFAULT);
  54. }
  55. void CollisionShape2D::OnSetEnabled()
  56. {
  57. if (IsEnabledEffective())
  58. {
  59. CreateFixture();
  60. if (rigidBody_)
  61. rigidBody_->AddCollisionShape2D(this);
  62. }
  63. else
  64. {
  65. if (rigidBody_)
  66. rigidBody_->RemoveCollisionShape2D(this);
  67. ReleaseFixture();
  68. }
  69. }
  70. void CollisionShape2D::SetTrigger(bool trigger)
  71. {
  72. if (fixtureDef_.isSensor == trigger)
  73. return;
  74. fixtureDef_.isSensor = trigger;
  75. if (fixture_)
  76. fixture_->SetSensor(trigger);
  77. MarkNetworkUpdate();
  78. }
  79. void CollisionShape2D::SetCategoryBits(int categoryBits)
  80. {
  81. if (fixtureDef_.filter.categoryBits == categoryBits)
  82. return;
  83. fixtureDef_.filter.categoryBits = (uint16)categoryBits;
  84. if (fixture_)
  85. fixture_->SetFilterData(fixtureDef_.filter);
  86. MarkNetworkUpdate();
  87. }
  88. void CollisionShape2D::SetMaskBits(int maskBits)
  89. {
  90. if (fixtureDef_.filter.maskBits == maskBits)
  91. return;
  92. fixtureDef_.filter.maskBits = (uint16)maskBits;
  93. if (fixture_)
  94. fixture_->SetFilterData(fixtureDef_.filter);
  95. MarkNetworkUpdate();
  96. }
  97. void CollisionShape2D::SetGroupIndex(int groupIndex)
  98. {
  99. if (fixtureDef_.filter.groupIndex == groupIndex)
  100. return;
  101. fixtureDef_.filter.groupIndex = (int16)groupIndex;
  102. if (fixture_)
  103. fixture_->SetFilterData(fixtureDef_.filter);
  104. MarkNetworkUpdate();
  105. }
  106. void CollisionShape2D::SetDensity(float density)
  107. {
  108. if (fixtureDef_.density == density)
  109. return;
  110. fixtureDef_.density = density;
  111. if (fixture_)
  112. {
  113. // This will not automatically adjust the mass of the body
  114. fixture_->SetDensity(density);
  115. if (rigidBody_->GetUseFixtureMass())
  116. rigidBody_->GetBody()->ResetMassData();
  117. }
  118. MarkNetworkUpdate();
  119. }
  120. void CollisionShape2D::SetFriction(float friction)
  121. {
  122. if (fixtureDef_.friction == friction)
  123. return;
  124. fixtureDef_.friction = friction;
  125. if (fixture_)
  126. {
  127. // This will not change the friction of existing contacts
  128. fixture_->SetFriction(friction);
  129. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  130. while (contractEdge)
  131. {
  132. b2Contact* contact = contractEdge->contact;
  133. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  134. contractEdge->contact->ResetFriction();
  135. contractEdge = contractEdge->next;
  136. }
  137. }
  138. MarkNetworkUpdate();
  139. }
  140. void CollisionShape2D::SetRestitution(float restitution)
  141. {
  142. if (fixtureDef_.restitution == restitution)
  143. return;
  144. fixtureDef_.restitution = restitution;
  145. if (fixture_)
  146. {
  147. // This will not change the restitution of existing contacts
  148. fixture_->SetRestitution(restitution);
  149. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  150. while (contractEdge)
  151. {
  152. b2Contact* contact = contractEdge->contact;
  153. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  154. contractEdge->contact->ResetRestitution();
  155. contractEdge = contractEdge->next;
  156. }
  157. }
  158. MarkNetworkUpdate();
  159. }
  160. void CollisionShape2D::CreateFixture()
  161. {
  162. if (fixture_)
  163. return;
  164. if (!fixtureDef_.shape)
  165. return;
  166. if (!rigidBody_)
  167. {
  168. rigidBody_ = node_->GetComponent<RigidBody2D>(); // RigidBody2D can be created after CollisionShape2D
  169. if (!rigidBody_)
  170. return;
  171. }
  172. b2Body* body = rigidBody_->GetBody();
  173. if (!body)
  174. return;
  175. // Chain shape must have atleast two vertices before creating fixture
  176. if (fixtureDef_.shape->m_type != b2Shape::e_chain || static_cast<const b2ChainShape*>(fixtureDef_.shape)->m_count >= 2)
  177. {
  178. fixture_ = body->CreateFixture(&fixtureDef_);
  179. fixture_->SetUserData(this);
  180. }
  181. }
  182. void CollisionShape2D::ReleaseFixture()
  183. {
  184. if (!fixture_)
  185. return;
  186. if (!rigidBody_)
  187. return;
  188. b2Body* body = rigidBody_->GetBody();
  189. if (!body)
  190. return;
  191. body->DestroyFixture(fixture_);
  192. fixture_ = 0;
  193. }
  194. float CollisionShape2D::GetMass() const
  195. {
  196. if (!fixture_)
  197. return 0.0f;
  198. b2MassData massData;
  199. fixture_->GetMassData(&massData);
  200. return massData.mass;
  201. }
  202. float CollisionShape2D::GetInertia() const
  203. {
  204. if (!fixture_)
  205. return 0.0f;
  206. b2MassData massData;
  207. fixture_->GetMassData(&massData);
  208. return massData.I;
  209. }
  210. Vector2 CollisionShape2D::GetMassCenter() const
  211. {
  212. if (!fixture_)
  213. return Vector2::ZERO;
  214. b2MassData massData;
  215. fixture_->GetMassData(&massData);
  216. return ToVector2(massData.center);
  217. }
  218. void CollisionShape2D::OnNodeSet(Node* node)
  219. {
  220. Component::OnNodeSet(node);
  221. if (node)
  222. {
  223. node->AddListener(this);
  224. rigidBody_ = node->GetComponent<RigidBody2D>();
  225. if (rigidBody_)
  226. {
  227. CreateFixture();
  228. rigidBody_->AddCollisionShape2D(this);
  229. }
  230. }
  231. }
  232. void CollisionShape2D::OnMarkedDirty(Node* node)
  233. {
  234. Vector3 newWorldScale = node_->GetWorldScale();
  235. Vector3 delta = newWorldScale - cachedWorldScale_;
  236. if (delta.DotProduct(delta) < 0.01f)
  237. return;
  238. // Physics operations are not safe from worker threads
  239. Scene* scene = GetScene();
  240. if (scene && scene->IsThreadedUpdate())
  241. {
  242. scene->DelayedMarkedDirty(this);
  243. return;
  244. }
  245. cachedWorldScale_ = newWorldScale;
  246. if (fixture_)
  247. ApplyNodeWorldScale();
  248. }
  249. }