Joint.cs 40 KB

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