CollisionShape2D.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // Copyright (c) 2008-2014 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 "CollisionShape2D.h"
  24. #include "Context.h"
  25. #include "Log.h"
  26. #include "Node.h"
  27. #include "PhysicsUtils2D.h"
  28. #include "RigidBody2D.h"
  29. #include "Scene.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. extern const char* URHO2D_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. COPY_BASE_ATTRIBUTES(Component);
  56. }
  57. void CollisionShape2D::OnSetEnabled()
  58. {
  59. if (IsEnabledEffective())
  60. {
  61. CreateFixture();
  62. if (rigidBody_)
  63. rigidBody_->AddCollisionShape2D(this);
  64. }
  65. else
  66. {
  67. if (rigidBody_)
  68. rigidBody_->RemoveCollisionShape2D(this);
  69. ReleaseFixture();
  70. }
  71. }
  72. void CollisionShape2D::SetTrigger(bool trigger)
  73. {
  74. if (fixtureDef_.isSensor == trigger)
  75. return;
  76. fixtureDef_.isSensor = trigger;
  77. if (fixture_)
  78. fixture_->SetSensor(trigger);
  79. MarkNetworkUpdate();
  80. }
  81. void CollisionShape2D::SetCategoryBits(int categoryBits)
  82. {
  83. if (fixtureDef_.filter.categoryBits == categoryBits)
  84. return;
  85. fixtureDef_.filter.categoryBits = categoryBits;
  86. if (fixture_)
  87. fixture_->SetFilterData(fixtureDef_.filter);
  88. MarkNetworkUpdate();
  89. }
  90. void CollisionShape2D::SetMaskBits(int maskBits)
  91. {
  92. if (fixtureDef_.filter.maskBits == maskBits)
  93. return;
  94. fixtureDef_.filter.maskBits = maskBits;
  95. if (fixture_)
  96. fixture_->SetFilterData(fixtureDef_.filter);
  97. MarkNetworkUpdate();
  98. }
  99. void CollisionShape2D::SetGroupIndex(int groupIndex)
  100. {
  101. if (fixtureDef_.filter.groupIndex == groupIndex)
  102. return;
  103. fixtureDef_.filter.groupIndex = groupIndex;
  104. if (fixture_)
  105. fixture_->SetFilterData(fixtureDef_.filter);
  106. MarkNetworkUpdate();
  107. }
  108. void CollisionShape2D::SetDensity(float density)
  109. {
  110. if (fixtureDef_.density == density)
  111. return;
  112. fixtureDef_.density = density;
  113. if (fixture_)
  114. {
  115. // This will not automatically adjust the mass of the body
  116. fixture_->SetDensity(density);
  117. if (rigidBody_->GetUseFixtureMass())
  118. rigidBody_->GetBody()->ResetMassData();
  119. }
  120. MarkNetworkUpdate();
  121. }
  122. void CollisionShape2D::SetFriction(float friction)
  123. {
  124. if (fixtureDef_.friction == friction)
  125. return;
  126. fixtureDef_.friction = friction;
  127. if (fixture_)
  128. {
  129. // This will not change the friction of existing contacts
  130. fixture_->SetFriction(friction);
  131. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  132. while (contractEdge)
  133. {
  134. b2Contact* contact = contractEdge->contact;
  135. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  136. contractEdge->contact->ResetFriction();
  137. contractEdge = contractEdge->next;
  138. }
  139. }
  140. MarkNetworkUpdate();
  141. }
  142. void CollisionShape2D::SetRestitution(float restitution)
  143. {
  144. if (fixtureDef_.restitution == restitution)
  145. return;
  146. fixtureDef_.restitution = restitution;
  147. if (fixture_)
  148. {
  149. // This will not change the restitution of existing contacts
  150. fixture_->SetRestitution(restitution);
  151. b2ContactEdge* contractEdge = rigidBody_->GetBody()->GetContactList();
  152. while (contractEdge)
  153. {
  154. b2Contact* contact = contractEdge->contact;
  155. if (contact->GetFixtureA() == fixture_ || contact->GetFixtureB() == fixture_)
  156. contractEdge->contact->ResetRestitution();
  157. contractEdge = contractEdge->next;
  158. }
  159. }
  160. MarkNetworkUpdate();
  161. }
  162. void CollisionShape2D::CreateFixture()
  163. {
  164. if (fixture_)
  165. return;
  166. if (!fixtureDef_.shape)
  167. return;
  168. if (!rigidBody_)
  169. return;
  170. b2Body* body = rigidBody_->GetBody();
  171. if (!body)
  172. return;
  173. fixture_ = body->CreateFixture(&fixtureDef_);
  174. fixture_->SetUserData(this);
  175. }
  176. void CollisionShape2D::ReleaseFixture()
  177. {
  178. if (!fixture_)
  179. return;
  180. if (!rigidBody_)
  181. return;
  182. b2Body* body = rigidBody_->GetBody();
  183. if (!body)
  184. return;
  185. body->DestroyFixture(fixture_);
  186. fixture_ = 0;
  187. }
  188. float CollisionShape2D::GetMass() const
  189. {
  190. if (!fixture_)
  191. return 0.0f;
  192. b2MassData massData;
  193. fixture_->GetMassData(&massData);
  194. return massData.mass;
  195. }
  196. float CollisionShape2D::GetInertia() const
  197. {
  198. if (!fixture_)
  199. return 0.0f;
  200. b2MassData massData;
  201. fixture_->GetMassData(&massData);
  202. return massData.I;
  203. }
  204. Vector2 CollisionShape2D::GetMassCenter() const
  205. {
  206. if (!fixture_)
  207. return Vector2::ZERO;
  208. b2MassData massData;
  209. fixture_->GetMassData(&massData);
  210. return ToVector2(massData.center);
  211. }
  212. void CollisionShape2D::OnNodeSet(Node* node)
  213. {
  214. Component::OnNodeSet(node);
  215. if (node)
  216. {
  217. node->AddListener(this);
  218. rigidBody_ = node->GetComponent<RigidBody2D>();
  219. if (rigidBody_)
  220. {
  221. CreateFixture();
  222. rigidBody_->AddCollisionShape2D(this);
  223. }
  224. else
  225. LOGERROR("No right body component in node, can not create collision shape");
  226. }
  227. }
  228. void CollisionShape2D::OnMarkedDirty(Node* node)
  229. {
  230. Vector3 newWorldScale = node_->GetWorldScale();
  231. Vector3 delta = newWorldScale - cachedWorldScale_;
  232. if (delta.DotProduct(delta) < 0.01f)
  233. return;
  234. // Physics operations are not safe from worker threads
  235. Scene* scene = GetScene();
  236. if (scene && scene->IsThreadedUpdate())
  237. {
  238. scene->DelayedMarkedDirty(this);
  239. return;
  240. }
  241. cachedWorldScale_ = newWorldScale;
  242. if (fixture_)
  243. ApplyNodeWorldScale();
  244. }
  245. }