CollisionShape2D.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // Copyright (c) 2008-2017 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. 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. ATOMIC_ACCESSOR_ATTRIBUTE("Trigger", IsTrigger, SetTrigger, bool, false, AM_DEFAULT);
  48. ATOMIC_ACCESSOR_ATTRIBUTE("Category Bits", GetCategoryBits, SetCategoryBits, int, 0, AM_DEFAULT);
  49. ATOMIC_ACCESSOR_ATTRIBUTE("Mask Bits", GetMaskBits, SetMaskBits, int, 0, AM_DEFAULT);
  50. ATOMIC_ACCESSOR_ATTRIBUTE("Group Index", GetGroupIndex, SetGroupIndex, int, 0, AM_DEFAULT);
  51. ATOMIC_ACCESSOR_ATTRIBUTE("Density", GetDensity, SetDensity, float, 0.0f, AM_DEFAULT);
  52. ATOMIC_ACCESSOR_ATTRIBUTE("Friction", GetFriction, SetFriction, float, 0.2f, AM_DEFAULT);
  53. ATOMIC_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. b2MassData massData;
  179. body->GetMassData(&massData);
  180. fixture_ = body->CreateFixture(&fixtureDef_);
  181. if (!rigidBody_->GetUseFixtureMass()) // Workaround for resetting mass in CreateFixture().
  182. body->SetMassData(&massData);
  183. fixture_->SetUserData(this);
  184. }
  185. }
  186. void CollisionShape2D::ReleaseFixture()
  187. {
  188. if (!fixture_)
  189. return;
  190. if (!rigidBody_)
  191. return;
  192. b2Body* body = rigidBody_->GetBody();
  193. if (!body)
  194. return;
  195. b2MassData massData;
  196. body->GetMassData(&massData);
  197. body->DestroyFixture(fixture_);
  198. if (!rigidBody_->GetUseFixtureMass()) // Workaround for resetting mass in DestroyFixture().
  199. body->SetMassData(&massData);
  200. fixture_ = 0;
  201. }
  202. float CollisionShape2D::GetMass() const
  203. {
  204. if (!fixture_)
  205. return 0.0f;
  206. b2MassData massData;
  207. fixture_->GetMassData(&massData);
  208. return massData.mass;
  209. }
  210. float CollisionShape2D::GetInertia() const
  211. {
  212. if (!fixture_)
  213. return 0.0f;
  214. b2MassData massData;
  215. fixture_->GetMassData(&massData);
  216. return massData.I;
  217. }
  218. Vector2 CollisionShape2D::GetMassCenter() const
  219. {
  220. if (!fixture_)
  221. return Vector2::ZERO;
  222. b2MassData massData;
  223. fixture_->GetMassData(&massData);
  224. return ToVector2(massData.center);
  225. }
  226. void CollisionShape2D::OnNodeSet(Node* node)
  227. {
  228. Component::OnNodeSet(node);
  229. if (node)
  230. {
  231. node->AddListener(this);
  232. rigidBody_ = node->GetComponent<RigidBody2D>();
  233. if (rigidBody_)
  234. {
  235. CreateFixture();
  236. rigidBody_->AddCollisionShape2D(this);
  237. }
  238. }
  239. }
  240. void CollisionShape2D::OnMarkedDirty(Node* node)
  241. {
  242. // Use signed world scale to allow flipping of sprites by negative scale to work properly in regard to the collision shape
  243. Vector3 newWorldScale = node_->GetSignedWorldScale();
  244. Vector3 delta = newWorldScale - cachedWorldScale_;
  245. if (delta.DotProduct(delta) < 0.01f)
  246. return;
  247. // Physics operations are not safe from worker threads
  248. Scene* scene = GetScene();
  249. if (scene && scene->IsThreadedUpdate())
  250. {
  251. scene->DelayedMarkedDirty(this);
  252. return;
  253. }
  254. cachedWorldScale_ = newWorldScale;
  255. if (fixture_)
  256. ApplyNodeWorldScale();
  257. }
  258. }