NativePlaneCollider.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.IO;
  3. using kt.Utility;
  4. using Newtonsoft.Json.Serialization;
  5. using UnityEngine;
  6. namespace kt.Physics
  7. {
  8. public class NativePlaneCollider : ANativeColliderBase
  9. {
  10. public NativePlaneColliderStatus planeStatus
  11. {
  12. get
  13. {
  14. return this.PlaneStatus;
  15. }
  16. }
  17. public override NativeColliderStatus status
  18. {
  19. get
  20. {
  21. return this.PlaneStatus;
  22. }
  23. protected set
  24. {
  25. this.PlaneStatus = (value as NativePlaneColliderStatus);
  26. }
  27. }
  28. public Vector3 localDirection
  29. {
  30. get
  31. {
  32. Vector3 vector = MathUtility.GetPrimitiveVector(this.planeStatus.direction);
  33. if (this.planeStatus.isDirectionInverse)
  34. {
  35. vector = -vector;
  36. }
  37. return vector;
  38. }
  39. }
  40. public Vector3 worldDirection
  41. {
  42. get
  43. {
  44. return base.transform.TransformDirection(this.localDirection);
  45. }
  46. }
  47. public override bool Collide(ref Vector3 position, float radius)
  48. {
  49. Plane plane = new Plane(this.worldDirection, this.worldCenter);
  50. float num = plane.GetDistanceToPoint(position) - radius;
  51. if (this.status.bound == NativeColliderStatus.Bound.Outside)
  52. {
  53. if (num < 0f)
  54. {
  55. position -= this.worldDirection * num;
  56. return true;
  57. }
  58. }
  59. else if (num > 0f)
  60. {
  61. position -= this.worldDirection * num;
  62. return true;
  63. }
  64. return false;
  65. }
  66. protected override bool CollidePlane(NativePlaneCollider plane, ref Vector3 normal)
  67. {
  68. Vector3 worldCenter = plane.worldCenter;
  69. bool flag = this.Collide(ref worldCenter, 0f, NativeColliderStatus.Bound.Outside);
  70. if (flag)
  71. {
  72. normal = worldCenter - plane.worldCenter;
  73. }
  74. return flag;
  75. }
  76. protected override bool CollideCapsule(NativeCapsuleCollider capsule, ref Vector3 normal)
  77. {
  78. float hit_diff = -1f;
  79. Vector3 hit_normal = Vector3.zero;
  80. Newtonsoft.Json.Serialization.Func<Vector3, float, bool> func = delegate(Vector3 pos, float radius)
  81. {
  82. Vector3 hit_normal = Vector3.zero;
  83. Vector3 b = pos;
  84. if (this.Collide(ref pos, radius, NativeColliderStatus.Bound.Outside))
  85. {
  86. hit_normal = pos - b;
  87. if (hit_diff < 0f || hit_diff < hit_normal.sqrMagnitude)
  88. {
  89. hit_diff = hit_normal.sqrMagnitude;
  90. hit_normal = hit_normal;
  91. }
  92. return true;
  93. }
  94. return false;
  95. };
  96. bool flag = func(capsule.startPos, capsule.capsuleStatus.startRadius);
  97. bool flag2 = func(capsule.endPos, capsule.capsuleStatus.endRadius);
  98. if (flag || flag2)
  99. {
  100. normal = hit_normal;
  101. return true;
  102. }
  103. return false;
  104. }
  105. public override void Save(StreamWriter writer)
  106. {
  107. base.Save<NativePlaneColliderStatus>(writer);
  108. }
  109. public override void Load(StreamReader reader, Transform parent_search_root)
  110. {
  111. base.Load<NativePlaneColliderStatus>(reader, parent_search_root);
  112. }
  113. [SerializeField]
  114. private NativePlaneColliderStatus PlaneStatus = new NativePlaneColliderStatus();
  115. }
  116. }