CapsuleCollider.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (radius == value)
  25. return;
  26. radius = value;
  27. if (Native != null)
  28. {
  29. Native.Radius = value;
  30. if (parent != null)
  31. parent.UpdateMassDistribution();
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// Half height of the capsule, from the origin to one of the hemispherical centers, along the normal vector.
  37. /// </summary>
  38. public float HalfHeight
  39. {
  40. get { return halfHeight; }
  41. set
  42. {
  43. if (halfHeight == value)
  44. return;
  45. halfHeight = value;
  46. if (Native != null)
  47. {
  48. Native.HalfHeight = value;
  49. if (parent != null)
  50. parent.UpdateMassDistribution();
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Position of the capsule shape, relative to the component's scene object.
  56. /// </summary>
  57. public Vector3 Center
  58. {
  59. get { return serializableData.localPosition; }
  60. set
  61. {
  62. if (serializableData.localPosition == value)
  63. return;
  64. serializableData.localPosition = value;
  65. if (Native != null)
  66. UpdateTransform();
  67. }
  68. }
  69. /// <summary>
  70. /// Normal vector of the capsule. It determines how is the capsule oriented.
  71. /// </summary>
  72. public Vector3 Normal
  73. {
  74. get { return normal; }
  75. set
  76. {
  77. if (normal == value)
  78. return;
  79. normal = value.Normalized;
  80. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  81. if (Native != null)
  82. UpdateTransform();
  83. }
  84. }
  85. /// <summary>
  86. /// Returns the native capsule collider wrapped by this component.
  87. /// </summary>
  88. private NativeCapsuleCollider Native
  89. {
  90. get { return (NativeCapsuleCollider)native; }
  91. }
  92. /// <inheritdoc/>
  93. internal override NativeCollider CreateCollider()
  94. {
  95. NativeCapsuleCollider capsuleCollider = new NativeCapsuleCollider();
  96. capsuleCollider.Radius = radius;
  97. capsuleCollider.HalfHeight = halfHeight;
  98. return capsuleCollider;
  99. }
  100. }
  101. }