Joint.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// Base class for all Joint types. Joints constrain how two rigidbodies move relative to one another (e.g. a door
  9. /// hinge). One of the bodies in the joint must always be movable (i.e. non-kinematic).
  10. /// </summary>
  11. public abstract class Joint : Component
  12. {
  13. internal NativeJoint native;
  14. [SerializeField]
  15. internal SerializableData serializableData = new SerializableData();
  16. /// <summary>
  17. /// Triggered when the joint's break force or torque is exceeded.
  18. /// </summary>
  19. public event Action OnJointBreak;
  20. /// <summary>
  21. /// Maximum force the joint can apply before breaking. Broken joints no longer participate in physics simulation.
  22. /// </summary>
  23. public float BreakForce
  24. {
  25. get { return serializableData.breakForce; }
  26. set
  27. {
  28. if (serializableData.breakForce == value)
  29. return;
  30. serializableData.breakForce = value;
  31. if (native != null)
  32. native.BreakForce = value;
  33. }
  34. }
  35. /// <summary>
  36. /// Sets the maximum force the joint can apply before breaking. Broken joints no longer participate in physics
  37. /// simulation.
  38. /// </summary>
  39. public float BreakTorque
  40. {
  41. get { return serializableData.breakTorque; }
  42. set
  43. {
  44. if (serializableData.breakTorque == value)
  45. return;
  46. serializableData.breakTorque = value;
  47. if (native != null)
  48. native.BreakTorque = value;
  49. }
  50. }
  51. /// <summary>
  52. /// Determines whether collisions between the two bodies managed by the joint are enabled.
  53. /// </summary>
  54. public bool EnableCollision
  55. {
  56. get { return serializableData.enableCollision; }
  57. set
  58. {
  59. if (serializableData.enableCollision == value)
  60. return;
  61. serializableData.enableCollision = value;
  62. if (native != null)
  63. native.EnableCollision = value;
  64. }
  65. }
  66. /// <summary>
  67. /// Returns one of the bodies managed by the joint.
  68. /// </summary>
  69. /// <param name="body">Which of the rigidbodies to return.</param>
  70. /// <returns>Rigidbody managed by the joint, or null if none.</returns>
  71. public Rigidbody GetRigidbody(JointBody body)
  72. {
  73. return serializableData.bodies[(int) body];
  74. }
  75. /// <summary>
  76. /// Sets a body managed by the joint. One of the bodies must be movable (i.e. non-kinematic).
  77. /// </summary>
  78. /// <param name="body">Which of the rigidbodies to set.</param>
  79. /// <param name="rigidbody">Rigidbody to managed by the joint, or null. If one of the bodies is null the other
  80. /// one will be anchored globally to the position/rotation set by <see cref="SetPosition"/>
  81. /// and <see cref="SetRotation"/>.</param>
  82. public void SetRigidbody(JointBody body, Rigidbody rigidbody)
  83. {
  84. if (serializableData.bodies[(int)body] == rigidbody)
  85. return;
  86. if (serializableData.bodies[(int)body] != null)
  87. serializableData.bodies[(int)body].SetJoint(null);
  88. serializableData.bodies[(int)body] = rigidbody;
  89. if (rigidbody != null)
  90. serializableData.bodies[(int)body].SetJoint(this);
  91. if (native != null)
  92. {
  93. native.SetRigidbody(body, rigidbody);
  94. UpdateTransform(body);
  95. }
  96. }
  97. /// <summary>
  98. /// Returns the position at which the body is anchored to the joint.
  99. /// </summary>
  100. /// <param name="body">Which body to retrieve position for.</param>
  101. /// <returns>Position relative to the body.</returns>
  102. public Vector3 GetPosition(JointBody body)
  103. {
  104. return serializableData.positions[(int)body];
  105. }
  106. /// <summary>
  107. /// Sets the position at which the body is anchored to the joint.
  108. /// </summary>
  109. /// <param name="body">Which body set the position for.</param>
  110. /// <param name="position">Position relative to the body.</param>
  111. public void SetPosition(JointBody body, Vector3 position)
  112. {
  113. if (serializableData.positions[(int)body] == position)
  114. return;
  115. serializableData.positions[(int) body] = position;
  116. if (native != null)
  117. UpdateTransform(body);
  118. }
  119. /// <summary>
  120. /// Returns the rotation at which the body is anchored to the joint.
  121. /// </summary>
  122. /// <param name="body">Which body to retrieve rotation for.</param>
  123. /// <returns>Rotation relative to the body.</returns>
  124. public Quaternion GetRotation(JointBody body)
  125. {
  126. return serializableData.rotations[(int)body];
  127. }
  128. /// <summary>
  129. /// Sets the rotation at which the body is anchored to the joint.
  130. /// </summary>
  131. /// <param name="body">Which body set the rotation for.</param>
  132. /// <param name="rotation">Rotation relative to the body.</param>
  133. public void SetRotation(JointBody body, Quaternion rotation)
  134. {
  135. if (serializableData.rotations[(int)body] == rotation)
  136. return;
  137. serializableData.rotations[(int)body] = rotation;
  138. if (native != null)
  139. UpdateTransform(body);
  140. }
  141. /// <summary>
  142. /// Triggered when the joint breaks.
  143. /// </summary>
  144. internal void DoOnJointBreak()
  145. {
  146. if (OnJointBreak != null)
  147. OnJointBreak();
  148. }
  149. /// <summary>
  150. /// Notifies the joint that one of the attached rigidbodies moved and that its transform needs updating.
  151. /// </summary>
  152. /// <param name="body">Rigidbody that moved.</param>
  153. internal void NotifyRigidbodyMoved(Rigidbody body)
  154. {
  155. // If physics update is in progress do nothing, as its the joint itself that's probably moving the body
  156. if (Physics.IsUpdateInProgress)
  157. return;
  158. if (serializableData.bodies[0] == body)
  159. UpdateTransform(JointBody.A);
  160. else if (serializableData.bodies[1] == body)
  161. UpdateTransform(JointBody.B);
  162. }
  163. /// <summary>
  164. /// Creates the internal representation of the Joint for use by the component.
  165. /// </summary>
  166. /// <returns>New native joint object.</returns>
  167. internal abstract NativeJoint CreateNative();
  168. private void OnInitialize()
  169. {
  170. NotifyFlags = TransformChangedFlags.Transform | TransformChangedFlags.Parent;
  171. }
  172. private void OnReset()
  173. {
  174. RestoreNative();
  175. }
  176. private void OnEnable()
  177. {
  178. if (native == null)
  179. RestoreNative();
  180. }
  181. private void OnDisable()
  182. {
  183. DestroyNative();
  184. }
  185. private void OnDestroy()
  186. {
  187. if (serializableData.bodies[0] != null)
  188. serializableData.bodies[0].SetJoint(null);
  189. if (serializableData.bodies[1] != null)
  190. serializableData.bodies[1].SetJoint(null);
  191. DestroyNative();
  192. }
  193. private void OnTransformChanged(TransformChangedFlags flags)
  194. {
  195. if (!SceneObject.Active)
  196. return;
  197. // We're ignoring this during physics update because it would cause problems if the joint itself was moved by physics
  198. // Note: This isn't particularily correct because if the joint is being moved by physics but the rigidbodies
  199. // themselves are not parented to the joint, the transform will need updating. However I'm leaving it up to the
  200. // user to ensure rigidbodies are always parented to the joint in such a case (It's an unlikely situation that
  201. // I can't think of an use for - joint transform will almost always be set as an initialization step and not a
  202. // physics response).
  203. if (Physics.IsUpdateInProgress)
  204. return;
  205. UpdateTransform(JointBody.A);
  206. UpdateTransform(JointBody.B);
  207. }
  208. /// <summary>
  209. /// Creates the internal representation of the Joint and restores the values saved by the Component.
  210. /// </summary>
  211. private void RestoreNative()
  212. {
  213. native = CreateNative();
  214. // Note: Merge into one call to avoid many virtual function calls
  215. Rigidbody[] bodies = new Rigidbody[2];
  216. if (serializableData.bodies[0] != null)
  217. bodies[0] = serializableData.bodies[0];
  218. else
  219. bodies[0] = null;
  220. if (serializableData.bodies[1] != null)
  221. bodies[1] = serializableData.bodies[1];
  222. else
  223. bodies[1] = null;
  224. native.SetRigidbody(JointBody.A, bodies[0]);
  225. native.SetRigidbody(JointBody.B, bodies[1]);
  226. native.BreakForce = serializableData.breakForce;
  227. native.BreakTorque = serializableData.breakTorque;
  228. native.EnableCollision = serializableData.enableCollision;
  229. native.BreakTorque = serializableData.breakTorque;
  230. native.EnableCollision = serializableData.enableCollision;
  231. UpdateTransform(JointBody.A);
  232. UpdateTransform(JointBody.B);
  233. }
  234. /// <summary>
  235. /// Destroys the internal joint representation.
  236. /// </summary>
  237. private void DestroyNative()
  238. {
  239. if (native != null)
  240. {
  241. native.Destroy();
  242. native = null;
  243. }
  244. }
  245. /// <summary>
  246. /// Updates the local transform for the specified body attached to the joint.
  247. /// </summary>
  248. /// <param name="body">Body to update.</param>
  249. private void UpdateTransform(JointBody body)
  250. {
  251. Vector3 localPos;
  252. Quaternion localRot;
  253. localPos = serializableData.positions[(int)body];
  254. localRot = serializableData.rotations[(int)body];
  255. // Transform to world space of the related body
  256. Rigidbody rigidbody = serializableData.bodies[(int)body];
  257. if (rigidbody != null)
  258. {
  259. localRot = rigidbody.SceneObject.Rotation * localRot;
  260. localPos = localRot.Rotate(localPos) + rigidbody.SceneObject.Position;
  261. }
  262. // Transform to space local to the joint
  263. Quaternion invRotation = SceneObject.Rotation.Inverse;
  264. localPos = invRotation.Rotate(localPos - SceneObject.Position);
  265. localRot = invRotation * localRot;
  266. native.SetPosition(body, localPos);
  267. native.SetRotation(body, localRot);
  268. }
  269. /// <summary>
  270. /// Holds all data the joint component needs to persist through serialization.
  271. /// </summary>
  272. [SerializeObject]
  273. internal class SerializableData
  274. {
  275. public Rigidbody[] bodies = new Rigidbody[2];
  276. public Vector3[] positions = new Vector3[2];
  277. public Quaternion[] rotations = new Quaternion[2];
  278. public float breakForce = float.MaxValue;
  279. public float breakTorque = float.MaxValue;
  280. public bool enableCollision = false;
  281. }
  282. }
  283. /// <summary>
  284. /// Controls spring parameters for a physics joint limits. If a limit is soft (body bounces back due to restition when
  285. /// the limit is reached) the spring will pull the body back towards the limit using the specified parameters.
  286. /// </summary>
  287. [StructLayout(LayoutKind.Sequential), SerializeObject]
  288. public struct Spring // Note: Must match C++ struct Spring
  289. {
  290. /// <summary>
  291. /// Constructs a spring.
  292. /// </summary>
  293. /// <param name="stiffness">Spring strength.Force proportional to the position error.</param>
  294. /// <param name="damping">Damping strength. Force propertional to the velocity error.</param>
  295. public Spring(float stiffness, float damping)
  296. {
  297. this.stiffness = stiffness;
  298. this.damping = damping;
  299. }
  300. /// <inheritdoc/>
  301. public override bool Equals(object rhs)
  302. {
  303. if (rhs is Spring)
  304. {
  305. Spring other = (Spring)rhs;
  306. return stiffness == other.stiffness && damping == other.damping;
  307. }
  308. return false;
  309. }
  310. public static bool operator ==(Spring a, Spring b)
  311. {
  312. return a.Equals(b);
  313. }
  314. public static bool operator !=(Spring a, Spring b)
  315. {
  316. return !(a == b);
  317. }
  318. /// <summary>
  319. /// Spring strength. Force proportional to the position error.
  320. /// </summary>
  321. public float stiffness;
  322. /// <summary>
  323. /// Damping strength. Force propertional to the velocity error.
  324. /// </summary>
  325. public float damping;
  326. }
  327. /// <summary>
  328. /// Specifies first or second body referenced by a Joint.
  329. /// </summary>
  330. public enum JointBody
  331. {
  332. A, B
  333. };
  334. /// <summary>
  335. /// Specifies axes that the D6 joint can constrain motion on.
  336. /// </summary>
  337. public enum D6JointAxis
  338. {
  339. /// <summary>
  340. /// Movement on the X axis.
  341. /// </summary>
  342. X,
  343. /// <summary>
  344. /// Movement on the Y axis.
  345. /// </summary>
  346. Y,
  347. /// <summary>
  348. /// Movement on the Z axis.
  349. /// </summary>
  350. Z,
  351. /// <summary>
  352. /// Rotation around the X axis.
  353. /// </summary>
  354. Twist,
  355. /// <summary>
  356. /// Rotation around the Y axis.
  357. /// </summary>
  358. SwingY,
  359. /// <summary>
  360. /// Rotation around the Z axis.
  361. /// </summary>
  362. SwingZ,
  363. Count
  364. }
  365. /// <summary>
  366. /// Specifies type of constraint placed on a specific axis of a D6 joint.
  367. /// </summary>
  368. public enum D6JointMotion
  369. {
  370. /// <summary>
  371. /// Axis is immovable.
  372. /// </summary>
  373. Locked,
  374. /// <summary>
  375. /// Axis will be constrained by the specified limits.
  376. /// </summary>
  377. Limited,
  378. /// <summary>
  379. /// Axis will not be constrained.
  380. /// </summary>
  381. Free,
  382. Count
  383. }
  384. /// <summary>
  385. /// Type of drives that can be used for moving or rotating bodies attached to the D6 joint.
  386. /// </summary>
  387. public enum D6JointDriveType
  388. {
  389. /// <summary>
  390. /// Linear movement on the X axis using the linear drive model.
  391. /// </summary>
  392. X,
  393. /// <summary>
  394. /// Linear movement on the Y axis using the linear drive model.
  395. /// </summary>
  396. Y,
  397. /// <summary>
  398. /// Linear movement on the Z axis using the linear drive model.
  399. /// </summary>
  400. Z,
  401. /// <summary>
  402. /// Rotation around the Y axis using the twist/swing angular drive model. Should not be used together with
  403. /// SLERP mode.
  404. /// </summary>
  405. Swing,
  406. /// <summary>
  407. /// Rotation around the Z axis using the twist/swing angular drive model. Should not be used together with
  408. /// SLERP mode.
  409. /// </summary>
  410. Twist,
  411. /// <summary>
  412. /// Rotation using spherical linear interpolation. Uses the SLERP angular drive mode which performs rotation
  413. /// by interpolating the quaternion values directly over the shortest path (applies to all three axes, which
  414. /// they all must be unlocked).
  415. /// </summary>
  416. SLERP,
  417. Count
  418. }
  419. /// <summary>
  420. /// Specifies parameters for a drive that will attempt to move the D6 joint bodies to the specified drive position and
  421. /// velocity.
  422. /// </summary>
  423. public class D6JointDrive
  424. {
  425. /// <summary>
  426. /// Spring strength. Force proportional to the position error.
  427. /// </summary>
  428. public float stiffness = 0.0f;
  429. /// <summary>
  430. /// Damping strength. Force propertional to the velocity error.
  431. /// </summary>
  432. public float damping = 0.0f;
  433. /// <summary>
  434. /// Maximum force the drive can apply.
  435. /// </summary>
  436. public float forceLimit = float.MaxValue;
  437. /// <summary>
  438. /// If true the drive will generate acceleration instead of forces. Acceleration drives are easier to tune as
  439. /// they account for the masses of the actors to which the joint is attached.
  440. /// </summary>
  441. public bool acceleration = false;
  442. /// <inheritdoc/>
  443. public override bool Equals(object rhs)
  444. {
  445. if (rhs is D6JointDrive)
  446. {
  447. D6JointDrive other = (D6JointDrive)rhs;
  448. return stiffness == other.stiffness && damping == other.damping && forceLimit == other.forceLimit
  449. && acceleration == other.acceleration;
  450. }
  451. return false;
  452. }
  453. public static bool operator ==(D6JointDrive a, D6JointDrive b)
  454. {
  455. return a.Equals(b);
  456. }
  457. public static bool operator !=(D6JointDrive a, D6JointDrive b)
  458. {
  459. return !(a == b);
  460. }
  461. /// <summary>
  462. /// Used for accessing drive data from native code.
  463. /// </summary>
  464. /// <param name="output">Native readable drive structure.</param>
  465. private void Internal_GetNative(ref ScriptD6JointDrive output)
  466. {
  467. output.stiffness = stiffness;
  468. output.damping = damping;
  469. output.forceLimit = forceLimit;
  470. output.acceleration = acceleration;
  471. }
  472. }
  473. /// <summary>
  474. /// Properties of a drive that drives the hinge joint's angular velocity towards a paricular value.
  475. /// </summary>
  476. public class HingeJointDrive
  477. {
  478. /// <summary>
  479. /// Target speed of the joint.
  480. /// </summary>
  481. public float speed = 0.0f;
  482. /// <summary>
  483. /// Maximum torque the drive is allowed to apply.
  484. /// </summary>
  485. public float forceLimit = float.MaxValue;
  486. /// <summary>
  487. /// Scales the velocity of the first body, and its response to drive torque is scaled down.
  488. /// </summary>
  489. public float gearRatio = 1.0f;
  490. /// <summary>
  491. /// If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  492. /// the breaking to happen set this to true.
  493. /// </summary>
  494. public bool freeSpin = false;
  495. /// <inheritdoc/>
  496. public override bool Equals(object rhs)
  497. {
  498. if (rhs is HingeJointDrive)
  499. {
  500. HingeJointDrive other = (HingeJointDrive)rhs;
  501. return speed == other.speed && gearRatio == other.gearRatio && forceLimit == other.forceLimit
  502. && freeSpin == other.freeSpin;
  503. }
  504. return false;
  505. }
  506. public static bool operator ==(HingeJointDrive a, HingeJointDrive b)
  507. {
  508. return a.Equals(b);
  509. }
  510. public static bool operator !=(HingeJointDrive a, HingeJointDrive b)
  511. {
  512. return !(a == b);
  513. }
  514. /// <summary>
  515. /// Used for accessing drive data from native code.
  516. /// </summary>
  517. /// <param name="output">Native readable drive structure.</param>
  518. private void Internal_GetNative(ref ScriptHingeJointDrive output)
  519. {
  520. output.speed = speed;
  521. output.forceLimit = forceLimit;
  522. output.gearRatio = gearRatio;
  523. output.freeSpin = freeSpin;
  524. }
  525. };
  526. /// <summary>
  527. /// Contains common values used by all Joint limit types.
  528. /// </summary>
  529. [SerializeObject]
  530. public class LimitCommon
  531. {
  532. public LimitCommon(float contactDist = -1.0f)
  533. {
  534. this.contactDist = contactDist;
  535. this.restitution = 0.0f;
  536. this.spring = new Spring();
  537. }
  538. public LimitCommon(Spring spring, float restitution = 0.0f)
  539. {
  540. this.contactDist = -1.0f;
  541. this.restitution = restitution;
  542. this.spring = spring;
  543. }
  544. /// <inheritdoc/>
  545. public override bool Equals(object rhs)
  546. {
  547. if (rhs is LimitCommon)
  548. {
  549. LimitCommon other = (LimitCommon)rhs;
  550. return contactDist == other.contactDist && restitution == other.restitution && spring == other.spring;
  551. }
  552. return false;
  553. }
  554. public static bool operator ==(LimitCommon a, LimitCommon b)
  555. {
  556. return a.Equals(b);
  557. }
  558. public static bool operator !=(LimitCommon a, LimitCommon b)
  559. {
  560. return !(a == b);
  561. }
  562. /// <summary>
  563. /// Distance from the limit at which it becomes active. Allows the solver to activate earlier than the limit is
  564. /// reached to avoid breaking the limit.
  565. /// </summary>
  566. public float contactDist;
  567. /// <summary>
  568. /// Controls how do objects react when the limit is reached, values closer to zero specify non-ellastic collision,
  569. /// while those closer to one specify more ellastic(i.e bouncy) collision.Must be in [0, 1] range.
  570. /// </summary>
  571. public float restitution;
  572. /// <summary>
  573. /// Spring that controls how are the bodies pulled back towards the limit when they breach it.
  574. /// </summary>
  575. public Spring spring;
  576. }
  577. /// <summary>
  578. /// Represents a joint limit between two distance values. Lower value must be less than the upper value.
  579. /// </summary>
  580. [SerializeObject]
  581. public class LimitLinearRange : LimitCommon
  582. {
  583. /// <summary>
  584. /// Constructs an empty limit.
  585. /// </summary>
  586. public LimitLinearRange()
  587. { }
  588. /// <summary>
  589. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  590. /// </summary>
  591. /// <param name="lower">Lower distance of the limit.Must be less than <paramref name="upper"/>.</param>
  592. /// <param name="upper">Upper distance of the limit.Must be more than <paramref name="lower"/>.</param>
  593. /// <param name="contactDist">Distance from the limit at which it becomes active.Allows the solver to activate
  594. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  595. /// default.</param>
  596. public LimitLinearRange(float lower, float upper, float contactDist = -1.0f)
  597. :base(contactDist)
  598. {
  599. this.lower = lower;
  600. this.upper = upper;
  601. }
  602. /// <summary>
  603. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  604. /// parameter and will be pulled back towards the limit by the provided spring.
  605. /// </summary>
  606. /// <param name="lower">Lower distance of the limit. Must be less than <paramref name="upper"/>.</param>
  607. /// <param name="upper">Upper distance of the limit. Must be more than <paramref name="lower"/>.</param>
  608. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  609. /// </param>
  610. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  611. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  612. /// collision.Must be in [0, 1] range.</param>
  613. public LimitLinearRange(float lower, float upper, Spring spring, float restitution = 0.0f)
  614. :base(spring, restitution)
  615. {
  616. this.lower = lower;
  617. this.upper = upper;
  618. }
  619. /// <inheritdoc/>
  620. public override bool Equals(object rhs)
  621. {
  622. if (rhs is LimitLinearRange)
  623. {
  624. LimitLinearRange other = (LimitLinearRange)rhs;
  625. return base.Equals(rhs) && lower == other.lower && upper == other.upper;
  626. }
  627. return false;
  628. }
  629. public static bool operator ==(LimitLinearRange a, LimitLinearRange b)
  630. {
  631. return a.Equals(b);
  632. }
  633. public static bool operator !=(LimitLinearRange a, LimitLinearRange b)
  634. {
  635. return !(a == b);
  636. }
  637. /// <summary>
  638. /// Lower distance of the limit. Must be less than #upper.
  639. /// </summary>
  640. public float lower;
  641. /// <summary>
  642. /// Upper distance of the limit. Must be more than #lower.
  643. /// </summary>
  644. public float upper;
  645. /// <summary>
  646. /// Used for accessing limit data from native code.
  647. /// </summary>
  648. /// <param name="output">Native readable limit structure.</param>
  649. private void Internal_GetNative(ref ScriptLimitLinearRange output)
  650. {
  651. output.contactDist = contactDist;
  652. output.resitution = restitution;
  653. output.spring = spring;
  654. output.lower = lower;
  655. output.upper = upper;
  656. }
  657. }
  658. /// <summary>
  659. /// Represents a joint limit between zero a single distance value.
  660. /// </summary>
  661. [SerializeObject]
  662. public class LimitLinear : LimitCommon
  663. {
  664. /// <summary>
  665. /// Constructs an empty limit.
  666. /// </summary>
  667. public LimitLinear()
  668. { }
  669. /// <summary>
  670. /// Constructs a hard limit.Once the limit is reached the movement of the attached bodies will come to a stop.
  671. /// </summary>
  672. /// <param name="extent">Distance at which the limit becomes active.</param>
  673. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  674. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  675. /// default.</param>
  676. public LimitLinear(float extent, float contactDist = -1.0f)
  677. :base(contactDist)
  678. {
  679. this.extent = extent;
  680. }
  681. /// <summary>
  682. /// Constructs a soft limit.Once the limit is reached the bodies will bounce back according to the resitution
  683. /// parameter and will be pulled back towards the limit by the provided spring.
  684. /// </summary>
  685. /// <param name="extent">Distance at which the limit becomes active. </param>
  686. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  687. /// </param>
  688. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  689. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  690. /// collision.Must be in [0, 1] range.</param>
  691. public LimitLinear(float extent, Spring spring, float restitution = 0.0f)
  692. :base(spring, restitution)
  693. {
  694. this.extent = extent;
  695. }
  696. /// <inheritdoc/>
  697. public override bool Equals(object rhs)
  698. {
  699. if (rhs is LimitLinear)
  700. {
  701. LimitLinear other = (LimitLinear)rhs;
  702. return base.Equals(rhs) && extent == other.extent;
  703. }
  704. return false;
  705. }
  706. public static bool operator ==(LimitLinear a, LimitLinear b)
  707. {
  708. return a.Equals(b);
  709. }
  710. public static bool operator !=(LimitLinear a, LimitLinear b)
  711. {
  712. return !(a == b);
  713. }
  714. /// <summary>
  715. /// Distance at which the limit becomes active.
  716. /// </summary>
  717. public float extent = 0.0f;
  718. /// <summary>
  719. /// Used for accessing limit data from native code.
  720. /// </summary>
  721. /// <param name="output">Native readable limit structure.</param>
  722. private void Internal_GetNative(ref ScriptLimitLinear output)
  723. {
  724. output.contactDist = contactDist;
  725. output.resitution = restitution;
  726. output.spring = spring;
  727. output.extent = extent;
  728. }
  729. }
  730. /// <summary>
  731. /// Represents a joint limit between two angles.
  732. /// </summary>
  733. [SerializeObject]
  734. public class LimitAngularRange : LimitCommon
  735. {
  736. /// <summary>
  737. /// Constructs an empty limit.
  738. /// </summary>
  739. public LimitAngularRange()
  740. { }
  741. /// <summary>
  742. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  743. /// </summary>
  744. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  745. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  746. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  747. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  748. /// default.</param>
  749. public LimitAngularRange(Radian lower, Radian upper, float contactDist = -1.0f)
  750. : base(contactDist)
  751. {
  752. this.lower = lower;
  753. this.upper = upper;
  754. }
  755. /// <summary>
  756. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  757. /// parameter and will be pulled back towards the limit by the provided spring.
  758. /// </summary>
  759. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  760. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  761. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  762. /// </param>
  763. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  764. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  765. /// collision.Must be in [0, 1] range.</param>
  766. public LimitAngularRange(Radian lower, Radian upper, Spring spring, float restitution = 0.0f)
  767. : base(spring, restitution)
  768. {
  769. this.lower = lower;
  770. this.upper = upper;
  771. }
  772. /// <inheritdoc/>
  773. public override bool Equals(object rhs)
  774. {
  775. if (rhs is LimitAngularRange)
  776. {
  777. LimitAngularRange other = (LimitAngularRange)rhs;
  778. return base.Equals(rhs) && lower == other.lower && upper == other.upper;
  779. }
  780. return false;
  781. }
  782. public static bool operator ==(LimitAngularRange a, LimitAngularRange b)
  783. {
  784. return a.Equals(b);
  785. }
  786. public static bool operator !=(LimitAngularRange a, LimitAngularRange b)
  787. {
  788. return !(a == b);
  789. }
  790. /// <summary>
  791. /// Lower angle of the limit. Must be less than #upper.
  792. /// </summary>
  793. public Radian lower = new Radian(0.0f);
  794. /// <summary>
  795. /// Upper angle of the limit. Must be less than #lower.
  796. /// </summary>
  797. public Radian upper = new Radian(0.0f);
  798. /// <summary>
  799. /// Used for accessing limit data from native code.
  800. /// </summary>
  801. /// <param name="output">Native readable limit structure.</param>
  802. private void Internal_GetNative(ref ScriptLimitAngularRange output)
  803. {
  804. output.contactDist = contactDist;
  805. output.resitution = restitution;
  806. output.spring = spring;
  807. output.lower = lower;
  808. output.upper = upper;
  809. }
  810. }
  811. /// <summary>
  812. /// Represents a joint limit that contraints movement to within an elliptical cone.
  813. /// </summary>
  814. [SerializeObject]
  815. public class LimitConeRange : LimitCommon
  816. {
  817. /// <summary>
  818. /// Constructs a limit with a 45 degree cone.
  819. /// </summary>
  820. public LimitConeRange()
  821. { }
  822. /// <summary>
  823. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  824. /// </summary>
  825. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  826. /// </param>
  827. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  828. /// </param>
  829. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  830. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  831. /// default.</param>
  832. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, float contactDist = -1.0f)
  833. : base(contactDist)
  834. {
  835. this.yLimitAngle = yLimitAngle;
  836. this.zLimitAngle = zLimitAngle;
  837. }
  838. /// <summary>
  839. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  840. /// parameter and will be pulled back towards the limit by the provided spring.
  841. /// </summary>
  842. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  843. /// </param>
  844. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  845. /// </param>
  846. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  847. /// </param>
  848. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  849. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  850. /// collision.Must be in [0, 1] range.</param>
  851. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, Spring spring, float restitution = 0.0f)
  852. : base(spring, restitution)
  853. {
  854. this.yLimitAngle = yLimitAngle;
  855. this.zLimitAngle = zLimitAngle;
  856. }
  857. /// <inheritdoc/>
  858. public override bool Equals(object rhs)
  859. {
  860. if (rhs is LimitConeRange)
  861. {
  862. LimitConeRange other = (LimitConeRange)rhs;
  863. return base.Equals(rhs) && yLimitAngle == other.yLimitAngle && zLimitAngle == other.zLimitAngle;
  864. }
  865. return false;
  866. }
  867. public static bool operator ==(LimitConeRange a, LimitConeRange b)
  868. {
  869. return a.Equals(b);
  870. }
  871. public static bool operator !=(LimitConeRange a, LimitConeRange b)
  872. {
  873. return !(a == b);
  874. }
  875. /// <summary>
  876. /// Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  877. /// </summary>
  878. public Radian yLimitAngle = new Radian(MathEx.Pi * 0.5f);
  879. /// <summary>
  880. /// Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  881. /// </summary>
  882. public Radian zLimitAngle = new Radian(MathEx.Pi * 0.5f);
  883. /// <summary>
  884. /// Used for accessing limit data from native code.
  885. /// </summary>
  886. /// <param name="output">Native readable limit structure.</param>
  887. private void Internal_GetNative(ref ScriptLimitConeRange output)
  888. {
  889. output.contactDist = contactDist;
  890. output.resitution = restitution;
  891. output.spring = spring;
  892. output.yLimitAngle = yLimitAngle;
  893. output.zLimitAngle = zLimitAngle;
  894. }
  895. }
  896. /// <summary>
  897. /// Used for passing HingeJointDrive data between native and managed code.
  898. /// </summary>
  899. [StructLayout(LayoutKind.Sequential)]
  900. internal struct ScriptHingeJointDrive // Note: Must match C++ struct HingeJoint::Drive
  901. {
  902. public float speed;
  903. public float forceLimit;
  904. public float gearRatio;
  905. public bool freeSpin;
  906. }
  907. /// <summary>
  908. /// Used for passing D6JointDrive data between native and managed code.
  909. /// </summary>
  910. [StructLayout(LayoutKind.Sequential)]
  911. internal struct ScriptD6JointDrive // Note: Must match C++ struct D6Joint::Drive
  912. {
  913. public float stiffness;
  914. public float damping;
  915. public float forceLimit;
  916. public bool acceleration;
  917. }
  918. /// <summary>
  919. /// Used for passing LimitLinearRange data between native and managed code.
  920. /// </summary>
  921. [StructLayout(LayoutKind.Sequential)]
  922. internal struct ScriptLimitLinearRange // Note: Must match C++ struct LimitLinearRange
  923. {
  924. public float contactDist;
  925. public float resitution;
  926. public Spring spring;
  927. public float lower;
  928. public float upper;
  929. }
  930. /// <summary>
  931. /// Used for passing LimitLinear data between native and managed code.
  932. /// </summary>
  933. [StructLayout(LayoutKind.Sequential)]
  934. internal struct ScriptLimitLinear // Note: Must match C++ struct LimitLinear
  935. {
  936. public float contactDist;
  937. public float resitution;
  938. public Spring spring;
  939. public float extent;
  940. }
  941. /// <summary>
  942. /// Used for passing LimitAngularRange data between native and managed code.
  943. /// </summary>
  944. [StructLayout(LayoutKind.Sequential)]
  945. internal struct ScriptLimitAngularRange // Note: Must match C++ struct LimitAngularRange
  946. {
  947. public float contactDist;
  948. public float resitution;
  949. public Spring spring;
  950. public Radian lower;
  951. public Radian upper;
  952. }
  953. /// <summary>
  954. /// Used for passing LimitConeRange data between native and managed code.
  955. /// </summary>
  956. [StructLayout(LayoutKind.Sequential)]
  957. internal struct ScriptLimitConeRange // Note: Must match C++ struct LimitConeRange
  958. {
  959. public float contactDist;
  960. public float resitution;
  961. public Spring spring;
  962. public Radian yLimitAngle;
  963. public Radian zLimitAngle;
  964. }
  965. }