RigidBody2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "Constraint2D.h"
  25. #include "Context.h"
  26. #include "Log.h"
  27. #include "PhysicsUtils2D.h"
  28. #include "PhysicsWorld2D.h"
  29. #include "RigidBody2D.h"
  30. #include "Scene.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. extern const char* URHO2D_CATEGORY;
  35. static const BodyType2D DEFAULT_BODYTYPE = BT_STATIC;
  36. static const char* bodyTypeNames[] =
  37. {
  38. "Static",
  39. "Dynamic",
  40. "Kinematic",
  41. 0
  42. };
  43. RigidBody2D::RigidBody2D(Context* context) :
  44. Component(context),
  45. massData_(), // b2MassData structure does not have a constructor so need to zero-initialize all its members
  46. useFixtureMass_(true),
  47. body_(0)
  48. {
  49. // Make sure the massData's center is zero-initialized as well
  50. massData_.center.SetZero();
  51. }
  52. RigidBody2D::~RigidBody2D()
  53. {
  54. if (physicsWorld_)
  55. {
  56. ReleaseBody();
  57. physicsWorld_->RemoveRigidBody(this);
  58. }
  59. }
  60. void RigidBody2D::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<RigidBody2D>(URHO2D_CATEGORY);
  63. ENUM_ACCESSOR_ATTRIBUTE("Body Type", GetBodyType, SetBodyType, BodyType2D, bodyTypeNames, DEFAULT_BODYTYPE, AM_DEFAULT);
  64. ACCESSOR_ATTRIBUTE("Mass", GetMass, SetMass, float, 0.0f, AM_DEFAULT);
  65. ACCESSOR_ATTRIBUTE("Inertia", GetInertia, SetInertia, float, 0.0f, AM_DEFAULT);
  66. MIXED_ACCESSOR_ATTRIBUTE("Mass Center", GetMassCenter, SetMassCenter, Vector2, Vector2::ZERO, AM_DEFAULT);
  67. ACCESSOR_ATTRIBUTE("Use Fixture Mass", GetUseFixtureMass, SetUseFixtureMass, bool, true, AM_DEFAULT);
  68. ACCESSOR_ATTRIBUTE("Linear Damping", GetLinearDamping, SetLinearDamping, float, 0.0f, AM_DEFAULT);
  69. ACCESSOR_ATTRIBUTE("Angular Damping", GetAngularDamping, SetAngularDamping, float, 0.0f, AM_DEFAULT);
  70. ACCESSOR_ATTRIBUTE("Allow Sleep", IsAllowSleep, SetAllowSleep, bool, true, AM_DEFAULT);
  71. ACCESSOR_ATTRIBUTE("Fixed Rotation", IsFixedRotation, SetFixedRotation, bool, false, AM_DEFAULT);
  72. ACCESSOR_ATTRIBUTE("Bullet", IsBullet, SetBullet, bool, false, AM_DEFAULT);
  73. ACCESSOR_ATTRIBUTE("Gravity Scale", GetGravityScale, SetGravityScale, float, 1.0f, AM_DEFAULT);
  74. ACCESSOR_ATTRIBUTE("Awake", IsAwake, SetAwake, bool, true, AM_DEFAULT);
  75. MIXED_ACCESSOR_ATTRIBUTE("Linear Velocity", GetLinearVelocity, SetLinearVelocity, Vector2, Vector2::ZERO, AM_DEFAULT);
  76. ACCESSOR_ATTRIBUTE("Angular Velocity", GetAngularVelocity, SetAngularVelocity, float, 0.0f, AM_DEFAULT);
  77. }
  78. void RigidBody2D::OnSetEnabled()
  79. {
  80. bool enabled = IsEnabledEffective();
  81. bodyDef_.active = enabled;
  82. if (body_)
  83. body_->SetActive(enabled);
  84. MarkNetworkUpdate();
  85. }
  86. void RigidBody2D::SetBodyType(BodyType2D type)
  87. {
  88. b2BodyType bodyType = (b2BodyType)type;
  89. if (bodyDef_.type == bodyType)
  90. return;
  91. bodyDef_.type = bodyType;
  92. if (body_)
  93. body_->SetType(bodyType);
  94. MarkNetworkUpdate();
  95. }
  96. void RigidBody2D::SetMass(float mass)
  97. {
  98. mass = Max(mass, 0.0f);
  99. if (massData_.mass == mass)
  100. return;
  101. massData_.mass = mass;
  102. if (useFixtureMass_ && body_)
  103. body_->SetMassData(&massData_);
  104. MarkNetworkUpdate();
  105. }
  106. void RigidBody2D::SetInertia(float inertia)
  107. {
  108. inertia = Max(inertia, 0.0f);
  109. if (massData_.I == inertia)
  110. return;
  111. massData_.I = inertia;
  112. if (useFixtureMass_ && body_)
  113. body_->SetMassData(&massData_);
  114. MarkNetworkUpdate();
  115. }
  116. void RigidBody2D::SetMassCenter(const Vector2& center)
  117. {
  118. b2Vec2 b2Center = ToB2Vec2(center);
  119. if (massData_.center == b2Center)
  120. return;
  121. massData_.center = b2Center;
  122. if (useFixtureMass_ && body_)
  123. body_->SetMassData(&massData_);
  124. MarkNetworkUpdate();
  125. }
  126. void RigidBody2D::SetUseFixtureMass(bool useFixtureMass)
  127. {
  128. if (useFixtureMass_ == useFixtureMass)
  129. return;
  130. useFixtureMass_ = useFixtureMass;
  131. if (body_)
  132. {
  133. if (useFixtureMass_)
  134. body_->ResetMassData();
  135. else
  136. body_->SetMassData(&massData_);
  137. }
  138. MarkNetworkUpdate();
  139. }
  140. void RigidBody2D::SetLinearDamping(float linearDamping)
  141. {
  142. if (bodyDef_.linearDamping == linearDamping)
  143. return;
  144. bodyDef_.linearDamping = linearDamping;
  145. if (body_)
  146. body_->SetLinearDamping(linearDamping);
  147. MarkNetworkUpdate();
  148. }
  149. void RigidBody2D::SetAngularDamping(float angularDamping)
  150. {
  151. if (bodyDef_.angularDamping == angularDamping)
  152. return;
  153. bodyDef_.angularDamping = angularDamping;
  154. if (body_)
  155. body_->SetAngularDamping(angularDamping);
  156. MarkNetworkUpdate();
  157. }
  158. void RigidBody2D::SetAllowSleep(bool allowSleep)
  159. {
  160. if (bodyDef_.allowSleep == allowSleep)
  161. return;
  162. bodyDef_.allowSleep = allowSleep;
  163. if (body_)
  164. body_->SetSleepingAllowed(allowSleep);
  165. MarkNetworkUpdate();
  166. }
  167. void RigidBody2D::SetFixedRotation(bool fixedRotation)
  168. {
  169. if (bodyDef_.fixedRotation == fixedRotation)
  170. return;
  171. bodyDef_.fixedRotation = fixedRotation;
  172. if (body_)
  173. body_->SetFixedRotation(fixedRotation);
  174. MarkNetworkUpdate();
  175. }
  176. void RigidBody2D::SetBullet(bool bullet)
  177. {
  178. if (bodyDef_.bullet == bullet)
  179. return;
  180. bodyDef_.bullet = bullet;
  181. if (body_)
  182. body_->SetBullet(bullet);
  183. MarkNetworkUpdate();
  184. }
  185. void RigidBody2D::SetGravityScale(float gravityScale)
  186. {
  187. if (bodyDef_.gravityScale == gravityScale)
  188. return;
  189. bodyDef_.gravityScale = gravityScale;
  190. if (body_)
  191. body_->SetGravityScale(gravityScale);
  192. MarkNetworkUpdate();
  193. }
  194. void RigidBody2D::SetAwake(bool awake)
  195. {
  196. if (bodyDef_.awake == awake)
  197. return;
  198. bodyDef_.awake = awake;
  199. if (body_)
  200. body_->SetAwake(awake);
  201. MarkNetworkUpdate();
  202. }
  203. void RigidBody2D::SetLinearVelocity(const Vector2& linearVelocity)
  204. {
  205. b2Vec2 b2linearVelocity = ToB2Vec2(linearVelocity);
  206. if (bodyDef_.linearVelocity == b2linearVelocity)
  207. return;
  208. bodyDef_.linearVelocity = b2linearVelocity;
  209. if (body_)
  210. body_->SetLinearVelocity(b2linearVelocity);
  211. MarkNetworkUpdate();
  212. }
  213. void RigidBody2D::SetAngularVelocity(float angularVelocity)
  214. {
  215. if (bodyDef_.angularVelocity == angularVelocity)
  216. return;
  217. bodyDef_.angularVelocity = angularVelocity;
  218. if (body_)
  219. body_->SetAngularVelocity(angularVelocity);
  220. MarkNetworkUpdate();
  221. }
  222. void RigidBody2D::ApplyForce(const Vector2& force, const Vector2& point, bool wake)
  223. {
  224. if (body_ && force != Vector2::ZERO)
  225. body_->ApplyForce(ToB2Vec2(force), ToB2Vec2(point), wake);
  226. }
  227. void RigidBody2D::ApplyForceToCenter(const Vector2& force, bool wake)
  228. {
  229. if (body_ && force != Vector2::ZERO)
  230. body_->ApplyForceToCenter(ToB2Vec2(force), wake);
  231. }
  232. void RigidBody2D::ApplyTorque(float torque, bool wake)
  233. {
  234. if (body_ && torque != 0)
  235. body_->ApplyTorque(torque, wake);
  236. }
  237. void RigidBody2D::ApplyLinearImpulse(const Vector2& impulse, const Vector2& point, bool wake)
  238. {
  239. if (body_ && impulse != Vector2::ZERO)
  240. body_->ApplyLinearImpulse(ToB2Vec2(impulse), ToB2Vec2(point), wake);
  241. }
  242. void RigidBody2D::ApplyAngularImpulse(float impulse, bool wake)
  243. {
  244. if (body_)
  245. body_->ApplyAngularImpulse(impulse, wake);
  246. }
  247. void RigidBody2D::CreateBody()
  248. {
  249. if (body_)
  250. return;
  251. if (!physicsWorld_ || !physicsWorld_->GetWorld())
  252. return;
  253. bodyDef_.position = ToB2Vec2(node_->GetWorldPosition());;
  254. bodyDef_.angle = node_->GetWorldRotation().RollAngle() * M_DEGTORAD;
  255. body_ = physicsWorld_->GetWorld()->CreateBody(&bodyDef_);
  256. body_->SetUserData(this);
  257. for (unsigned i = 0; i < collisionShapes_.Size(); ++i)
  258. {
  259. if (collisionShapes_[i])
  260. collisionShapes_[i]->CreateFixture();
  261. }
  262. if (!useFixtureMass_)
  263. body_->SetMassData(&massData_);
  264. for (unsigned i = 0; i < constraints_.Size(); ++i)
  265. {
  266. if (constraints_[i])
  267. constraints_[i]->CreateJoint();
  268. }
  269. }
  270. void RigidBody2D::ReleaseBody()
  271. {
  272. if (!body_)
  273. return;
  274. if (!physicsWorld_ || !physicsWorld_->GetWorld())
  275. return;
  276. for (unsigned i = 0; i < constraints_.Size(); ++i)
  277. {
  278. if (constraints_[i])
  279. constraints_[i]->ReleaseJoint();
  280. }
  281. for (unsigned i = 0; i < collisionShapes_.Size(); ++i)
  282. {
  283. if (collisionShapes_[i])
  284. collisionShapes_[i]->ReleaseFixture();
  285. }
  286. physicsWorld_->GetWorld()->DestroyBody(body_);
  287. body_ = 0;
  288. }
  289. void RigidBody2D::ApplyWorldTransform()
  290. {
  291. if (!body_)
  292. return;
  293. physicsWorld_->SetApplyingTransforms(true);
  294. Node* node = GetNode();
  295. const b2Transform& transform = body_->GetTransform();
  296. node->SetWorldPosition(ToVector3(transform.p));
  297. node->SetWorldRotation(Quaternion(transform.q.GetAngle() * M_RADTODEG, Vector3::FORWARD));
  298. physicsWorld_->SetApplyingTransforms(false);
  299. }
  300. void RigidBody2D::AddCollisionShape2D(CollisionShape2D* collisionShape)
  301. {
  302. if (!collisionShape)
  303. return;
  304. WeakPtr<CollisionShape2D> collisionShapePtr(collisionShape);
  305. if (collisionShapes_.Contains(collisionShapePtr))
  306. return;
  307. collisionShapes_.Push(collisionShapePtr);
  308. }
  309. void RigidBody2D::RemoveCollisionShape2D(CollisionShape2D* collisionShape)
  310. {
  311. if (!collisionShape)
  312. return;
  313. WeakPtr<CollisionShape2D> collisionShapePtr(collisionShape);
  314. collisionShapes_.Remove(collisionShapePtr);
  315. }
  316. void RigidBody2D::AddConstraint2D(Constraint2D* constraint)
  317. {
  318. if (!constraint)
  319. return;
  320. WeakPtr<Constraint2D> constraintPtr(constraint);
  321. if (constraints_.Contains(constraintPtr))
  322. return;
  323. constraints_.Push(constraintPtr);
  324. }
  325. void RigidBody2D::RemoveConstraint2D(Constraint2D* constraint)
  326. {
  327. if (!constraint)
  328. return;
  329. WeakPtr<Constraint2D> constraintPtr(constraint);
  330. constraints_.Remove(constraintPtr);
  331. }
  332. float RigidBody2D::GetMass() const
  333. {
  334. if (!useFixtureMass_)
  335. return massData_.mass;
  336. return body_ ? body_->GetMass() : 0.0f;
  337. }
  338. float RigidBody2D::GetInertia() const
  339. {
  340. if (!useFixtureMass_)
  341. return massData_.I;
  342. return body_ ? body_->GetInertia() : 0.0f;
  343. }
  344. Vector2 RigidBody2D::GetMassCenter() const
  345. {
  346. if (!useFixtureMass_)
  347. return ToVector2(massData_.center);
  348. return body_ ? ToVector2(body_->GetLocalCenter()) : Vector2::ZERO;
  349. }
  350. bool RigidBody2D::IsAwake() const
  351. {
  352. return body_ ? body_->IsAwake() : bodyDef_.awake;
  353. }
  354. Vector2 RigidBody2D::GetLinearVelocity() const
  355. {
  356. return ToVector2(body_ ? body_->GetLinearVelocity() : bodyDef_.linearVelocity);
  357. }
  358. float RigidBody2D::GetAngularVelocity() const
  359. {
  360. return body_ ? body_->GetAngularVelocity() : bodyDef_.angularVelocity;
  361. }
  362. void RigidBody2D::OnNodeSet(Node* node)
  363. {
  364. Component::OnNodeSet(node);
  365. if (node)
  366. {
  367. node->AddListener(this);
  368. Scene* scene = GetScene();
  369. physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld2D>();
  370. CreateBody();
  371. physicsWorld_->AddRigidBody(this);
  372. }
  373. }
  374. void RigidBody2D::OnMarkedDirty(Node* node)
  375. {
  376. if (physicsWorld_ && physicsWorld_->IsApplyingTransforms())
  377. return;
  378. // Physics operations are not safe from worker threads
  379. Scene* scene = GetScene();
  380. if (scene && scene->IsThreadedUpdate())
  381. {
  382. scene->DelayedMarkedDirty(this);
  383. return;
  384. }
  385. // Check if transform has changed from the last one set in ApplyWorldTransform()
  386. b2Vec2 newPosition = ToB2Vec2(node_->GetWorldPosition());
  387. float newAngle = node_->GetWorldRotation().RollAngle() * M_DEGTORAD;
  388. if (newPosition != bodyDef_.position || newAngle != bodyDef_.angle)
  389. {
  390. bodyDef_.position = newPosition;
  391. bodyDef_.angle = newAngle;
  392. if (body_)
  393. body_->SetTransform(newPosition, newAngle);
  394. }
  395. }
  396. }