RigidBody2D.cpp 13 KB

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