Joint.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Runtime.InteropServices;
  4. namespace BansheeEngine
  5. {
  6. public class Joint : Component
  7. {
  8. // TODO
  9. }
  10. /// <summary>
  11. /// Controls spring parameters for a physics joint limits. If a limit is soft (body bounces back due to restition when
  12. /// the limit is reached) the spring will pull the body back towards the limit using the specified parameters.
  13. /// </summary>
  14. [StructLayout(LayoutKind.Sequential), SerializeObject]
  15. public struct Spring // Note: Must match C++ struct Spring
  16. {
  17. /// <summary>
  18. /// Constructs a spring.
  19. /// </summary>
  20. /// <param name="stiffness">Spring strength.Force proportional to the position error.</param>
  21. /// <param name="damping">Damping strength. Force propertional to the velocity error.</param>
  22. public Spring(float stiffness, float damping)
  23. {
  24. this.stiffness = stiffness;
  25. this.damping = damping;
  26. }
  27. /// <summary>
  28. /// Spring strength. Force proportional to the position error.
  29. /// </summary>
  30. public float stiffness;
  31. /// <summary>
  32. /// Damping strength. Force propertional to the velocity error.
  33. /// </summary>
  34. public float damping;
  35. }
  36. /// <summary>
  37. /// Specifies first or second body referenced by a Joint.
  38. /// </summary>
  39. public enum JointBody
  40. {
  41. A, B
  42. };
  43. /// <summary>
  44. /// Specifies axes that the D6 joint can constrain motion on.
  45. /// </summary>
  46. public enum D6JointAxis
  47. {
  48. /// <summary>
  49. /// Movement on the X axis.
  50. /// </summary>
  51. X,
  52. /// <summary>
  53. /// Movement on the Y axis.
  54. /// </summary>
  55. Y,
  56. /// <summary>
  57. /// Movement on the Z axis.
  58. /// </summary>
  59. Z,
  60. /// <summary>
  61. /// Rotation around the X axis.
  62. /// </summary>
  63. Twist,
  64. /// <summary>
  65. /// Rotation around the Y axis.
  66. /// </summary>
  67. SwingY,
  68. /// <summary>
  69. /// Rotation around the Z axis.
  70. /// </summary>
  71. SwingZ,
  72. Count
  73. }
  74. /// <summary>
  75. /// Specifies type of constraint placed on a specific axis of a D6 joint.
  76. /// </summary>
  77. public enum D6JointMotion
  78. {
  79. /// <summary>
  80. /// Axis is immovable.
  81. /// </summary>
  82. Locked,
  83. /// <summary>
  84. /// Axis will be constrained by the specified limits.
  85. /// </summary>
  86. Limited,
  87. /// <summary>
  88. /// Axis will not be constrained.
  89. /// </summary>
  90. Free,
  91. Count
  92. }
  93. /// <summary>
  94. /// Type of drives that can be used for moving or rotating bodies attached to the D6 joint.
  95. /// </summary>
  96. public enum DriveType
  97. {
  98. /// <summary>
  99. /// Linear movement on the X axis using the linear drive model.
  100. /// </summary>
  101. X,
  102. /// <summary>
  103. /// Linear movement on the Y axis using the linear drive model.
  104. /// </summary>
  105. Y,
  106. /// <summary>
  107. /// Linear movement on the Z axis using the linear drive model.
  108. /// </summary>
  109. Z,
  110. /// <summary>
  111. /// Rotation around the Y axis using the twist/swing angular drive model. Should not be used together with
  112. /// SLERP mode.
  113. /// </summary>
  114. Swing,
  115. /// <summary>
  116. /// Rotation around the Z axis using the twist/swing angular drive model. Should not be used together with
  117. /// SLERP mode.
  118. /// </summary>
  119. Twist,
  120. /// <summary>
  121. /// Rotation using spherical linear interpolation. Uses the SLERP angular drive mode which performs rotation
  122. /// by interpolating the quaternion values directly over the shortest path (applies to all three axes, which
  123. /// they all must be unlocked).
  124. /// </summary>
  125. SLERP,
  126. Count
  127. }
  128. /// <summary>
  129. /// Specifies parameters for a drive that will attempt to move the D6 joint bodies to the specified drive position and
  130. /// velocity.
  131. /// </summary>
  132. public class D6JointDrive
  133. {
  134. /// <summary>
  135. /// Spring strength. Force proportional to the position error.
  136. /// </summary>
  137. public float stiffness = 0.0f;
  138. /// <summary>
  139. /// Damping strength. Force propertional to the velocity error.
  140. /// </summary>
  141. public float damping = 0.0f;
  142. /// <summary>
  143. /// Maximum force the drive can apply.
  144. /// </summary>
  145. public float forceLimit = float.MaxValue;
  146. /// <summary>
  147. /// If true the drive will generate acceleration instead of forces. Acceleration drives are easier to tune as
  148. /// they account for the masses of the actors to which the joint is attached.
  149. /// </summary>
  150. public bool acceleration = false;
  151. /// <summary>
  152. /// Used for accessing drive data from native code.
  153. /// </summary>
  154. /// <param name="output">Native readable drive structure.</param>
  155. private void Internal_GetNative(ref ScriptD6JointDrive output)
  156. {
  157. output.stiffness = stiffness;
  158. output.damping = damping;
  159. output.forceLimit = forceLimit;
  160. output.acceleration = acceleration;
  161. }
  162. }
  163. /// <summary>
  164. /// Properties of a drive that drives the hinge joint's angular velocity towards a paricular value.
  165. /// </summary>
  166. public class HingeJointDrive
  167. {
  168. /// <summary>
  169. /// Target speed of the joint.
  170. /// </summary>
  171. public float speed = 0.0f;
  172. /// <summary>
  173. /// Maximum torque the drive is allowed to apply.
  174. /// </summary>
  175. public float forceLimit = float.MaxValue;
  176. /// <summary>
  177. /// Scales the velocity of the first body, and its response to drive torque is scaled down.
  178. /// </summary>
  179. public float gearRatio = 1.0f;
  180. /// <summary>
  181. /// If the joint is moving faster than the drive's target speed, the drive will try to break. If you don't want
  182. /// the breaking to happen set this to true.
  183. /// </summary>
  184. public bool freeSpin = false;
  185. /// <summary>
  186. /// Used for accessing drive data from native code.
  187. /// </summary>
  188. /// <param name="output">Native readable drive structure.</param>
  189. private void Internal_GetNative(ref ScriptHingeJointDrive output)
  190. {
  191. output.speed = speed;
  192. output.forceLimit = forceLimit;
  193. output.gearRatio = gearRatio;
  194. output.freeSpin = freeSpin;
  195. }
  196. };
  197. /// <summary>
  198. /// Contains common values used by all Joint limit types.
  199. /// </summary>
  200. [SerializeObject]
  201. public class LimitCommon
  202. {
  203. public LimitCommon(float contactDist = -1.0f)
  204. {
  205. this.contactDist = contactDist;
  206. this.restitution = 0.0f;
  207. this.spring = new Spring();
  208. }
  209. public LimitCommon(Spring spring, float restitution = 0.0f)
  210. {
  211. this.contactDist = -1.0f;
  212. this.restitution = restitution;
  213. this.spring = spring;
  214. }
  215. /// <summary>
  216. /// Distance from the limit at which it becomes active. Allows the solver to activate earlier than the limit is
  217. /// reached to avoid breaking the limit.
  218. /// </summary>
  219. public float contactDist;
  220. /// <summary>
  221. /// Controls how do objects react when the limit is reached, values closer to zero specify non-ellastic collision,
  222. /// while those closer to one specify more ellastic(i.e bouncy) collision.Must be in [0, 1] range.
  223. /// </summary>
  224. public float restitution;
  225. /// <summary>
  226. /// Spring that controls how are the bodies pulled back towards the limit when they breach it.
  227. /// </summary>
  228. public Spring spring;
  229. }
  230. /// <summary>
  231. /// Represents a joint limit between two distance values. Lower value must be less than the upper value.
  232. /// </summary>
  233. [SerializeObject]
  234. public class LimitLinearRange : LimitCommon
  235. {
  236. /// <summary>
  237. /// Constructs an empty limit.
  238. /// </summary>
  239. public LimitLinearRange()
  240. { }
  241. /// <summary>
  242. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  243. /// </summary>
  244. /// <param name="lower">Lower distance of the limit.Must be less than <paramref name="upper"/>.</param>
  245. /// <param name="upper">Upper distance of the limit.Must be more than <paramref name="lower"/>.</param>
  246. /// <param name="contactDist">Distance from the limit at which it becomes active.Allows the solver to activate
  247. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  248. /// default.</param>
  249. public LimitLinearRange(float lower, float upper, float contactDist = -1.0f)
  250. :base(contactDist)
  251. {
  252. this.lower = lower;
  253. this.upper = upper;
  254. }
  255. /// <summary>
  256. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  257. /// parameter and will be pulled back towards the limit by the provided spring.
  258. /// </summary>
  259. /// <param name="lower">Lower distance of the limit. Must be less than <paramref name="upper"/>.</param>
  260. /// <param name="upper">Upper distance of the limit. Must be more than <paramref name="lower"/>.</param>
  261. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  262. /// </param>
  263. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  264. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  265. /// collision.Must be in [0, 1] range.</param>
  266. public LimitLinearRange(float lower, float upper, Spring spring, float restitution = 0.0f)
  267. :base(spring, restitution)
  268. {
  269. this.lower = lower;
  270. this.upper = upper;
  271. }
  272. /// <summary>
  273. /// Lower distance of the limit. Must be less than #upper.
  274. /// </summary>
  275. public float lower;
  276. /// <summary>
  277. /// Upper distance of the limit. Must be more than #lower.
  278. /// </summary>
  279. public float upper;
  280. /// <summary>
  281. /// Used for accessing limit data from native code.
  282. /// </summary>
  283. /// <param name="output">Native readable limit structure.</param>
  284. private void Internal_GetNative(ref ScriptLimitLinearRange output)
  285. {
  286. output.contactDist = contactDist;
  287. output.resitution = restitution;
  288. output.spring = spring;
  289. output.lower = lower;
  290. output.upper = upper;
  291. }
  292. }
  293. /// <summary>
  294. /// Represents a joint limit between zero a single distance value.
  295. /// </summary>
  296. [SerializeObject]
  297. public class LimitLinear : LimitCommon
  298. {
  299. /// <summary>
  300. /// Constructs an empty limit.
  301. /// </summary>
  302. public LimitLinear()
  303. { }
  304. /// <summary>
  305. /// Constructs a hard limit.Once the limit is reached the movement of the attached bodies will come to a stop.
  306. /// </summary>
  307. /// <param name="extent">Distance at which the limit becomes active.</param>
  308. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  309. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  310. /// default.</param>
  311. public LimitLinear(float extent, float contactDist = -1.0f)
  312. :base(contactDist)
  313. {
  314. this.extent = extent;
  315. }
  316. /// <summary>
  317. /// Constructs a soft limit.Once the limit is reached the bodies will bounce back according to the resitution
  318. /// parameter and will be pulled back towards the limit by the provided spring.
  319. /// </summary>
  320. /// <param name="extent">Distance at which the limit becomes active. </param>
  321. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  322. /// </param>
  323. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  324. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  325. /// collision.Must be in [0, 1] range.</param>
  326. public LimitLinear(float extent, Spring spring, float restitution = 0.0f)
  327. :base(spring, restitution)
  328. {
  329. this.extent = extent;
  330. }
  331. /// <summary>
  332. /// Distance at which the limit becomes active.
  333. /// </summary>
  334. public float extent = 0.0f;
  335. /// <summary>
  336. /// Used for accessing limit data from native code.
  337. /// </summary>
  338. /// <param name="output">Native readable limit structure.</param>
  339. private void Internal_GetNative(ref ScriptLimitLinear output)
  340. {
  341. output.contactDist = contactDist;
  342. output.resitution = restitution;
  343. output.spring = spring;
  344. output.extent = extent;
  345. }
  346. }
  347. /// <summary>
  348. /// Represents a joint limit between two angles.
  349. /// </summary>
  350. [SerializeObject]
  351. public class LimitAngularRange : LimitCommon
  352. {
  353. /// <summary>
  354. /// Constructs an empty limit.
  355. /// </summary>
  356. public LimitAngularRange()
  357. { }
  358. /// <summary>
  359. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  360. /// </summary>
  361. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  362. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  363. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  364. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  365. /// default.</param>
  366. public LimitAngularRange(Radian lower, Radian upper, float contactDist = -1.0f)
  367. : base(contactDist)
  368. {
  369. this.lower = lower;
  370. this.upper = upper;
  371. }
  372. /// <summary>
  373. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  374. /// parameter and will be pulled back towards the limit by the provided spring.
  375. /// </summary>
  376. /// <param name="lower">Lower angle of the limit. Must be less than <paramref name="upper"/>.</param>
  377. /// <param name="upper">Upper angle of the limit. Must be more than <paramref name="lower"/>.</param>
  378. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  379. /// </param>
  380. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  381. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  382. /// collision.Must be in [0, 1] range.</param>
  383. public LimitAngularRange(Radian lower, Radian upper, Spring spring, float restitution = 0.0f)
  384. : base(spring, restitution)
  385. {
  386. this.lower = lower;
  387. this.upper = upper;
  388. }
  389. /// <summary>
  390. /// Lower angle of the limit. Must be less than #upper.
  391. /// </summary>
  392. public Radian lower = new Radian(0.0f);
  393. /// <summary>
  394. /// Upper angle of the limit. Must be less than #lower.
  395. /// </summary>
  396. public Radian upper = new Radian(0.0f);
  397. /// <summary>
  398. /// Used for accessing limit data from native code.
  399. /// </summary>
  400. /// <param name="output">Native readable limit structure.</param>
  401. private void Internal_GetNative(ref ScriptLimitAngularRange output)
  402. {
  403. output.contactDist = contactDist;
  404. output.resitution = restitution;
  405. output.spring = spring;
  406. output.lower = lower;
  407. output.upper = upper;
  408. }
  409. }
  410. /// <summary>
  411. /// Represents a joint limit that contraints movement to within an elliptical cone.
  412. /// </summary>
  413. [SerializeObject]
  414. public class LimitConeRange : LimitCommon
  415. {
  416. /// <summary>
  417. /// Constructs a limit with a 45 degree cone.
  418. /// </summary>
  419. public LimitConeRange()
  420. { }
  421. /// <summary>
  422. /// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
  423. /// </summary>
  424. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  425. /// </param>
  426. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  427. /// </param>
  428. /// <param name="contactDist">Distance from the limit at which it becomes active. Allows the solver to activate
  429. /// earlier than the limit is reached to avoid breaking the limit.Specify -1 for the
  430. /// default.</param>
  431. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, float contactDist = -1.0f)
  432. : base(contactDist)
  433. {
  434. this.yLimitAngle = yLimitAngle;
  435. this.zLimitAngle = zLimitAngle;
  436. }
  437. /// <summary>
  438. /// Constructs a soft limit. Once the limit is reached the bodies will bounce back according to the resitution
  439. /// parameter and will be pulled back towards the limit by the provided spring.
  440. /// </summary>
  441. /// <param name="yLimitAngle">Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  442. /// </param>
  443. /// <param name="zLimitAngle">Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  444. /// </param>
  445. /// <param name="spring">Spring that controls how are the bodies pulled back towards the limit when they breach it.
  446. /// </param>
  447. /// <param name="restitution">Controls how do objects react when the limit is reached, values closer to zero specify
  448. /// non-ellastic collision, while those closer to one specify more ellastic(i.e bouncy)
  449. /// collision.Must be in [0, 1] range.</param>
  450. public LimitConeRange(Radian yLimitAngle, Radian zLimitAngle, Spring spring, float restitution = 0.0f)
  451. : base(spring, restitution)
  452. {
  453. this.yLimitAngle = yLimitAngle;
  454. this.zLimitAngle = zLimitAngle;
  455. }
  456. /// <summary>
  457. /// Y angle of the cone. Movement is constrainted between 0 and this angle on the Y axis.
  458. /// </summary>
  459. public Radian yLimitAngle = new Radian(MathEx.Pi * 0.5f);
  460. /// <summary>
  461. /// Z angle of the cone. Movement is constrainted between 0 and this angle on the Z axis.
  462. /// </summary>
  463. public Radian zLimitAngle = new Radian(MathEx.Pi * 0.5f);
  464. /// <summary>
  465. /// Used for accessing limit data from native code.
  466. /// </summary>
  467. /// <param name="output">Native readable limit structure.</param>
  468. private void Internal_GetNative(ref ScriptLimitConeRange output)
  469. {
  470. output.contactDist = contactDist;
  471. output.resitution = restitution;
  472. output.spring = spring;
  473. output.yLimitAngle = yLimitAngle;
  474. output.zLimitAngle = zLimitAngle;
  475. }
  476. }
  477. /// <summary>
  478. /// Used for passing HingeJointDrive data between native and managed code.
  479. /// </summary>
  480. [StructLayout(LayoutKind.Sequential)]
  481. internal struct ScriptHingeJointDrive // Note: Must match C++ struct HingeJoint::Drive
  482. {
  483. public float speed;
  484. public float forceLimit;
  485. public float gearRatio;
  486. public bool freeSpin;
  487. }
  488. /// <summary>
  489. /// Used for passing D6JointDrive data between native and managed code.
  490. /// </summary>
  491. [StructLayout(LayoutKind.Sequential)]
  492. internal struct ScriptD6JointDrive // Note: Must match C++ struct D6Joint::Drive
  493. {
  494. public float stiffness;
  495. public float damping;
  496. public float forceLimit;
  497. public bool acceleration;
  498. }
  499. /// <summary>
  500. /// Used for passing LimitLinearRange data between native and managed code.
  501. /// </summary>
  502. [StructLayout(LayoutKind.Sequential)]
  503. internal struct ScriptLimitLinearRange // Note: Must match C++ struct LimitLinearRange
  504. {
  505. public float contactDist;
  506. public float resitution;
  507. public Spring spring;
  508. public float lower;
  509. public float upper;
  510. }
  511. /// <summary>
  512. /// Used for passing LimitLinear data between native and managed code.
  513. /// </summary>
  514. [StructLayout(LayoutKind.Sequential)]
  515. internal struct ScriptLimitLinear // Note: Must match C++ struct LimitLinear
  516. {
  517. public float contactDist;
  518. public float resitution;
  519. public Spring spring;
  520. public float extent;
  521. }
  522. /// <summary>
  523. /// Used for passing LimitAngularRange data between native and managed code.
  524. /// </summary>
  525. [StructLayout(LayoutKind.Sequential)]
  526. internal struct ScriptLimitAngularRange // Note: Must match C++ struct LimitAngularRange
  527. {
  528. public float contactDist;
  529. public float resitution;
  530. public Spring spring;
  531. public Radian lower;
  532. public Radian upper;
  533. }
  534. /// <summary>
  535. /// Used for passing LimitConeRange data between native and managed code.
  536. /// </summary>
  537. [StructLayout(LayoutKind.Sequential)]
  538. internal struct ScriptLimitConeRange // Note: Must match C++ struct LimitConeRange
  539. {
  540. public float contactDist;
  541. public float resitution;
  542. public Spring spring;
  543. public Radian yLimitAngle;
  544. public Radian zLimitAngle;
  545. }
  546. }