Joint.cs 45 KB

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