PlaneCollider.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 plane geometry.
  10. /// </summary>
  11. public sealed class PlaneCollider : Collider
  12. {
  13. [SerializeField]
  14. private float distance;
  15. [SerializeField]
  16. private Vector3 normal = new Vector3(1.0f, 0.0f, 0.0f);
  17. /// <summary>
  18. /// Distance of the plane from the local origin, along its normal vector.
  19. /// </summary>
  20. public float Distance
  21. {
  22. get { return distance; }
  23. set
  24. {
  25. if (distance == value)
  26. return;
  27. distance = value;
  28. serializableData.localPosition = normal * distance;
  29. if (Native != null)
  30. UpdateTransform();
  31. }
  32. }
  33. /// <summary>
  34. /// Normal vector of the plane. It determines how is the plane oriented.
  35. /// </summary>
  36. public Vector3 Normal
  37. {
  38. get { return normal; }
  39. set
  40. {
  41. if (normal == value)
  42. return;
  43. normal = value.Normalized;
  44. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  45. if (Native != null)
  46. UpdateTransform();
  47. }
  48. }
  49. /// <inheritdoc/>
  50. protected internal override bool IsValidParent(Rigidbody parent)
  51. {
  52. // Planes cannot be added to non-kinematic rigidbodies
  53. return parent.Kinematic;
  54. }
  55. /// <summary>
  56. /// Returns the native plane collider wrapped by this component.
  57. /// </summary>
  58. private NativePlaneCollider Native
  59. {
  60. get { return (NativePlaneCollider)native; }
  61. }
  62. public PlaneCollider()
  63. {
  64. serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
  65. }
  66. /// <inheritdoc/>
  67. internal override NativeCollider CreateCollider()
  68. {
  69. return new NativePlaneCollider();
  70. }
  71. }
  72. /** @} */
  73. }