Joint.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 restitution 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. /// <inheritdoc/>
  311. public override int GetHashCode()
  312. {
  313. return base.GetHashCode();
  314. }
  315. public static bool operator ==(Spring a, Spring b)
  316. {
  317. return a.Equals(b);
  318. }
  319. public static bool operator !=(Spring a, Spring b)
  320. {
  321. return !(a == b);
  322. }
  323. /// <summary>
  324. /// Spring strength. Force proportional to the position error.
  325. /// </summary>
  326. public float stiffness;
  327. /// <summary>
  328. /// Damping strength. Force propertional to the velocity error.
  329. /// </summary>
  330. public float damping;
  331. }
  332. /// <summary>
  333. /// Specifies first or second body referenced by a Joint.
  334. /// </summary>
  335. public enum JointBody
  336. {
  337. A, B
  338. };
  339. /// <summary>
  340. /// Specifies axes that the D6 joint can constrain motion on.
  341. /// </summary>
  342. public enum D6JointAxis
  343. {
  344. /// <summary>
  345. /// Movement on the X axis.
  346. /// </summary>
  347. X,
  348. /// <summary>
  349. /// Movement on the Y axis.
  350. /// </summary>
  351. Y,
  352. /// <summary>
  353. /// Movement on the Z axis.
  354. /// </summary>
  355. Z,
  356. /// <summary>
  357. /// Rotation around the X axis.
  358. /// </summary>
  359. Twist,
  360. /// <summary>
  361. /// Rotation around the Y axis.
  362. /// </summary>
  363. SwingY,
  364. /// <summary>
  365. /// Rotation around the Z axis.
  366. /// </summary>
  367. SwingZ,
  368. Count
  369. }
  370. /// <summary>
  371. /// Specifies type of constraint placed on a specific axis of a D6 joint.
  372. /// </summary>
  373. public enum D6JointMotion
  374. {
  375. /// <summary>
  376. /// Axis is immovable.
  377. /// </summary>
  378. Locked,
  379. /// <summary>
  380. /// Axis will be constrained by the specified limits.
  381. /// </summary>
  382. Limited,
  383. /// <summary>
  384. /// Axis will not be constrained.
  385. /// </summary>
  386. Free,
  387. Count
  388. }
  389. /// <summary>
  390. /// Type of drives that can be used for moving or rotating bodies attached to the D6 joint.
  391. /// </summary>
  392. public enum D6JointDriveType
  393. {
  394. /// <summary>
  395. /// Linear movement on the X axis using the linear drive model.
  396. /// </summary>
  397. X,
  398. /// <summary>
  399. /// Linear movement on the Y axis using the linear drive model.
  400. /// </summary>
  401. Y,
  402. /// <summary>
  403. /// Linear movement on the Z axis using the linear drive model.
  404. /// </summary>
  405. Z,
  406. /// <summary>
  407. /// Rotation around the Y axis using the twist/swing angular drive model. Should not be used together with
  408. /// SLERP mode.
  409. /// </summary>
  410. Swing,
  411. /// <summary>
  412. /// Rotation around the Z axis using the twist/swing angular drive model. Should not be used together with
  413. /// SLERP mode.
  414. /// </summary>
  415. Twist,
  416. /// <summary>
  417. /// Rotation using spherical linear interpolation. Uses the SLERP angular drive mode which performs rotation
  418. /// by interpolating the quaternion values directly over the shortest path (applies to all three axes, which
  419. /// they all must be unlocked).
  420. /// </summary>
  421. SLERP,
  422. Count
  423. }
  424. /// <summary>
  425. /// Specifies parameters for a drive that will attempt to move the D6 joint bodies to the specified drive position and
  426. /// velocity.
  427. /// </summary>
  428. [SerializeObject]
  429. public class D6JointDrive
  430. {
  431. [SerializeField]
  432. private D6JointDriveData data;
  433. /// <summary>
  434. /// Spring strength. Force proportional to the position error.
  435. /// </summary>
  436. public float Stiffness { get { return data.stiffness; } }
  437. /// <summary>
  438. /// Damping strength. Force propertional to the velocity error.
  439. /// </summary>
  440. public float Damping { get { return data.damping; } }
  441. /// <summary>
  442. /// Maximum force the drive can apply.
  443. /// </summary>
  444. public float ForceLimit { get { return data.forceLimit; } }
  445. /// <summary>
  446. /// If true the drive will generate acceleration instead of forces. Acceleration drives are easier to tune as
  447. /// they account for the masses of the actors to which the joint is attached.
  448. /// </summary>
  449. public bool Acceleration { get { return data.acceleration; } }
  450. /// <summary>
  451. /// Gets drive properties.
  452. /// </summary>
  453. public D6JointDriveData Data
  454. {
  455. get { return data; }
  456. }
  457. /// <summary>
  458. /// Constructs a new D6 joint drive.
  459. /// </summary>
  460. /// <param name="stiffness"><see cref="Stiffness"/></param>
  461. /// <param name="damping"><see cref="Damping"/></param>
  462. /// <param name="forceLimit"><see cref="ForceLimit"/></param>
  463. /// <param name="acceleration"><see cref="Acceleration"/></param>
  464. public D6JointDrive(float stiffness = 0.0f, float damping = 0.0f, float forceLimit = float.MaxValue,
  465. bool acceleration = false)
  466. {
  467. data.stiffness = stiffness;
  468. data.damping = damping;
  469. data.forceLimit = forceLimit;
  470. data.acceleration = acceleration;
  471. }
  472. /// <summary>
  473. /// Constructs a new D6 joint drive.
  474. /// </summary>
  475. /// <param name="data">Properties to initialize the drive with.</param>
  476. public D6JointDrive(D6JointDriveData data)
  477. {
  478. this.data = data;
  479. }
  480. /// <inheritdoc/>
  481. public override bool Equals(object rhs)
  482. {
  483. if (rhs is D6JointDrive)
  484. {
  485. D6JointDrive other = (D6JointDrive)rhs;
  486. return Stiffness == other.Stiffness && Damping == other.Damping && ForceLimit == other.ForceLimit
  487. && Acceleration == other.Acceleration;
  488. }
  489. return false;
  490. }
  491. /// <inheritdoc/>
  492. public override int GetHashCode()
  493. {
  494. return base.GetHashCode();
  495. }
  496. public static bool operator ==(D6JointDrive a, D6JointDrive b)
  497. {
  498. return a.Equals(b);
  499. }
  500. public static bool operator !=(D6JointDrive a, D6JointDrive b)
  501. {
  502. return !(a == b);
  503. }
  504. /// <summary>
  505. /// Used for accessing drive data from native code.
  506. /// </summary>
  507. /// <param name="output">Native readable drive structure.</param>
  508. private void Internal_GetNative(out D6JointDriveData output)
  509. {
  510. output = data;
  511. }
  512. }
  513. /// <summary>
  514. /// Properties of a drive that drives the hinge joint's angular velocity towards a paricular value.
  515. /// </summary>
  516. [SerializeObject]
  517. public class HingeJointDrive
  518. {
  519. [SerializeField]
  520. private HingeJointDriveData data;
  521. /// <summary>
  522. /// Target speed of the joint.
  523. /// </summary>
  524. public float Speed { get { return data.speed; } }
  525. /// <summary>
  526. /// Maximum torque the drive is allowed to apply.
  527. /// </summary>
  528. public float ForceLimit { get { return data.forceLimit; } }
  529. /// <summary>
  530. /// Scales the velocity of the first body, and its response to drive torque is scaled down.
  531. /// </summary>
  532. public float GearRatio { get { return data.gearRatio; } }
  533. /// <summary>
  534. /// If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  535. /// the breaking to happen set this to true.
  536. /// </summary>
  537. public bool FreeSpin { get { return data.freeSpin; } }
  538. /// <summary>
  539. /// Gets drive properties.
  540. /// </summary>
  541. public HingeJointDriveData Data
  542. {
  543. get { return data; }
  544. }
  545. /// <summary>
  546. /// Constructs a new hinge joint drive.
  547. /// </summary>
  548. /// <param name="speed"><see cref="Speed"/></param>
  549. /// <param name="forceLimit"><see cref="ForceLimit"/></param>
  550. /// <param name="gearRatio"><see cref="GearRatio"/></param>
  551. /// <param name="freeSpin"><see cref="FreeSpin"/></param>
  552. public HingeJointDrive(float speed = 0.0f, float forceLimit = float.MaxValue,
  553. float gearRatio = 1.0f, bool freeSpin = false)
  554. {
  555. data.speed = speed;
  556. data.forceLimit = forceLimit;
  557. data.gearRatio = gearRatio;
  558. data.freeSpin = freeSpin;
  559. }
  560. /// <summary>
  561. /// Constructs a new hinge joint drive.
  562. /// </summary>
  563. /// <param name="data">Properties to initialize the drive with.</param>
  564. public HingeJointDrive(HingeJointDriveData data)
  565. {
  566. this.data = data;
  567. }
  568. /// <inheritdoc/>
  569. public override bool Equals(object rhs)
  570. {
  571. if (rhs is HingeJointDrive)
  572. {
  573. HingeJointDrive other = (HingeJointDrive)rhs;
  574. return data.speed == other.data.speed && data.gearRatio == other.data.gearRatio &&
  575. data.forceLimit == other.data.forceLimit && data.freeSpin == other.data.freeSpin;
  576. }
  577. return false;
  578. }
  579. /// <inheritdoc/>
  580. public override int GetHashCode()
  581. {
  582. return base.GetHashCode();
  583. }
  584. public static bool operator ==(HingeJointDrive a, HingeJointDrive b)
  585. {
  586. return a.Equals(b);
  587. }
  588. public static bool operator !=(HingeJointDrive a, HingeJointDrive b)
  589. {
  590. return !(a == b);
  591. }
  592. /// <summary>
  593. /// Used for accessing drive data from native code.
  594. /// </summary>
  595. /// <param name="output">Native readable drive structure.</param>
  596. private void Internal_GetNative(out HingeJointDriveData output)
  597. {
  598. output = data;
  599. }
  600. };
  601. /// <summary>
  602. /// Contains common values used by all Joint limit types.
  603. /// </summary>
  604. [SerializeObject]
  605. public class LimitCommon
  606. {
  607. private LimitCommonData data;
  608. /// <summary>
  609. /// Distance from the limit at which it becomes active. Allows the solver to activate earlier than the limit is
  610. /// reached to avoid breaking the limit.
  611. /// </summary>
  612. public float ContactDist { get { return data.contactDist; } }
  613. /// <summary>
  614. /// Controls how do objects react when the limit is reached, values closer to zero specify non-ellastic collision,
  615. /// while those closer to one specify more ellastic(i.e bouncy) collision.Must be in [0, 1] range.
  616. /// </summary>
  617. public float Restitution { get { return data.restitution; } }
  618. /// <summary>
  619. /// Spring that controls how are the bodies pulled back towards the limit when they breach it.
  620. /// </summary>
  621. public Spring Spring { get { return data.spring; } }
  622. /// <summary>
  623. /// Gets properties common to all limit types.
  624. /// </summary>
  625. public LimitCommonData CommonData
  626. {
  627. get { return data; }
  628. }
  629. protected LimitCommon(float contactDist = -1.0f)
  630. {
  631. data.contactDist = contactDist;
  632. data.restitution = 0.0f;
  633. data.spring = new Spring();
  634. }
  635. protected LimitCommon(Spring spring, float restitution = 0.0f)
  636. {
  637. data.contactDist = -1.0f;
  638. data.restitution = restitution;
  639. data.spring = spring;
  640. }
  641. protected LimitCommon(LimitCommonData data)
  642. {
  643. this.data = data;
  644. }
  645. /// <inheritdoc/>
  646. public override bool Equals(object rhs)
  647. {
  648. if (rhs is LimitCommon)
  649. {
  650. LimitCommon other = (LimitCommon)rhs;
  651. return ContactDist == other.ContactDist && Restitution == other.Restitution && Spring == other.Spring;
  652. }
  653. return false;
  654. }
  655. /// <inheritdoc/>
  656. public override int GetHashCode()
  657. {
  658. return base.GetHashCode();
  659. }
  660. public static bool operator ==(LimitCommon a, LimitCommon b)
  661. {
  662. return a.Equals(b);
  663. }
  664. public static bool operator !=(LimitCommon a, LimitCommon b)
  665. {
  666. return !(a == b);
  667. }
  668. }
  669. /// <summary>
  670. /// Represents a joint limit between two distance values. Lower value must be less than the upper value.
  671. /// </summary>
  672. [SerializeObject]
  673. public class LimitLinearRange : LimitCommon
  674. {
  675. private LimitLinearRangeData data;
  676. /// <summary>
  677. /// Lower distance of the limit. Must be less than <see cref="Upper"/>.
  678. /// </summary>
  679. public float Lower { get { return data.lower; } }
  680. /// <summary>
  681. /// Upper distance of the limit. Must be greater than <see cref="Lower"/>.
  682. /// </summary>
  683. public float Upper { get { return data.upper; } }
  684. /// <summary>
  685. /// Gets properties of the linear limit range.
  686. /// </summary>
  687. public LimitLinearRangeData Data
  688. {
  689. get { return data; }
  690. }
  691. /// <summary>
  692. /// Constructs an empty limit.
  693. /// </summary>
  694. public LimitLinearRange()
  695. { }
  696. /// <summary>
  697. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  698. /// </summary>
  699. /// <param name="lower"><see cref="Lower"/></param>
  700. /// <param name="upper"><see cref="Upper"/></param>
  701. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  702. public LimitLinearRange(float lower, float upper, float contactDist = -1.0f)
  703. :base(contactDist)
  704. {
  705. data.lower = lower;
  706. data.upper = upper;
  707. }
  708. /// <summary>
  709. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  710. /// parameter and will be pulled back towards the limit by the provided spring.
  711. /// </summary>
  712. /// <param name="lower"><see cref="Lower"/></param>
  713. /// <param name="upper"><see cref="Upper"/></param>
  714. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  715. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  716. public LimitLinearRange(float lower, float upper, Spring spring, float restitution = 0.0f)
  717. :base(spring, restitution)
  718. {
  719. data.lower = lower;
  720. data.upper = upper;
  721. }
  722. /// <summary>
  723. /// Constructs a new limit from the provided properties.
  724. /// </summary>
  725. /// <param name="limitData">Linear range specific properties.</param>
  726. /// <param name="commonData">Properties common to all limit types.</param>
  727. public LimitLinearRange(LimitLinearRangeData limitData, LimitCommonData commonData)
  728. :base(commonData)
  729. {
  730. this.data = limitData;
  731. }
  732. /// <inheritdoc/>
  733. public override bool Equals(object rhs)
  734. {
  735. if (rhs is LimitLinearRange)
  736. {
  737. LimitLinearRange other = (LimitLinearRange)rhs;
  738. return base.Equals(rhs) && Lower == other.Lower && Upper == other.Upper;
  739. }
  740. return false;
  741. }
  742. /// <inheritdoc/>
  743. public override int GetHashCode()
  744. {
  745. return base.GetHashCode();
  746. }
  747. public static bool operator ==(LimitLinearRange a, LimitLinearRange b)
  748. {
  749. return a.Equals(b);
  750. }
  751. public static bool operator !=(LimitLinearRange a, LimitLinearRange b)
  752. {
  753. return !(a == b);
  754. }
  755. /// <summary>
  756. /// Used for accessing limit data from native code.
  757. /// </summary>
  758. /// <param name="output">Native readable limit structure.</param>
  759. private void Internal_GetNative(ref ScriptLimitLinearRange output)
  760. {
  761. output.contactDist = ContactDist;
  762. output.restitution = Restitution;
  763. output.spring = Spring;
  764. output.lower = Lower;
  765. output.upper = Upper;
  766. }
  767. }
  768. /// <summary>
  769. /// Represents a joint limit between zero a single distance value.
  770. /// </summary>
  771. [SerializeObject]
  772. public class LimitLinear : LimitCommon
  773. {
  774. private LimitLinearData data;
  775. /// <summary>
  776. /// Distance at which the limit becomes active.
  777. /// </summary>
  778. public float Extent { get { return data.extent; } }
  779. /// <summary>
  780. /// Gets properties of the linear limit.
  781. /// </summary>
  782. public LimitLinearData Data
  783. {
  784. get { return data; }
  785. }
  786. /// <summary>
  787. /// Constructs an empty limit.
  788. /// </summary>
  789. public LimitLinear()
  790. { }
  791. /// <summary>
  792. /// Constructs a hard limit.Once the limit is reached the movement of the attached bodies will come to a stop.
  793. /// </summary>
  794. /// <param name="extent"><see cref="Extent"/></param>
  795. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  796. public LimitLinear(float extent, float contactDist = -1.0f)
  797. :base(contactDist)
  798. {
  799. data.extent = extent;
  800. }
  801. /// <summary>
  802. /// Constructs a soft limit.Once the limit is reached the bodies will bounce back according to the resitution
  803. /// parameter and will be pulled back towards the limit by the provided spring.
  804. /// </summary>
  805. /// <param name="extent"><see cref="Extent"/></param>
  806. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  807. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  808. public LimitLinear(float extent, Spring spring, float restitution = 0.0f)
  809. :base(spring, restitution)
  810. {
  811. data.extent = extent;
  812. }
  813. /// <summary>
  814. /// Constructs a new limit from the provided properties.
  815. /// </summary>
  816. /// <param name="limitData">Linear limit specific properties.</param>
  817. /// <param name="commonData">Properties common to all limit types.</param>
  818. public LimitLinear(LimitLinearData limitData, LimitCommonData commonData)
  819. :base(commonData)
  820. {
  821. this.data = limitData;
  822. }
  823. /// <inheritdoc/>
  824. public override bool Equals(object rhs)
  825. {
  826. if (rhs is LimitLinear)
  827. {
  828. LimitLinear other = (LimitLinear)rhs;
  829. return base.Equals(rhs) && Extent == other.Extent;
  830. }
  831. return false;
  832. }
  833. /// <inheritdoc/>
  834. public override int GetHashCode()
  835. {
  836. return base.GetHashCode();
  837. }
  838. public static bool operator ==(LimitLinear a, LimitLinear b)
  839. {
  840. return a.Equals(b);
  841. }
  842. public static bool operator !=(LimitLinear a, LimitLinear b)
  843. {
  844. return !(a == b);
  845. }
  846. /// <summary>
  847. /// Used for accessing limit data from native code.
  848. /// </summary>
  849. /// <param name="output">Native readable limit structure.</param>
  850. private void Internal_GetNative(ref ScriptLimitLinear output)
  851. {
  852. output.contactDist = ContactDist;
  853. output.restitution = Restitution;
  854. output.spring = Spring;
  855. output.extent = Extent;
  856. }
  857. }
  858. /// <summary>
  859. /// Represents a joint limit between two angles.
  860. /// </summary>
  861. [SerializeObject]
  862. public class LimitAngularRange : LimitCommon
  863. {
  864. private LimitAngularRangeData data;
  865. /// <summary>
  866. /// Lower angle of the limit. Must be less than <see cref="Upper"/>.
  867. /// </summary>
  868. public Radian Lower { get { return data.lower; } }
  869. /// <summary>
  870. /// Upper angle of the limit. Must be greater than <see cref="Lower"/>.
  871. /// </summary>
  872. public Radian Upper { get { return data.upper; } }
  873. /// <summary>
  874. /// Gets properties of the angular limit range.
  875. /// </summary>
  876. public LimitAngularRangeData Data
  877. {
  878. get { return data; }
  879. }
  880. /// <summary>
  881. /// Constructs an empty limit.
  882. /// </summary>
  883. public LimitAngularRange()
  884. { }
  885. /// <summary>
  886. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  887. /// </summary>
  888. /// <param name="lower"><see cref="Lower"/></param>
  889. /// <param name="upper"><see cref="Upper"/></param>
  890. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  891. public LimitAngularRange(Radian lower, Radian upper, float contactDist = -1.0f)
  892. : base(contactDist)
  893. {
  894. data.lower = lower;
  895. data.upper = upper;
  896. }
  897. /// <summary>
  898. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  899. /// parameter and will be pulled back towards the limit by the provided spring.
  900. /// </summary>
  901. /// <param name="lower"><see cref="Lower"/></param>
  902. /// <param name="upper"><see cref="Upper"/></param>
  903. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  904. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  905. public LimitAngularRange(Radian lower, Radian upper, Spring spring, float restitution = 0.0f)
  906. : base(spring, restitution)
  907. {
  908. data.lower = lower;
  909. data.upper = upper;
  910. }
  911. /// <summary>
  912. /// Constructs a new limit from the provided properties.
  913. /// </summary>
  914. /// <param name="limitData">Angular limit range specific properties.</param>
  915. /// <param name="commonData">Properties common to all limit types.</param>
  916. public LimitAngularRange(LimitAngularRangeData limitData, LimitCommonData commonData)
  917. :base(commonData)
  918. {
  919. this.data = limitData;
  920. }
  921. /// <inheritdoc/>
  922. public override bool Equals(object rhs)
  923. {
  924. if (rhs is LimitAngularRange)
  925. {
  926. LimitAngularRange other = (LimitAngularRange)rhs;
  927. return base.Equals(rhs) && Lower == other.Lower && Upper == other.Upper;
  928. }
  929. return false;
  930. }
  931. /// <inheritdoc/>
  932. public override int GetHashCode()
  933. {
  934. return base.GetHashCode();
  935. }
  936. public static bool operator ==(LimitAngularRange a, LimitAngularRange b)
  937. {
  938. return a.Equals(b);
  939. }
  940. public static bool operator !=(LimitAngularRange a, LimitAngularRange b)
  941. {
  942. return !(a == b);
  943. }
  944. /// <summary>
  945. /// Used for accessing limit data from native code.
  946. /// </summary>
  947. /// <param name="output">Native readable limit structure.</param>
  948. private void Internal_GetNative(ref ScriptLimitAngularRange output)
  949. {
  950. output.contactDist = ContactDist;
  951. output.restitution = Restitution;
  952. output.spring = Spring;
  953. output.lower = Lower;
  954. output.upper = Upper;
  955. }
  956. }
  957. /// <summary>
  958. /// Represents a joint limit that contraints movement to within an elliptical cone.
  959. /// </summary>
  960. [SerializeObject]
  961. public class LimitConeRange : LimitCommon
  962. {
  963. private LimitConeRangeData data;
  964. /// <summary>
  965. /// Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  966. /// </summary>
  967. public Radian YLimitAngle { get { return data.yLimitAngle; } }
  968. /// <summary>
  969. /// Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  970. /// </summary>
  971. public Radian ZLimitAngle { get { return data.zLimitAngle; } }
  972. /// <summary>
  973. /// Gets properties of the cone limit range.
  974. /// </summary>
  975. public LimitConeRangeData Data
  976. {
  977. get { return data; }
  978. }
  979. /// <summary>
  980. /// Constructs a limit with a 45 degree cone.
  981. /// </summary>
  982. public LimitConeRange()
  983. {
  984. data.yLimitAngle = new Radian(MathEx.Pi * 0.5f);
  985. data.zLimitAngle = new Radian(MathEx.Pi * 0.5f);
  986. }
  987. /// <summary>
  988. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  989. /// </summary>
  990. /// <param name="yLimitAngle"><see cref="YLimitAngle"/></param>
  991. /// <param name="zLimitAngle"><see cref="ZLimitAngle"/></param>
  992. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  993. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, float contactDist = -1.0f)
  994. : base(contactDist)
  995. {
  996. data.yLimitAngle = yLimitAngle;
  997. data.zLimitAngle = zLimitAngle;
  998. }
  999. /// <summary>
  1000. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  1001. /// parameter and will be pulled back towards the limit by the provided spring.
  1002. /// </summary>
  1003. /// <param name="yLimitAngle"><see cref="YLimitAngle"/></param>
  1004. /// <param name="zLimitAngle"><see cref="ZLimitAngle"/></param>
  1005. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  1006. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  1007. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, Spring spring, float restitution = 0.0f)
  1008. : base(spring, restitution)
  1009. {
  1010. data.yLimitAngle = yLimitAngle;
  1011. data.zLimitAngle = zLimitAngle;
  1012. }
  1013. /// <summary>
  1014. /// Constructs a new limit from the provided properties.
  1015. /// </summary>
  1016. /// <param name="limitData">Cone limit range specific properties.</param>
  1017. /// <param name="commonData">Properties common to all limit types.</param>
  1018. public LimitConeRange(LimitConeRangeData limitData, LimitCommonData commonData)
  1019. :base(commonData)
  1020. {
  1021. this.data = limitData;
  1022. }
  1023. /// <inheritdoc/>
  1024. public override bool Equals(object rhs)
  1025. {
  1026. if (rhs is LimitConeRange)
  1027. {
  1028. LimitConeRange other = (LimitConeRange)rhs;
  1029. return base.Equals(rhs) && YLimitAngle == other.YLimitAngle && ZLimitAngle == other.ZLimitAngle;
  1030. }
  1031. return false;
  1032. }
  1033. /// <inheritdoc/>
  1034. public override int GetHashCode()
  1035. {
  1036. return base.GetHashCode();
  1037. }
  1038. public static bool operator ==(LimitConeRange a, LimitConeRange b)
  1039. {
  1040. return a.Equals(b);
  1041. }
  1042. public static bool operator !=(LimitConeRange a, LimitConeRange b)
  1043. {
  1044. return !(a == b);
  1045. }
  1046. /// <summary>
  1047. /// Used for accessing limit data from native code.
  1048. /// </summary>
  1049. /// <param name="output">Native readable limit structure.</param>
  1050. private void Internal_GetNative(ref ScriptLimitConeRange output)
  1051. {
  1052. output.contactDist = ContactDist;
  1053. output.restitution = Restitution;
  1054. output.spring = Spring;
  1055. output.yLimitAngle = YLimitAngle;
  1056. output.zLimitAngle = ZLimitAngle;
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// Contains data used by HingeJointDrive.
  1061. /// </summary>
  1062. [StructLayout(LayoutKind.Sequential)]
  1063. public struct HingeJointDriveData // Note: Must match C++ struct HingeJoint::Drive
  1064. {
  1065. /// <summary>
  1066. /// <see cref="HingeJointDrive.Speed"/>
  1067. /// </summary>
  1068. public float speed;
  1069. /// <summary>
  1070. /// <see cref="HingeJointDrive.ForceLimit"/>
  1071. /// </summary>
  1072. public float forceLimit;
  1073. /// <summary>
  1074. /// <see cref="HingeJointDrive.GearRatio"/>
  1075. /// </summary>
  1076. public float gearRatio;
  1077. /// <summary>
  1078. /// <see cref="HingeJointDrive.FreeSpin"/>
  1079. /// </summary>
  1080. public bool freeSpin;
  1081. }
  1082. /// <summary>
  1083. /// Contains data used by D6JointDrive.
  1084. /// </summary>
  1085. [StructLayout(LayoutKind.Sequential)]
  1086. public struct D6JointDriveData // Note: Must match C++ struct D6Joint::Drive
  1087. {
  1088. /// <summary>
  1089. /// <see cref="D6JointDrive.Stiffness"/>
  1090. /// </summary>
  1091. public float stiffness;
  1092. /// <summary>
  1093. /// <see cref="D6JointDrive.Damping"/>
  1094. /// </summary>
  1095. public float damping;
  1096. /// <summary>
  1097. /// <see cref="D6JointDrive.ForceLimit"/>
  1098. /// </summary>
  1099. public float forceLimit;
  1100. /// <summary>
  1101. /// <see cref="D6JointDrive.Acceleration"/>
  1102. /// </summary>
  1103. public bool acceleration;
  1104. }
  1105. /// <summary>
  1106. /// Contains data used by LimitCommon.
  1107. /// </summary>
  1108. public struct LimitCommonData
  1109. {
  1110. /// <summary>
  1111. /// <see cref="LimitCommon.ContactDist"/>
  1112. /// </summary>
  1113. public float contactDist;
  1114. /// <summary>
  1115. /// <see cref="LimitCommon.Restitution"/>
  1116. /// </summary>
  1117. public float restitution;
  1118. /// <summary>
  1119. /// <see cref="LimitCommon.Spring"/>
  1120. /// </summary>
  1121. public Spring spring;
  1122. }
  1123. /// <summary>
  1124. /// Contains data used by LimitLinearRange.
  1125. /// </summary>
  1126. public struct LimitLinearRangeData
  1127. {
  1128. /// <summary>
  1129. /// <see cref="LimitLinearRange.Lower"/>
  1130. /// </summary>
  1131. public float lower;
  1132. /// <summary>
  1133. /// <see cref="LimitLinearRange.Upper"/>
  1134. /// </summary>
  1135. public float upper;
  1136. }
  1137. /// <summary>
  1138. /// Contains data used by LimitLinear.
  1139. /// </summary>
  1140. public struct LimitLinearData
  1141. {
  1142. /// <summary>
  1143. /// <see cref="LimitLinearRange.Extent"/>
  1144. /// </summary>
  1145. public float extent;
  1146. }
  1147. /// <summary>
  1148. /// Contains data used by LimitAngularRange.
  1149. /// </summary>
  1150. public struct LimitAngularRangeData
  1151. {
  1152. /// <summary>
  1153. /// <see cref="LimitAngularRange.Lower"/>
  1154. /// </summary>
  1155. public Radian lower;
  1156. /// <summary>
  1157. /// <see cref="LimitAngularRange.Upper"/>
  1158. /// </summary>
  1159. public Radian upper;
  1160. }
  1161. /// <summary>
  1162. /// Contains data used by LimitConeRange.
  1163. /// </summary>
  1164. public struct LimitConeRangeData
  1165. {
  1166. /// <summary>
  1167. /// <see cref="LimitConeRange.YLimitAngle"/>
  1168. /// </summary>
  1169. public Radian yLimitAngle;
  1170. /// <summary>
  1171. /// <see cref="LimitConeRange.ZLimitAngle"/>
  1172. /// </summary>
  1173. public Radian zLimitAngle;
  1174. }
  1175. /// <summary>
  1176. /// Used for passing LimitLinearRange data between native and managed code.
  1177. /// </summary>
  1178. [StructLayout(LayoutKind.Sequential)]
  1179. internal struct ScriptLimitLinearRange // Note: Must match C++ struct LimitLinearRange
  1180. {
  1181. public float contactDist;
  1182. public float restitution;
  1183. public Spring spring;
  1184. public float lower;
  1185. public float upper;
  1186. }
  1187. /// <summary>
  1188. /// Used for passing LimitLinear data between native and managed code.
  1189. /// </summary>
  1190. [StructLayout(LayoutKind.Sequential)]
  1191. internal struct ScriptLimitLinear // Note: Must match C++ struct LimitLinear
  1192. {
  1193. public float contactDist;
  1194. public float restitution;
  1195. public Spring spring;
  1196. public float extent;
  1197. }
  1198. /// <summary>
  1199. /// Used for passing LimitAngularRange data between native and managed code.
  1200. /// </summary>
  1201. [StructLayout(LayoutKind.Sequential)]
  1202. internal struct ScriptLimitAngularRange // Note: Must match C++ struct LimitAngularRange
  1203. {
  1204. public float contactDist;
  1205. public float restitution;
  1206. public Spring spring;
  1207. public Radian lower;
  1208. public Radian upper;
  1209. }
  1210. /// <summary>
  1211. /// Used for passing LimitConeRange data between native and managed code.
  1212. /// </summary>
  1213. [StructLayout(LayoutKind.Sequential)]
  1214. internal struct ScriptLimitConeRange // Note: Must match C++ struct LimitConeRange
  1215. {
  1216. public float contactDist;
  1217. public float restitution;
  1218. public Spring spring;
  1219. public Radian yLimitAngle;
  1220. public Radian zLimitAngle;
  1221. }
  1222. }