Joint.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 (for example a
  9. /// door hinge). One of the bodies in the joint must always be movable (that is 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 OnEnable()
  173. {
  174. RestoreNative();
  175. }
  176. private void OnDisable()
  177. {
  178. DestroyNative();
  179. }
  180. private void OnDestroy()
  181. {
  182. if (serializableData.bodies[0] != null)
  183. serializableData.bodies[0].SetJoint(null);
  184. if (serializableData.bodies[1] != null)
  185. serializableData.bodies[1].SetJoint(null);
  186. DestroyNative();
  187. }
  188. private void OnTransformChanged(TransformChangedFlags flags)
  189. {
  190. if (!SceneObject.Active)
  191. return;
  192. // We're ignoring this during physics update because it would cause problems if the joint itself was moved by physics
  193. // Note: This isn't particularily correct because if the joint is being moved by physics but the rigidbodies
  194. // themselves are not parented to the joint, the transform will need updating. However I'm leaving it up to the
  195. // user to ensure rigidbodies are always parented to the joint in such a case (It's an unlikely situation that
  196. // I can't think of an use for - joint transform will almost always be set as an initialization step and not a
  197. // physics response).
  198. if (Physics.IsUpdateInProgress)
  199. return;
  200. UpdateTransform(JointBody.A);
  201. UpdateTransform(JointBody.B);
  202. }
  203. /// <summary>
  204. /// Creates the internal representation of the Joint and restores the values saved by the Component.
  205. /// </summary>
  206. private void RestoreNative()
  207. {
  208. native = CreateNative();
  209. // Note: Merge into one call to avoid many virtual function calls
  210. Rigidbody[] bodies = new Rigidbody[2];
  211. if (serializableData.bodies[0] != null)
  212. bodies[0] = serializableData.bodies[0];
  213. else
  214. bodies[0] = null;
  215. if (serializableData.bodies[1] != null)
  216. bodies[1] = serializableData.bodies[1];
  217. else
  218. bodies[1] = null;
  219. native.SetRigidbody(JointBody.A, bodies[0]);
  220. native.SetRigidbody(JointBody.B, bodies[1]);
  221. native.BreakForce = serializableData.breakForce;
  222. native.BreakTorque = serializableData.breakTorque;
  223. native.EnableCollision = serializableData.enableCollision;
  224. native.BreakTorque = serializableData.breakTorque;
  225. native.EnableCollision = serializableData.enableCollision;
  226. UpdateTransform(JointBody.A);
  227. UpdateTransform(JointBody.B);
  228. }
  229. /// <summary>
  230. /// Destroys the internal joint representation.
  231. /// </summary>
  232. private void DestroyNative()
  233. {
  234. if (native != null)
  235. {
  236. native.Destroy();
  237. native = null;
  238. }
  239. }
  240. /// <summary>
  241. /// Updates the local transform for the specified body attached to the joint.
  242. /// </summary>
  243. /// <param name="body">Body to update.</param>
  244. private void UpdateTransform(JointBody body)
  245. {
  246. Vector3 localPos;
  247. Quaternion localRot;
  248. localPos = serializableData.positions[(int)body];
  249. localRot = serializableData.rotations[(int)body];
  250. // Transform to world space of the related body
  251. Rigidbody rigidbody = serializableData.bodies[(int)body];
  252. if (rigidbody != null)
  253. {
  254. Quaternion worldRot = rigidbody.SceneObject.Rotation;
  255. localRot = worldRot * localRot;
  256. localPos = worldRot.Rotate(localPos) + rigidbody.SceneObject.Position;
  257. }
  258. // Transform to space local to the joint
  259. Quaternion invRotation = SceneObject.Rotation.Inverse;
  260. localPos = invRotation.Rotate(localPos - SceneObject.Position);
  261. localRot = invRotation * localRot;
  262. native.SetPosition(body, localPos);
  263. native.SetRotation(body, localRot);
  264. }
  265. /// <summary>
  266. /// Holds all data the joint component needs to persist through serialization.
  267. /// </summary>
  268. [SerializeObject]
  269. internal class SerializableData
  270. {
  271. public Rigidbody[] bodies = new Rigidbody[2];
  272. public Vector3[] positions = new Vector3[2];
  273. public Quaternion[] rotations = new Quaternion[2];
  274. public float breakForce = float.MaxValue;
  275. public float breakTorque = float.MaxValue;
  276. public bool enableCollision = false;
  277. }
  278. }
  279. /// <summary>
  280. /// Controls spring parameters for a physics joint limits. If a limit is soft (body bounces back due to restitution when
  281. /// the limit is reached) the spring will pull the body back towards the limit using the specified parameters.
  282. /// </summary>
  283. [StructLayout(LayoutKind.Sequential), SerializeObject]
  284. public struct Spring // Note: Must match C++ struct Spring
  285. {
  286. /// <summary>
  287. /// Constructs a spring.
  288. /// </summary>
  289. /// <param name="stiffness">Spring strength.Force proportional to the position error.</param>
  290. /// <param name="damping">Damping strength. Force propertional to the velocity error.</param>
  291. public Spring(float stiffness, float damping)
  292. {
  293. this.stiffness = stiffness;
  294. this.damping = damping;
  295. }
  296. /// <inheritdoc/>
  297. public override bool Equals(object rhs)
  298. {
  299. if (rhs is Spring)
  300. {
  301. Spring other = (Spring)rhs;
  302. return stiffness == other.stiffness && damping == other.damping;
  303. }
  304. return false;
  305. }
  306. /// <inheritdoc/>
  307. public override int GetHashCode()
  308. {
  309. return base.GetHashCode();
  310. }
  311. public static bool operator ==(Spring a, Spring b)
  312. {
  313. return a.Equals(b);
  314. }
  315. public static bool operator !=(Spring a, Spring b)
  316. {
  317. return !(a == b);
  318. }
  319. /// <summary>
  320. /// Spring strength. Force proportional to the position error.
  321. /// </summary>
  322. public float stiffness;
  323. /// <summary>
  324. /// Damping strength. Force propertional to the velocity error.
  325. /// </summary>
  326. public float damping;
  327. }
  328. /// <summary>
  329. /// Specifies first or second body referenced by a Joint.
  330. /// </summary>
  331. public enum JointBody
  332. {
  333. A, B
  334. };
  335. /// <summary>
  336. /// Specifies axes that the D6 joint can constrain motion on.
  337. /// </summary>
  338. public enum D6JointAxis
  339. {
  340. /// <summary>
  341. /// Movement on the X axis.
  342. /// </summary>
  343. X,
  344. /// <summary>
  345. /// Movement on the Y axis.
  346. /// </summary>
  347. Y,
  348. /// <summary>
  349. /// Movement on the Z axis.
  350. /// </summary>
  351. Z,
  352. /// <summary>
  353. /// Rotation around the X axis.
  354. /// </summary>
  355. Twist,
  356. /// <summary>
  357. /// Rotation around the Y axis.
  358. /// </summary>
  359. SwingY,
  360. /// <summary>
  361. /// Rotation around the Z axis.
  362. /// </summary>
  363. SwingZ,
  364. Count
  365. }
  366. /// <summary>
  367. /// Specifies type of constraint placed on a specific axis of a D6 joint.
  368. /// </summary>
  369. public enum D6JointMotion
  370. {
  371. /// <summary>
  372. /// Axis is immovable.
  373. /// </summary>
  374. Locked,
  375. /// <summary>
  376. /// Axis will be constrained by the specified limits.
  377. /// </summary>
  378. Limited,
  379. /// <summary>
  380. /// Axis will not be constrained.
  381. /// </summary>
  382. Free,
  383. Count
  384. }
  385. /// <summary>
  386. /// Type of drives that can be used for moving or rotating bodies attached to the D6 joint.
  387. /// </summary>
  388. public enum D6JointDriveType
  389. {
  390. /// <summary>
  391. /// Linear movement on the X axis using the linear drive model.
  392. /// </summary>
  393. X,
  394. /// <summary>
  395. /// Linear movement on the Y axis using the linear drive model.
  396. /// </summary>
  397. Y,
  398. /// <summary>
  399. /// Linear movement on the Z axis using the linear drive model.
  400. /// </summary>
  401. Z,
  402. /// <summary>
  403. /// Rotation around the Y axis using the twist/swing angular drive model. Should not be used together with
  404. /// SLERP mode.
  405. /// </summary>
  406. Swing,
  407. /// <summary>
  408. /// Rotation around the Z axis using the twist/swing angular drive model. Should not be used together with
  409. /// SLERP mode.
  410. /// </summary>
  411. Twist,
  412. /// <summary>
  413. /// Rotation using spherical linear interpolation. Uses the SLERP angular drive mode which performs rotation
  414. /// by interpolating the quaternion values directly over the shortest path (applies to all three axes, which
  415. /// they all must be unlocked).
  416. /// </summary>
  417. SLERP,
  418. Count
  419. }
  420. /// <summary>
  421. /// Specifies parameters for a drive that will attempt to move the D6 joint bodies to the specified drive position and
  422. /// velocity.
  423. /// </summary>
  424. [SerializeObject]
  425. public class D6JointDrive
  426. {
  427. [SerializeField]
  428. private D6JointDriveData data;
  429. /// <summary>
  430. /// Spring strength. Force proportional to the position error.
  431. /// </summary>
  432. public float Stiffness { get { return data.stiffness; } }
  433. /// <summary>
  434. /// Damping strength. Force propertional to the velocity error.
  435. /// </summary>
  436. public float Damping { get { return data.damping; } }
  437. /// <summary>
  438. /// Maximum force the drive can apply.
  439. /// </summary>
  440. public float ForceLimit { get { return data.forceLimit; } }
  441. /// <summary>
  442. /// If true the drive will generate acceleration instead of forces. Acceleration drives are easier to tune as
  443. /// they account for the masses of the actors to which the joint is attached.
  444. /// </summary>
  445. public bool Acceleration { get { return data.acceleration; } }
  446. /// <summary>
  447. /// Gets drive properties.
  448. /// </summary>
  449. public D6JointDriveData Data
  450. {
  451. get { return data; }
  452. }
  453. /// <summary>
  454. /// Constructs a new D6 joint drive.
  455. /// </summary>
  456. /// <param name="stiffness"><see cref="Stiffness"/></param>
  457. /// <param name="damping"><see cref="Damping"/></param>
  458. /// <param name="forceLimit"><see cref="ForceLimit"/></param>
  459. /// <param name="acceleration"><see cref="Acceleration"/></param>
  460. public D6JointDrive(float stiffness = 0.0f, float damping = 0.0f, float forceLimit = float.MaxValue,
  461. bool acceleration = false)
  462. {
  463. data.stiffness = stiffness;
  464. data.damping = damping;
  465. data.forceLimit = forceLimit;
  466. data.acceleration = acceleration;
  467. }
  468. /// <summary>
  469. /// Constructs a new D6 joint drive.
  470. /// </summary>
  471. /// <param name="data">Properties to initialize the drive with.</param>
  472. public D6JointDrive(D6JointDriveData data)
  473. {
  474. this.data = data;
  475. }
  476. /// <inheritdoc/>
  477. public override bool Equals(object rhs)
  478. {
  479. if (rhs is D6JointDrive)
  480. {
  481. D6JointDrive other = (D6JointDrive)rhs;
  482. return Stiffness == other.Stiffness && Damping == other.Damping && ForceLimit == other.ForceLimit
  483. && Acceleration == other.Acceleration;
  484. }
  485. return false;
  486. }
  487. /// <inheritdoc/>
  488. public override int GetHashCode()
  489. {
  490. return base.GetHashCode();
  491. }
  492. public static bool operator ==(D6JointDrive a, D6JointDrive b)
  493. {
  494. return a.Equals(b);
  495. }
  496. public static bool operator !=(D6JointDrive a, D6JointDrive b)
  497. {
  498. return !(a == b);
  499. }
  500. /// <summary>
  501. /// Used for accessing drive data from native code.
  502. /// </summary>
  503. /// <returns>Native readable drive structure.</returns>
  504. private D6JointDriveData Internal_GetNative()
  505. {
  506. return data;
  507. }
  508. }
  509. /// <summary>
  510. /// Properties of a drive that drives the hinge joint's angular velocity towards a paricular value.
  511. /// </summary>
  512. [SerializeObject]
  513. public class HingeJointDrive
  514. {
  515. [SerializeField]
  516. private HingeJointDriveData data;
  517. /// <summary>
  518. /// Target speed of the joint.
  519. /// </summary>
  520. public float Speed { get { return data.speed; } }
  521. /// <summary>
  522. /// Maximum torque the drive is allowed to apply.
  523. /// </summary>
  524. public float ForceLimit { get { return data.forceLimit; } }
  525. /// <summary>
  526. /// Scales the velocity of the first body, and its response to drive torque is scaled down.
  527. /// </summary>
  528. public float GearRatio { get { return data.gearRatio; } }
  529. /// <summary>
  530. /// If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  531. /// the breaking to happen set this to true.
  532. /// </summary>
  533. public bool FreeSpin { get { return data.freeSpin; } }
  534. /// <summary>
  535. /// Gets drive properties.
  536. /// </summary>
  537. public HingeJointDriveData Data
  538. {
  539. get { return data; }
  540. }
  541. /// <summary>
  542. /// Constructs a new hinge joint drive.
  543. /// </summary>
  544. /// <param name="speed"><see cref="Speed"/></param>
  545. /// <param name="forceLimit"><see cref="ForceLimit"/></param>
  546. /// <param name="gearRatio"><see cref="GearRatio"/></param>
  547. /// <param name="freeSpin"><see cref="FreeSpin"/></param>
  548. public HingeJointDrive(float speed = 0.0f, float forceLimit = float.MaxValue,
  549. float gearRatio = 1.0f, bool freeSpin = false)
  550. {
  551. data.speed = speed;
  552. data.forceLimit = forceLimit;
  553. data.gearRatio = gearRatio;
  554. data.freeSpin = freeSpin;
  555. }
  556. /// <summary>
  557. /// Constructs a new hinge joint drive.
  558. /// </summary>
  559. /// <param name="data">Properties to initialize the drive with.</param>
  560. public HingeJointDrive(HingeJointDriveData data)
  561. {
  562. this.data = data;
  563. }
  564. /// <inheritdoc/>
  565. public override bool Equals(object rhs)
  566. {
  567. if (rhs is HingeJointDrive)
  568. {
  569. HingeJointDrive other = (HingeJointDrive)rhs;
  570. return data.speed == other.data.speed && data.gearRatio == other.data.gearRatio &&
  571. data.forceLimit == other.data.forceLimit && data.freeSpin == other.data.freeSpin;
  572. }
  573. return false;
  574. }
  575. /// <inheritdoc/>
  576. public override int GetHashCode()
  577. {
  578. return base.GetHashCode();
  579. }
  580. public static bool operator ==(HingeJointDrive a, HingeJointDrive b)
  581. {
  582. return a.Equals(b);
  583. }
  584. public static bool operator !=(HingeJointDrive a, HingeJointDrive b)
  585. {
  586. return !(a == b);
  587. }
  588. /// <summary>
  589. /// Used for accessing drive data from native code.
  590. /// </summary>
  591. /// <returns>Native readable drive structure.</returns>
  592. private HingeJointDriveData Internal_GetNative()
  593. {
  594. return data;
  595. }
  596. };
  597. /// <summary>
  598. /// Contains common values used by all Joint limit types.
  599. /// </summary>
  600. [SerializeObject]
  601. public class LimitCommon
  602. {
  603. private LimitCommonData data;
  604. /// <summary>
  605. /// Distance from the limit at which it becomes active. Allows the solver to activate earlier than the limit is
  606. /// reached to avoid breaking the limit.
  607. /// </summary>
  608. public float ContactDist { get { return data.contactDist; } }
  609. /// <summary>
  610. /// Controls how do objects react when the limit is reached, values closer to zero specify non-ellastic collision,
  611. /// while those closer to one specify more ellastic(i.e bouncy) collision.Must be in [0, 1] range.
  612. /// </summary>
  613. public float Restitution { get { return data.restitution; } }
  614. /// <summary>
  615. /// Spring that controls how are the bodies pulled back towards the limit when they breach it.
  616. /// </summary>
  617. public Spring Spring { get { return data.spring; } }
  618. /// <summary>
  619. /// Gets properties common to all limit types.
  620. /// </summary>
  621. public LimitCommonData CommonData
  622. {
  623. get { return data; }
  624. }
  625. protected LimitCommon(float contactDist = -1.0f)
  626. {
  627. data.contactDist = contactDist;
  628. data.restitution = 0.0f;
  629. data.spring = new Spring();
  630. }
  631. protected LimitCommon(Spring spring, float restitution = 0.0f)
  632. {
  633. data.contactDist = -1.0f;
  634. data.restitution = restitution;
  635. data.spring = spring;
  636. }
  637. protected LimitCommon(LimitCommonData data)
  638. {
  639. this.data = data;
  640. }
  641. /// <inheritdoc/>
  642. public override bool Equals(object rhs)
  643. {
  644. if (rhs is LimitCommon)
  645. {
  646. LimitCommon other = (LimitCommon)rhs;
  647. return ContactDist == other.ContactDist && Restitution == other.Restitution && Spring == other.Spring;
  648. }
  649. return false;
  650. }
  651. /// <inheritdoc/>
  652. public override int GetHashCode()
  653. {
  654. return base.GetHashCode();
  655. }
  656. public static bool operator ==(LimitCommon a, LimitCommon b)
  657. {
  658. return a.Equals(b);
  659. }
  660. public static bool operator !=(LimitCommon a, LimitCommon b)
  661. {
  662. return !(a == b);
  663. }
  664. }
  665. /// <summary>
  666. /// Represents a joint limit between two distance values. Lower value must be less than the upper value.
  667. /// </summary>
  668. [SerializeObject]
  669. public class LimitLinearRange : LimitCommon
  670. {
  671. private LimitLinearRangeData data;
  672. /// <summary>
  673. /// Lower distance of the limit. Must be less than <see cref="Upper"/>.
  674. /// </summary>
  675. public float Lower { get { return data.lower; } }
  676. /// <summary>
  677. /// Upper distance of the limit. Must be greater than <see cref="Lower"/>.
  678. /// </summary>
  679. public float Upper { get { return data.upper; } }
  680. /// <summary>
  681. /// Gets properties of the linear limit range.
  682. /// </summary>
  683. public LimitLinearRangeData Data
  684. {
  685. get { return data; }
  686. }
  687. /// <summary>
  688. /// Constructs an empty limit.
  689. /// </summary>
  690. public LimitLinearRange()
  691. { }
  692. /// <summary>
  693. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  694. /// </summary>
  695. /// <param name="lower"><see cref="Lower"/></param>
  696. /// <param name="upper"><see cref="Upper"/></param>
  697. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  698. public LimitLinearRange(float lower, float upper, float contactDist = -1.0f)
  699. :base(contactDist)
  700. {
  701. data.lower = lower;
  702. data.upper = upper;
  703. }
  704. /// <summary>
  705. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  706. /// parameter and will be pulled back towards the limit by the provided spring.
  707. /// </summary>
  708. /// <param name="lower"><see cref="Lower"/></param>
  709. /// <param name="upper"><see cref="Upper"/></param>
  710. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  711. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  712. public LimitLinearRange(float lower, float upper, Spring spring, float restitution = 0.0f)
  713. :base(spring, restitution)
  714. {
  715. data.lower = lower;
  716. data.upper = upper;
  717. }
  718. /// <summary>
  719. /// Constructs a new limit from the provided properties.
  720. /// </summary>
  721. /// <param name="limitData">Linear range specific properties.</param>
  722. /// <param name="commonData">Properties common to all limit types.</param>
  723. public LimitLinearRange(LimitLinearRangeData limitData, LimitCommonData commonData)
  724. :base(commonData)
  725. {
  726. this.data = limitData;
  727. }
  728. /// <inheritdoc/>
  729. public override bool Equals(object rhs)
  730. {
  731. if (rhs is LimitLinearRange)
  732. {
  733. LimitLinearRange other = (LimitLinearRange)rhs;
  734. return base.Equals(rhs) && Lower == other.Lower && Upper == other.Upper;
  735. }
  736. return false;
  737. }
  738. /// <inheritdoc/>
  739. public override int GetHashCode()
  740. {
  741. return base.GetHashCode();
  742. }
  743. public static bool operator ==(LimitLinearRange a, LimitLinearRange b)
  744. {
  745. return a.Equals(b);
  746. }
  747. public static bool operator !=(LimitLinearRange a, LimitLinearRange b)
  748. {
  749. return !(a == b);
  750. }
  751. /// <summary>
  752. /// Used for accessing limit data from native code.
  753. /// </summary>
  754. /// <returns>Native readable limit structure.</returns>
  755. private ScriptLimitLinearRange Internal_GetNative()
  756. {
  757. ScriptLimitLinearRange output;
  758. output.contactDist = ContactDist;
  759. output.restitution = Restitution;
  760. output.spring = Spring;
  761. output.lower = Lower;
  762. output.upper = Upper;
  763. return output;
  764. }
  765. }
  766. /// <summary>
  767. /// Represents a joint limit between zero a single distance value.
  768. /// </summary>
  769. [SerializeObject]
  770. public class LimitLinear : LimitCommon
  771. {
  772. private LimitLinearData data;
  773. /// <summary>
  774. /// Distance at which the limit becomes active.
  775. /// </summary>
  776. public float Extent { get { return data.extent; } }
  777. /// <summary>
  778. /// Gets properties of the linear limit.
  779. /// </summary>
  780. public LimitLinearData Data
  781. {
  782. get { return data; }
  783. }
  784. /// <summary>
  785. /// Constructs an empty limit.
  786. /// </summary>
  787. public LimitLinear()
  788. { }
  789. /// <summary>
  790. /// Constructs a hard limit.Once the limit is reached the movement of the attached bodies will come to a stop.
  791. /// </summary>
  792. /// <param name="extent"><see cref="Extent"/></param>
  793. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  794. public LimitLinear(float extent, float contactDist = -1.0f)
  795. :base(contactDist)
  796. {
  797. data.extent = extent;
  798. }
  799. /// <summary>
  800. /// Constructs a soft limit.Once the limit is reached the bodies will bounce back according to the resitution
  801. /// parameter and will be pulled back towards the limit by the provided spring.
  802. /// </summary>
  803. /// <param name="extent"><see cref="Extent"/></param>
  804. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  805. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  806. public LimitLinear(float extent, Spring spring, float restitution = 0.0f)
  807. :base(spring, restitution)
  808. {
  809. data.extent = extent;
  810. }
  811. /// <summary>
  812. /// Constructs a new limit from the provided properties.
  813. /// </summary>
  814. /// <param name="limitData">Linear limit specific properties.</param>
  815. /// <param name="commonData">Properties common to all limit types.</param>
  816. public LimitLinear(LimitLinearData limitData, LimitCommonData commonData)
  817. :base(commonData)
  818. {
  819. this.data = limitData;
  820. }
  821. /// <inheritdoc/>
  822. public override bool Equals(object rhs)
  823. {
  824. if (rhs is LimitLinear)
  825. {
  826. LimitLinear other = (LimitLinear)rhs;
  827. return base.Equals(rhs) && Extent == other.Extent;
  828. }
  829. return false;
  830. }
  831. /// <inheritdoc/>
  832. public override int GetHashCode()
  833. {
  834. return base.GetHashCode();
  835. }
  836. public static bool operator ==(LimitLinear a, LimitLinear b)
  837. {
  838. return a.Equals(b);
  839. }
  840. public static bool operator !=(LimitLinear a, LimitLinear b)
  841. {
  842. return !(a == b);
  843. }
  844. /// <summary>
  845. /// Used for accessing limit data from native code.
  846. /// </summary>
  847. /// <returns>Native readable limit structure.</returns>
  848. private ScriptLimitLinear Internal_GetNative()
  849. {
  850. ScriptLimitLinear output;
  851. output.contactDist = ContactDist;
  852. output.restitution = Restitution;
  853. output.spring = Spring;
  854. output.extent = Extent;
  855. return output;
  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. /// <returns>Native readable limit structure.</returns>
  948. private ScriptLimitAngularRange Internal_GetNative()
  949. {
  950. ScriptLimitAngularRange output;
  951. output.contactDist = ContactDist;
  952. output.restitution = Restitution;
  953. output.spring = Spring;
  954. output.lower = Lower;
  955. output.upper = Upper;
  956. return output;
  957. }
  958. }
  959. /// <summary>
  960. /// Represents a joint limit that contraints movement to within an elliptical cone.
  961. /// </summary>
  962. [SerializeObject]
  963. public class LimitConeRange : LimitCommon
  964. {
  965. private LimitConeRangeData data;
  966. /// <summary>
  967. /// Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  968. /// </summary>
  969. public Radian YLimitAngle { get { return data.yLimitAngle; } }
  970. /// <summary>
  971. /// Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  972. /// </summary>
  973. public Radian ZLimitAngle { get { return data.zLimitAngle; } }
  974. /// <summary>
  975. /// Gets properties of the cone limit range.
  976. /// </summary>
  977. public LimitConeRangeData Data
  978. {
  979. get { return data; }
  980. }
  981. /// <summary>
  982. /// Constructs a limit with a 45 degree cone.
  983. /// </summary>
  984. public LimitConeRange()
  985. {
  986. data.yLimitAngle = new Radian(MathEx.Pi * 0.5f);
  987. data.zLimitAngle = new Radian(MathEx.Pi * 0.5f);
  988. }
  989. /// <summary>
  990. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  991. /// </summary>
  992. /// <param name="yLimitAngle"><see cref="YLimitAngle"/></param>
  993. /// <param name="zLimitAngle"><see cref="ZLimitAngle"/></param>
  994. /// <param name="contactDist"><see cref="LimitCommon.ContactDist"/></param>
  995. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, float contactDist = -1.0f)
  996. : base(contactDist)
  997. {
  998. data.yLimitAngle = yLimitAngle;
  999. data.zLimitAngle = zLimitAngle;
  1000. }
  1001. /// <summary>
  1002. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  1003. /// parameter and will be pulled back towards the limit by the provided spring.
  1004. /// </summary>
  1005. /// <param name="yLimitAngle"><see cref="YLimitAngle"/></param>
  1006. /// <param name="zLimitAngle"><see cref="ZLimitAngle"/></param>
  1007. /// <param name="spring"><see cref="LimitCommon.Spring"/></param>
  1008. /// <param name="restitution"><see cref="LimitCommon.Restitution"/></param>
  1009. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, Spring spring, float restitution = 0.0f)
  1010. : base(spring, restitution)
  1011. {
  1012. data.yLimitAngle = yLimitAngle;
  1013. data.zLimitAngle = zLimitAngle;
  1014. }
  1015. /// <summary>
  1016. /// Constructs a new limit from the provided properties.
  1017. /// </summary>
  1018. /// <param name="limitData">Cone limit range specific properties.</param>
  1019. /// <param name="commonData">Properties common to all limit types.</param>
  1020. public LimitConeRange(LimitConeRangeData limitData, LimitCommonData commonData)
  1021. :base(commonData)
  1022. {
  1023. this.data = limitData;
  1024. }
  1025. /// <inheritdoc/>
  1026. public override bool Equals(object rhs)
  1027. {
  1028. if (rhs is LimitConeRange)
  1029. {
  1030. LimitConeRange other = (LimitConeRange)rhs;
  1031. return base.Equals(rhs) && YLimitAngle == other.YLimitAngle && ZLimitAngle == other.ZLimitAngle;
  1032. }
  1033. return false;
  1034. }
  1035. /// <inheritdoc/>
  1036. public override int GetHashCode()
  1037. {
  1038. return base.GetHashCode();
  1039. }
  1040. public static bool operator ==(LimitConeRange a, LimitConeRange b)
  1041. {
  1042. return a.Equals(b);
  1043. }
  1044. public static bool operator !=(LimitConeRange a, LimitConeRange b)
  1045. {
  1046. return !(a == b);
  1047. }
  1048. /// <summary>
  1049. /// Used for accessing limit data from native code.
  1050. /// </summary>
  1051. /// <returns>Native readable limit structure.</returns>
  1052. private ScriptLimitConeRange Internal_GetNative()
  1053. {
  1054. ScriptLimitConeRange output;
  1055. output.contactDist = ContactDist;
  1056. output.restitution = Restitution;
  1057. output.spring = Spring;
  1058. output.yLimitAngle = YLimitAngle;
  1059. output.zLimitAngle = ZLimitAngle;
  1060. return output;
  1061. }
  1062. }
  1063. /// <summary>
  1064. /// Contains data used by HingeJointDrive.
  1065. /// </summary>
  1066. [StructLayout(LayoutKind.Sequential)]
  1067. public struct HingeJointDriveData // Note: Must match C++ struct HingeJoint::Drive
  1068. {
  1069. /// <summary>
  1070. /// <see cref="HingeJointDrive.Speed"/>
  1071. /// </summary>
  1072. public float speed;
  1073. /// <summary>
  1074. /// <see cref="HingeJointDrive.ForceLimit"/>
  1075. /// </summary>
  1076. public float forceLimit;
  1077. /// <summary>
  1078. /// <see cref="HingeJointDrive.GearRatio"/>
  1079. /// </summary>
  1080. public float gearRatio;
  1081. /// <summary>
  1082. /// <see cref="HingeJointDrive.FreeSpin"/>
  1083. /// </summary>
  1084. public bool freeSpin;
  1085. }
  1086. /// <summary>
  1087. /// Contains data used by D6JointDrive.
  1088. /// </summary>
  1089. [StructLayout(LayoutKind.Sequential)]
  1090. public struct D6JointDriveData // Note: Must match C++ struct D6Joint::Drive
  1091. {
  1092. /// <summary>
  1093. /// <see cref="D6JointDrive.Stiffness"/>
  1094. /// </summary>
  1095. public float stiffness;
  1096. /// <summary>
  1097. /// <see cref="D6JointDrive.Damping"/>
  1098. /// </summary>
  1099. public float damping;
  1100. /// <summary>
  1101. /// <see cref="D6JointDrive.ForceLimit"/>
  1102. /// </summary>
  1103. public float forceLimit;
  1104. /// <summary>
  1105. /// <see cref="D6JointDrive.Acceleration"/>
  1106. /// </summary>
  1107. public bool acceleration;
  1108. }
  1109. /// <summary>
  1110. /// Contains data used by LimitCommon.
  1111. /// </summary>
  1112. public struct LimitCommonData
  1113. {
  1114. /// <summary>
  1115. /// <see cref="LimitCommon.ContactDist"/>
  1116. /// </summary>
  1117. public float contactDist;
  1118. /// <summary>
  1119. /// <see cref="LimitCommon.Restitution"/>
  1120. /// </summary>
  1121. public float restitution;
  1122. /// <summary>
  1123. /// <see cref="LimitCommon.Spring"/>
  1124. /// </summary>
  1125. public Spring spring;
  1126. }
  1127. /// <summary>
  1128. /// Contains data used by LimitLinearRange.
  1129. /// </summary>
  1130. public struct LimitLinearRangeData
  1131. {
  1132. /// <summary>
  1133. /// <see cref="LimitLinearRange.Lower"/>
  1134. /// </summary>
  1135. public float lower;
  1136. /// <summary>
  1137. /// <see cref="LimitLinearRange.Upper"/>
  1138. /// </summary>
  1139. public float upper;
  1140. }
  1141. /// <summary>
  1142. /// Contains data used by LimitLinear.
  1143. /// </summary>
  1144. public struct LimitLinearData
  1145. {
  1146. /// <summary>
  1147. /// <see cref="LimitLinearRange.Extent"/>
  1148. /// </summary>
  1149. public float extent;
  1150. }
  1151. /// <summary>
  1152. /// Contains data used by LimitAngularRange.
  1153. /// </summary>
  1154. public struct LimitAngularRangeData
  1155. {
  1156. /// <summary>
  1157. /// <see cref="LimitAngularRange.Lower"/>
  1158. /// </summary>
  1159. public Radian lower;
  1160. /// <summary>
  1161. /// <see cref="LimitAngularRange.Upper"/>
  1162. /// </summary>
  1163. public Radian upper;
  1164. }
  1165. /// <summary>
  1166. /// Contains data used by LimitConeRange.
  1167. /// </summary>
  1168. public struct LimitConeRangeData
  1169. {
  1170. /// <summary>
  1171. /// <see cref="LimitConeRange.YLimitAngle"/>
  1172. /// </summary>
  1173. public Radian yLimitAngle;
  1174. /// <summary>
  1175. /// <see cref="LimitConeRange.ZLimitAngle"/>
  1176. /// </summary>
  1177. public Radian zLimitAngle;
  1178. }
  1179. /// <summary>
  1180. /// Used for passing LimitLinearRange data between native and managed code.
  1181. /// </summary>
  1182. [StructLayout(LayoutKind.Sequential)]
  1183. internal struct ScriptLimitLinearRange // Note: Must match C++ struct LimitLinearRange
  1184. {
  1185. public float contactDist;
  1186. public float restitution;
  1187. public Spring spring;
  1188. public float lower;
  1189. public float upper;
  1190. }
  1191. /// <summary>
  1192. /// Used for passing LimitLinear data between native and managed code.
  1193. /// </summary>
  1194. [StructLayout(LayoutKind.Sequential)]
  1195. internal struct ScriptLimitLinear // Note: Must match C++ struct LimitLinear
  1196. {
  1197. public float contactDist;
  1198. public float restitution;
  1199. public Spring spring;
  1200. public float extent;
  1201. }
  1202. /// <summary>
  1203. /// Used for passing LimitAngularRange data between native and managed code.
  1204. /// </summary>
  1205. [StructLayout(LayoutKind.Sequential)]
  1206. internal struct ScriptLimitAngularRange // Note: Must match C++ struct LimitAngularRange
  1207. {
  1208. public float contactDist;
  1209. public float restitution;
  1210. public Spring spring;
  1211. public Radian lower;
  1212. public Radian upper;
  1213. }
  1214. /// <summary>
  1215. /// Used for passing LimitConeRange data between native and managed code.
  1216. /// </summary>
  1217. [StructLayout(LayoutKind.Sequential)]
  1218. internal struct ScriptLimitConeRange // Note: Must match C++ struct LimitConeRange
  1219. {
  1220. public float contactDist;
  1221. public float restitution;
  1222. public Spring spring;
  1223. public Radian yLimitAngle;
  1224. public Radian zLimitAngle;
  1225. }
  1226. }