Joint.cs 46 KB

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