CapsuleCollider.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Collider with capsule geometry.
  10. /// </summary>
  11. public sealed class CapsuleCollider : Collider
  12. {
  13. [SerializeField]
  14. private float radius = 0.2f;
  15. [SerializeField]
  16. private float halfHeight = 1.0f;
  17. [SerializeField]
  18. private Vector3 normal = Vector3.YAxis;
  19. /// <summary>
  20. /// Radius of the capsule.
  21. /// </summary>
  22. public float Radius
  23. {
  24. get { return radius; }
  25. set
  26. {
  27. value = MathEx.Max(value, 0.01f);
  28. if (radius == value)
  29. return;
  30. radius = value;
  31. if (Native != null)
  32. {
  33. Native.Radius = value;
  34. if (parent != null)
  35. parent.UpdateMassDistribution();
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Half height of the capsule, from the origin to one of the hemispherical centers, along the normal vector.
  41. /// </summary>
  42. public float HalfHeight
  43. {
  44. get { return halfHeight; }
  45. set
  46. {
  47. value = MathEx.Max(value, 0.01f);
  48. if (halfHeight == value)
  49. return;
  50. halfHeight = value;
  51. if (Native != null)
  52. {
  53. Native.HalfHeight = value;
  54. if (parent != null)
  55. parent.UpdateMassDistribution();
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Position of the capsule shape, relative to the component's scene object.
  61. /// </summary>
  62. public Vector3 Center
  63. {
  64. get { return serializableData.localPosition; }
  65. set
  66. {
  67. if (serializableData.localPosition == value)
  68. return;
  69. serializableData.localPosition = value;
  70. if (Native != null)
  71. UpdateTransform();
  72. }
  73. }
  74. /// <summary>
  75. /// Normal vector of the capsule. It determines how is the capsule oriented.
  76. /// </summary>
  77. public Vector3 Normal
  78. {
  79. get { return normal; }
  80. set
  81. {
  82. if (normal == value)
  83. return;
  84. normal = value.Normalized;
  85. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  86. if (Native != null)
  87. UpdateTransform();
  88. }
  89. }
  90. public CapsuleCollider()
  91. {
  92. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  93. }
  94. /// <summary>
  95. /// Returns the native capsule collider wrapped by this component.
  96. /// </summary>
  97. private NativeCapsuleCollider Native
  98. {
  99. get { return (NativeCapsuleCollider)native; }
  100. }
  101. /// <inheritdoc/>
  102. internal override NativeCollider CreateCollider()
  103. {
  104. NativeCapsuleCollider capsuleCollider = new NativeCapsuleCollider();
  105. capsuleCollider.Radius = radius;
  106. capsuleCollider.HalfHeight = halfHeight;
  107. return capsuleCollider;
  108. }
  109. }
  110. /** @} */
  111. }