Joint.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. /// <summary>
  301. /// Spring strength. Force proportional to the position error.
  302. /// </summary>
  303. public float stiffness;
  304. /// <summary>
  305. /// Damping strength. Force propertional to the velocity error.
  306. /// </summary>
  307. public float damping;
  308. }
  309. /// <summary>
  310. /// Specifies first or second body referenced by a Joint.
  311. /// </summary>
  312. public enum JointBody
  313. {
  314. A, B
  315. };
  316. /// <summary>
  317. /// Specifies axes that the D6 joint can constrain motion on.
  318. /// </summary>
  319. public enum D6JointAxis
  320. {
  321. /// <summary>
  322. /// Movement on the X axis.
  323. /// </summary>
  324. X,
  325. /// <summary>
  326. /// Movement on the Y axis.
  327. /// </summary>
  328. Y,
  329. /// <summary>
  330. /// Movement on the Z axis.
  331. /// </summary>
  332. Z,
  333. /// <summary>
  334. /// Rotation around the X axis.
  335. /// </summary>
  336. Twist,
  337. /// <summary>
  338. /// Rotation around the Y axis.
  339. /// </summary>
  340. SwingY,
  341. /// <summary>
  342. /// Rotation around the Z axis.
  343. /// </summary>
  344. SwingZ,
  345. Count
  346. }
  347. /// <summary>
  348. /// Specifies type of constraint placed on a specific axis of a D6 joint.
  349. /// </summary>
  350. public enum D6JointMotion
  351. {
  352. /// <summary>
  353. /// Axis is immovable.
  354. /// </summary>
  355. Locked,
  356. /// <summary>
  357. /// Axis will be constrained by the specified limits.
  358. /// </summary>
  359. Limited,
  360. /// <summary>
  361. /// Axis will not be constrained.
  362. /// </summary>
  363. Free,
  364. Count
  365. }
  366. /// <summary>
  367. /// Type of drives that can be used for moving or rotating bodies attached to the D6 joint.
  368. /// </summary>
  369. public enum DriveType
  370. {
  371. /// <summary>
  372. /// Linear movement on the X axis using the linear drive model.
  373. /// </summary>
  374. X,
  375. /// <summary>
  376. /// Linear movement on the Y axis using the linear drive model.
  377. /// </summary>
  378. Y,
  379. /// <summary>
  380. /// Linear movement on the Z axis using the linear drive model.
  381. /// </summary>
  382. Z,
  383. /// <summary>
  384. /// Rotation around the Y axis using the twist/swing angular drive model. Should not be used together with
  385. /// SLERP mode.
  386. /// </summary>
  387. Swing,
  388. /// <summary>
  389. /// Rotation around the Z axis using the twist/swing angular drive model. Should not be used together with
  390. /// SLERP mode.
  391. /// </summary>
  392. Twist,
  393. /// <summary>
  394. /// Rotation using spherical linear interpolation. Uses the SLERP angular drive mode which performs rotation
  395. /// by interpolating the quaternion values directly over the shortest path (applies to all three axes, which
  396. /// they all must be unlocked).
  397. /// </summary>
  398. SLERP,
  399. Count
  400. }
  401. /// <summary>
  402. /// Specifies parameters for a drive that will attempt to move the D6 joint bodies to the specified drive position and
  403. /// velocity.
  404. /// </summary>
  405. public class D6JointDrive
  406. {
  407. /// <summary>
  408. /// Spring strength. Force proportional to the position error.
  409. /// </summary>
  410. public float stiffness = 0.0f;
  411. /// <summary>
  412. /// Damping strength. Force propertional to the velocity error.
  413. /// </summary>
  414. public float damping = 0.0f;
  415. /// <summary>
  416. /// Maximum force the drive can apply.
  417. /// </summary>
  418. public float forceLimit = float.MaxValue;
  419. /// <summary>
  420. /// If true the drive will generate acceleration instead of forces. Acceleration drives are easier to tune as
  421. /// they account for the masses of the actors to which the joint is attached.
  422. /// </summary>
  423. public bool acceleration = false;
  424. /// <summary>
  425. /// Used for accessing drive data from native code.
  426. /// </summary>
  427. /// <param name="output">Native readable drive structure.</param>
  428. private void Internal_GetNative(ref ScriptD6JointDrive output)
  429. {
  430. output.stiffness = stiffness;
  431. output.damping = damping;
  432. output.forceLimit = forceLimit;
  433. output.acceleration = acceleration;
  434. }
  435. }
  436. /// <summary>
  437. /// Properties of a drive that drives the hinge joint's angular velocity towards a paricular value.
  438. /// </summary>
  439. public class HingeJointDrive
  440. {
  441. /// <summary>
  442. /// Target speed of the joint.
  443. /// </summary>
  444. public float speed = 0.0f;
  445. /// <summary>
  446. /// Maximum torque the drive is allowed to apply.
  447. /// </summary>
  448. public float forceLimit = float.MaxValue;
  449. /// <summary>
  450. /// Scales the velocity of the first body, and its response to drive torque is scaled down.
  451. /// </summary>
  452. public float gearRatio = 1.0f;
  453. /// <summary>
  454. /// If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  455. /// the breaking to happen set this to true.
  456. /// </summary>
  457. public bool freeSpin = false;
  458. /// <summary>
  459. /// Used for accessing drive data from native code.
  460. /// </summary>
  461. /// <param name="output">Native readable drive structure.</param>
  462. private void Internal_GetNative(ref ScriptHingeJointDrive output)
  463. {
  464. output.speed = speed;
  465. output.forceLimit = forceLimit;
  466. output.gearRatio = gearRatio;
  467. output.freeSpin = freeSpin;
  468. }
  469. };
  470. /// <summary>
  471. /// Contains common values used by all Joint limit types.
  472. /// </summary>
  473. [SerializeObject]
  474. public class LimitCommon
  475. {
  476. public LimitCommon(float contactDist = -1.0f)
  477. {
  478. this.contactDist = contactDist;
  479. this.restitution = 0.0f;
  480. this.spring = new Spring();
  481. }
  482. public LimitCommon(Spring spring, float restitution = 0.0f)
  483. {
  484. this.contactDist = -1.0f;
  485. this.restitution = restitution;
  486. this.spring = spring;
  487. }
  488. /// <summary>
  489. /// Distance from the limit at which it becomes active. Allows the solver to activate earlier than the limit is
  490. /// reached to avoid breaking the limit.
  491. /// </summary>
  492. public float contactDist;
  493. /// <summary>
  494. /// Controls how do objects react when the limit is reached, values closer to zero specify non-ellastic collision,
  495. /// while those closer to one specify more ellastic(i.e bouncy) collision.Must be in [0, 1] range.
  496. /// </summary>
  497. public float restitution;
  498. /// <summary>
  499. /// Spring that controls how are the bodies pulled back towards the limit when they breach it.
  500. /// </summary>
  501. public Spring spring;
  502. }
  503. /// <summary>
  504. /// Represents a joint limit between two distance values. Lower value must be less than the upper value.
  505. /// </summary>
  506. [SerializeObject]
  507. public class LimitLinearRange : LimitCommon
  508. {
  509. /// <summary>
  510. /// Constructs an empty limit.
  511. /// </summary>
  512. public LimitLinearRange()
  513. { }
  514. /// <summary>
  515. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  516. /// </summary>
  517. /// <param name="lower">Lower distance of the limit.Must be less than <paramref name="upper"/>.</param>
  518. /// <param name="upper">Upper distance of the limit.Must be more than <paramref name="lower"/>.</param>
  519. /// <param name="contactDist">Distance from the limit at which it becomes active.Allows the solver to activate
  520. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  521. /// default.</param>
  522. public LimitLinearRange(float lower, float upper, float contactDist = -1.0f)
  523. :base(contactDist)
  524. {
  525. this.lower = lower;
  526. this.upper = upper;
  527. }
  528. /// <summary>
  529. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  530. /// parameter and will be pulled back towards the limit by the provided spring.
  531. /// </summary>
  532. /// <param name="lower">Lower distance of the limit. Must be less than <paramref name="upper"/>.</param>
  533. /// <param name="upper">Upper distance of the limit. Must be more than <paramref name="lower"/>.</param>
  534. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  535. /// </param>
  536. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  537. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  538. /// collision.Must be in [0, 1] range.</param>
  539. public LimitLinearRange(float lower, float upper, Spring spring, float restitution = 0.0f)
  540. :base(spring, restitution)
  541. {
  542. this.lower = lower;
  543. this.upper = upper;
  544. }
  545. /// <summary>
  546. /// Lower distance of the limit. Must be less than #upper.
  547. /// </summary>
  548. public float lower;
  549. /// <summary>
  550. /// Upper distance of the limit. Must be more than #lower.
  551. /// </summary>
  552. public float upper;
  553. /// <summary>
  554. /// Used for accessing limit data from native code.
  555. /// </summary>
  556. /// <param name="output">Native readable limit structure.</param>
  557. private void Internal_GetNative(ref ScriptLimitLinearRange output)
  558. {
  559. output.contactDist = contactDist;
  560. output.resitution = restitution;
  561. output.spring = spring;
  562. output.lower = lower;
  563. output.upper = upper;
  564. }
  565. }
  566. /// <summary>
  567. /// Represents a joint limit between zero a single distance value.
  568. /// </summary>
  569. [SerializeObject]
  570. public class LimitLinear : LimitCommon
  571. {
  572. /// <summary>
  573. /// Constructs an empty limit.
  574. /// </summary>
  575. public LimitLinear()
  576. { }
  577. /// <summary>
  578. /// Constructs a hard limit.Once the limit is reached the movement of the attached bodies will come to a stop.
  579. /// </summary>
  580. /// <param name="extent">Distance at which the limit becomes active.</param>
  581. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  582. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  583. /// default.</param>
  584. public LimitLinear(float extent, float contactDist = -1.0f)
  585. :base(contactDist)
  586. {
  587. this.extent = extent;
  588. }
  589. /// <summary>
  590. /// Constructs a soft limit.Once the limit is reached the bodies will bounce back according to the resitution
  591. /// parameter and will be pulled back towards the limit by the provided spring.
  592. /// </summary>
  593. /// <param name="extent">Distance at which the limit becomes active. </param>
  594. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  595. /// </param>
  596. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  597. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  598. /// collision.Must be in [0, 1] range.</param>
  599. public LimitLinear(float extent, Spring spring, float restitution = 0.0f)
  600. :base(spring, restitution)
  601. {
  602. this.extent = extent;
  603. }
  604. /// <summary>
  605. /// Distance at which the limit becomes active.
  606. /// </summary>
  607. public float extent = 0.0f;
  608. /// <summary>
  609. /// Used for accessing limit data from native code.
  610. /// </summary>
  611. /// <param name="output">Native readable limit structure.</param>
  612. private void Internal_GetNative(ref ScriptLimitLinear output)
  613. {
  614. output.contactDist = contactDist;
  615. output.resitution = restitution;
  616. output.spring = spring;
  617. output.extent = extent;
  618. }
  619. }
  620. /// <summary>
  621. /// Represents a joint limit between two angles.
  622. /// </summary>
  623. [SerializeObject]
  624. public class LimitAngularRange : LimitCommon
  625. {
  626. /// <summary>
  627. /// Constructs an empty limit.
  628. /// </summary>
  629. public LimitAngularRange()
  630. { }
  631. /// <summary>
  632. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  633. /// </summary>
  634. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  635. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  636. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  637. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  638. /// default.</param>
  639. public LimitAngularRange(Radian lower, Radian upper, float contactDist = -1.0f)
  640. : base(contactDist)
  641. {
  642. this.lower = lower;
  643. this.upper = upper;
  644. }
  645. /// <summary>
  646. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  647. /// parameter and will be pulled back towards the limit by the provided spring.
  648. /// </summary>
  649. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  650. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  651. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  652. /// </param>
  653. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  654. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  655. /// collision.Must be in [0, 1] range.</param>
  656. public LimitAngularRange(Radian lower, Radian upper, Spring spring, float restitution = 0.0f)
  657. : base(spring, restitution)
  658. {
  659. this.lower = lower;
  660. this.upper = upper;
  661. }
  662. /// <summary>
  663. /// Lower angle of the limit. Must be less than #upper.
  664. /// </summary>
  665. public Radian lower = new Radian(0.0f);
  666. /// <summary>
  667. /// Upper angle of the limit. Must be less than #lower.
  668. /// </summary>
  669. public Radian upper = new Radian(0.0f);
  670. /// <summary>
  671. /// Used for accessing limit data from native code.
  672. /// </summary>
  673. /// <param name="output">Native readable limit structure.</param>
  674. private void Internal_GetNative(ref ScriptLimitAngularRange output)
  675. {
  676. output.contactDist = contactDist;
  677. output.resitution = restitution;
  678. output.spring = spring;
  679. output.lower = lower;
  680. output.upper = upper;
  681. }
  682. }
  683. /// <summary>
  684. /// Represents a joint limit that contraints movement to within an elliptical cone.
  685. /// </summary>
  686. [SerializeObject]
  687. public class LimitConeRange : LimitCommon
  688. {
  689. /// <summary>
  690. /// Constructs a limit with a 45 degree cone.
  691. /// </summary>
  692. public LimitConeRange()
  693. { }
  694. /// <summary>
  695. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  696. /// </summary>
  697. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  698. /// </param>
  699. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  700. /// </param>
  701. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  702. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  703. /// default.</param>
  704. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, float contactDist = -1.0f)
  705. : base(contactDist)
  706. {
  707. this.yLimitAngle = yLimitAngle;
  708. this.zLimitAngle = zLimitAngle;
  709. }
  710. /// <summary>
  711. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  712. /// parameter and will be pulled back towards the limit by the provided spring.
  713. /// </summary>
  714. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  715. /// </param>
  716. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  717. /// </param>
  718. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  719. /// </param>
  720. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  721. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  722. /// collision.Must be in [0, 1] range.</param>
  723. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, Spring spring, float restitution = 0.0f)
  724. : base(spring, restitution)
  725. {
  726. this.yLimitAngle = yLimitAngle;
  727. this.zLimitAngle = zLimitAngle;
  728. }
  729. /// <summary>
  730. /// Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  731. /// </summary>
  732. public Radian yLimitAngle = new Radian(MathEx.Pi * 0.5f);
  733. /// <summary>
  734. /// Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  735. /// </summary>
  736. public Radian zLimitAngle = new Radian(MathEx.Pi * 0.5f);
  737. /// <summary>
  738. /// Used for accessing limit data from native code.
  739. /// </summary>
  740. /// <param name="output">Native readable limit structure.</param>
  741. private void Internal_GetNative(ref ScriptLimitConeRange output)
  742. {
  743. output.contactDist = contactDist;
  744. output.resitution = restitution;
  745. output.spring = spring;
  746. output.yLimitAngle = yLimitAngle;
  747. output.zLimitAngle = zLimitAngle;
  748. }
  749. }
  750. /// <summary>
  751. /// Used for passing HingeJointDrive data between native and managed code.
  752. /// </summary>
  753. [StructLayout(LayoutKind.Sequential)]
  754. internal struct ScriptHingeJointDrive // Note: Must match C++ struct HingeJoint::Drive
  755. {
  756. public float speed;
  757. public float forceLimit;
  758. public float gearRatio;
  759. public bool freeSpin;
  760. }
  761. /// <summary>
  762. /// Used for passing D6JointDrive data between native and managed code.
  763. /// </summary>
  764. [StructLayout(LayoutKind.Sequential)]
  765. internal struct ScriptD6JointDrive // Note: Must match C++ struct D6Joint::Drive
  766. {
  767. public float stiffness;
  768. public float damping;
  769. public float forceLimit;
  770. public bool acceleration;
  771. }
  772. /// <summary>
  773. /// Used for passing LimitLinearRange data between native and managed code.
  774. /// </summary>
  775. [StructLayout(LayoutKind.Sequential)]
  776. internal struct ScriptLimitLinearRange // Note: Must match C++ struct LimitLinearRange
  777. {
  778. public float contactDist;
  779. public float resitution;
  780. public Spring spring;
  781. public float lower;
  782. public float upper;
  783. }
  784. /// <summary>
  785. /// Used for passing LimitLinear data between native and managed code.
  786. /// </summary>
  787. [StructLayout(LayoutKind.Sequential)]
  788. internal struct ScriptLimitLinear // Note: Must match C++ struct LimitLinear
  789. {
  790. public float contactDist;
  791. public float resitution;
  792. public Spring spring;
  793. public float extent;
  794. }
  795. /// <summary>
  796. /// Used for passing LimitAngularRange data between native and managed code.
  797. /// </summary>
  798. [StructLayout(LayoutKind.Sequential)]
  799. internal struct ScriptLimitAngularRange // Note: Must match C++ struct LimitAngularRange
  800. {
  801. public float contactDist;
  802. public float resitution;
  803. public Spring spring;
  804. public Radian lower;
  805. public Radian upper;
  806. }
  807. /// <summary>
  808. /// Used for passing LimitConeRange data between native and managed code.
  809. /// </summary>
  810. [StructLayout(LayoutKind.Sequential)]
  811. internal struct ScriptLimitConeRange // Note: Must match C++ struct LimitConeRange
  812. {
  813. public float contactDist;
  814. public float resitution;
  815. public Spring spring;
  816. public Radian yLimitAngle;
  817. public Radian zLimitAngle;
  818. }
  819. }