CapsuleCollider.cs 3.7 KB

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