SliderJoint.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Physics
  6. * @{
  7. */
  8. /// <summary>
  9. /// Joint that removes all but a single translational degree of freedom. Bodies are allowed to move along a single axis.
  10. /// </summary>
  11. public sealed class SliderJoint : Joint
  12. {
  13. [SerializeField]
  14. private SerializableData data = new SerializableData();
  15. /// <summary>
  16. /// Determines the limit that constrains the movement of the joint to a specific minimum and maximum distance. You
  17. /// must enable <see cref="EnableLimit"/> for this to be enforced.
  18. /// </summary>
  19. public LimitLinearRange Limit
  20. {
  21. get { return [email protected]; }
  22. set
  23. {
  24. if ([email protected] == value)
  25. return;
  26. [email protected] = value;
  27. if (Native != null)
  28. Native.Limit = value;
  29. }
  30. }
  31. /// <summary>
  32. /// Enables or disables the limit that clamps the movement of the joint.
  33. /// </summary>
  34. public bool EnableLimit
  35. {
  36. get { return [email protected]; }
  37. set
  38. {
  39. if ([email protected] == value)
  40. return;
  41. [email protected] = value;
  42. if (Native != null)
  43. Native.EnableLimit = value;
  44. }
  45. }
  46. /// <summary>
  47. /// Returns the native joint wrapped by this component.
  48. /// </summary>
  49. private NativeSliderJoint Native
  50. {
  51. get { return (NativeSliderJoint)native; }
  52. }
  53. /// <inheritdoc/>
  54. internal override NativeJoint CreateNative()
  55. {
  56. NativeSliderJoint joint = new NativeSliderJoint(commonData.@internal, data.@internal);
  57. return joint;
  58. }
  59. /// <inheritdoc/>
  60. protected override void GetLocalTransform(JointBody body, out Vector3 position, out Quaternion rotation)
  61. {
  62. position = commonData.positions[(int)body];
  63. rotation = commonData.rotations[(int)body];
  64. Rigidbody rigidbody = commonData.bodies[(int)body];
  65. if (rigidbody == null) // Get world space transform if not relative to any body
  66. {
  67. Quaternion worldRot = SceneObject.Rotation;
  68. rotation = worldRot * rotation;
  69. position = worldRot.Rotate(position) + SceneObject.Position;
  70. }
  71. else
  72. {
  73. // Use only the offset for positioning, but for rotation use both the offset and target SO rotation.
  74. // (Needed because we need to rotate the joint SO in order to orient the slider direction, so we need an
  75. // additional transform that allows us to orient the object)
  76. position = rotation.Rotate(position);
  77. rotation = (rigidbody.SceneObject.Rotation*rotation).Inverse*SceneObject.Rotation;
  78. }
  79. }
  80. /// <summary>
  81. /// Holds all data the joint component needs to persist through serialization.
  82. /// </summary>
  83. [SerializeObject]
  84. internal new class SerializableData
  85. {
  86. public ScriptSliderJointData @internal;
  87. public SerializableData()
  88. {
  89. @internal.limit = new LimitLinearRange();
  90. @internal.enableLimit = false;
  91. }
  92. }
  93. }
  94. /** @} */
  95. }