Skip to content

Commit 400cdb1

Browse files
committed
fix some bug
1 parent 760a92c commit 400cdb1

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

Assets/Scene/06-collisionDetection.unity

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ MonoBehaviour:
539539
m_Script: {fileID: 11500000, guid: e703d08da4ec2a542af732657ca30bb9, type: 3}
540540
m_Name:
541541
m_EditorClassIdentifier:
542-
radius: {x: 5.96, y: 1.92, z: 1.57}
542+
radius: {x: 5.96, y: 5.48, z: 1.57}
543543
--- !u!33 &1195460714
544544
MeshFilter:
545545
m_ObjectHideFlags: 0
@@ -636,7 +636,7 @@ Transform:
636636
m_PrefabInternal: {fileID: 0}
637637
m_GameObject: {fileID: 1699725054}
638638
m_LocalRotation: {x: -0, y: 0.7071068, z: -0, w: 0.7071068}
639-
m_LocalPosition: {x: 2.81, y: 0.5, z: -2.74}
639+
m_LocalPosition: {x: 2.71, y: 0.5, z: -2.74}
640640
m_LocalScale: {x: 4.460002, y: 1, z: 1.0000005}
641641
m_Children: []
642642
m_Father: {fileID: 2020052343}
@@ -689,7 +689,7 @@ MonoBehaviour:
689689
m_Script: {fileID: 11500000, guid: e703d08da4ec2a542af732657ca30bb9, type: 3}
690690
m_Name:
691691
m_EditorClassIdentifier:
692-
radius: {x: 0.94, y: 2.45, z: 7.4}
692+
radius: {x: 1.03, y: 2.45, z: 11.93}
693693
--- !u!33 &1699725058
694694
MeshFilter:
695695
m_ObjectHideFlags: 0
@@ -800,9 +800,6 @@ MonoBehaviour:
800800
m_Script: {fileID: 11500000, guid: 4f95a9bc6c65cef4eb0756dfaa6f1e17, type: 3}
801801
m_Name:
802802
m_EditorClassIdentifier:
803-
a: {x: 0, y: 0, z: 0}
804-
b: {x: 0, y: 0, z: 0}
805-
r: 0
806803
--- !u!1 &2020052339
807804
GameObject:
808805
m_ObjectHideFlags: 0
@@ -965,7 +962,7 @@ MonoBehaviour:
965962
m_Script: {fileID: 11500000, guid: e703d08da4ec2a542af732657ca30bb9, type: 3}
966963
m_Name:
967964
m_EditorClassIdentifier:
968-
radius: {x: 0.98, y: 2.36, z: 7.4}
965+
radius: {x: 0.98, y: 2.36, z: 12.27}
969966
--- !u!33 &2071813810
970967
MeshFilter:
971968
m_ObjectHideFlags: 0

Assets/Scripts/06-collisionDetection/Component/GameManager.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,26 @@ private void Update()
3434
{
3535
for (int j = 0; j < aabbs.Count; ++j)
3636
{
37-
if (spheres[i].HasCheckedAABB.Contains(aabbs[j])) continue;
37+
if (spheres[i].HasCheckedAABB == aabbs[j]) continue;
3838
Vector3 closestPt;
3939
if(IntersectionTest.Check_Sphere_AABB(spheres[i].sphere,aabbs[j],out closestPt))
4040
{
4141
CollisionDetection.Plane p = aabbs[j].GetClosestPlane(spheres[i].sphere.center);
4242
Vector3 noraml = p.normal;
43-
Debug.Log(noraml);
44-
//Vector3 v = aabbs[j].center - closestPt;
4543
Vector3 v = spheres[i].Rigidbody.velocity;
4644
v = Vector3.Reflect(v, noraml);
47-
spheres[i].Rigidbody.velocity = v;
48-
spheres[i].HasCheckedAABB.Add(aabbs[j]);
45+
spheres[i].Rigidbody.velocity = v * 0.9f;
46+
spheres[i].HasCheckedAABB = aabbs[j];
4947
}
5048
}
5149
for (int k = 0; k < spheres.Count; ++k)
5250
{
5351
if (k == i) continue;
54-
if (spheres[i].HasCheckedSphere.Contains(spheres[k])) continue;
52+
if (spheres[i].HasCheckedSphere == spheres[k]) continue;
5553
if (IntersectionTest.Check_Sphere_Sphere(spheres[i].sphere, spheres[k].sphere))
5654
{
5755
spheres[i].Rigidbody.velocity = -spheres[i].Rigidbody.velocity * 0.9f;
58-
spheres[i].HasCheckedSphere.Add(spheres[k]);
56+
spheres[i].HasCheckedSphere = spheres[k];
5957
}
6058
}
6159
}

Assets/Scripts/06-collisionDetection/Component/SphereComponent.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class SphereComponent : MonoBehaviour
1212
/// <summary>
1313
/// 已经检测过的AABB
1414
/// </summary>
15-
public List<AABB> HasCheckedAABB = new List<AABB>();
16-
public List<SphereComponent> HasCheckedSphere = new List<SphereComponent>();
15+
public AABB HasCheckedAABB ;
16+
public SphereComponent HasCheckedSphere ;
1717
public Rigidbody Rigidbody
1818
{
1919
get
@@ -44,12 +44,7 @@ private void OnDestroy()
4444
GameManager.Instance.spheres.Remove(this);
4545
}
4646

47-
IEnumerator removeChecked()
48-
{
49-
yield return new WaitForSeconds(0.5f);
50-
HasCheckedAABB.Clear();
51-
HasCheckedSphere.Clear();
52-
}
47+
5348
}
5449
}
5550

Assets/Scripts/Common/collisionDetection/BV.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,36 @@ public AABB(Vector3 center,Vector3 radius)
9696
this.center = center;
9797
this.radius = radius;
9898
}
99+
100+
public override bool Equals(object obj)
101+
{
102+
if (obj == null)
103+
{
104+
return false;
105+
}
106+
if ((obj.GetType().Equals(this.GetType())) == false)
107+
{
108+
return false;
109+
}
110+
111+
AABB temp = (AABB)obj;
112+
113+
return this.center.Equals(temp.center) && this.radius.Equals(temp.radius);
114+
}
99115

116+
public override int GetHashCode()
117+
{
118+
return this.center.GetHashCode() ^ this.radius.GetHashCode();
119+
}
120+
121+
public static bool operator ==(AABB leftHandSide, AABB rightHandSide)
122+
{
123+
return (leftHandSide.Equals(rightHandSide));
124+
}
125+
public static bool operator !=(AABB leftHandSide, AABB rightHandSide)
126+
{
127+
return !(leftHandSide == rightHandSide);
128+
}
100129
}
101130

102131
/// <summary>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434

3535
## 06-collisionDetection(碰撞检测)
3636

37-
已完成AABB,OBB,Sphere,Capsule包围体;(未考虑复杂多面体) ; 已实现部分图元测试 ; 非弹性碰撞
37+
已完成AABB,OBB,Sphere,Capsule包围体;(未考虑复杂多面体) ; 已实现部分图元测试 ; 已实现非弹性碰撞

0 commit comments

Comments
 (0)