NativeSphereCollider.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace kt.Physics
  5. {
  6. public class NativeSphereCollider : ANativeColliderBase
  7. {
  8. public NativeSphereColliderStatus sphereStatus
  9. {
  10. get
  11. {
  12. return this.SphereStatus;
  13. }
  14. }
  15. public override NativeColliderStatus status
  16. {
  17. get
  18. {
  19. return this.SphereStatus;
  20. }
  21. protected set
  22. {
  23. this.SphereStatus = (value as NativeSphereColliderStatus);
  24. }
  25. }
  26. public float radius
  27. {
  28. get
  29. {
  30. return this.sphereStatus.radius * base.GetLossyScale();
  31. }
  32. }
  33. public override bool Collide(ref Vector3 position, float radius)
  34. {
  35. Vector3 a = position - this.worldCenter;
  36. if (this.status.bound == NativeColliderStatus.Bound.Outside)
  37. {
  38. float num = this.radius + radius;
  39. if (a.sqrMagnitude > num * num)
  40. {
  41. return false;
  42. }
  43. float magnitude = a.magnitude;
  44. if (magnitude > 0f)
  45. {
  46. position += a / magnitude * num;
  47. }
  48. }
  49. else
  50. {
  51. float num2 = Mathf.Max(radius - radius, 0f);
  52. if (a.sqrMagnitude < num2 * num2)
  53. {
  54. return false;
  55. }
  56. float magnitude2 = a.magnitude;
  57. if (magnitude2 > 0f)
  58. {
  59. position = a / magnitude2 * num2;
  60. }
  61. }
  62. return true;
  63. }
  64. protected override bool CollideCapsule(NativeCapsuleCollider capsule, ref Vector3 normal)
  65. {
  66. bool flag = capsule.Collide(this, out normal);
  67. if (flag)
  68. {
  69. normal = -normal;
  70. }
  71. return flag;
  72. }
  73. public override void Save(StreamWriter writer)
  74. {
  75. base.Save<NativeSphereColliderStatus>(writer);
  76. }
  77. public override void Load(StreamReader reader, Transform parent_search_root)
  78. {
  79. base.Load<NativeSphereColliderStatus>(reader, parent_search_root);
  80. }
  81. [SerializeField]
  82. private NativeSphereColliderStatus SphereStatus = new NativeSphereColliderStatus();
  83. }
  84. }