Skip to content

Instantly share code, notes, and snippets.

@chuwilliamson
Last active February 3, 2024 15:21
Show Gist options
  • Save chuwilliamson/8c45bf43c58aab45c15e to your computer and use it in GitHub Desktop.
Save chuwilliamson/8c45bf43c58aab45c15e to your computer and use it in GitHub Desktop.
public bool IsColliding (GameObject a, GameObject b)
{
Vector3 minA = a.GetComponent<AABB> ().Min;
Vector3 maxA = a.GetComponent<AABB> ().Max;
Vector3 minB = b.GetComponent<AABB> ().Min;
Vector3 maxB = b.GetComponent<AABB> ().Max;
bool xColliding = false;
bool yColliding = false;
bool zColliding = false;
if (maxA.x > minB.x && minA.x < maxB.x) {
xColliding = true;
}
if (maxA.y > minB.y && minA.y < maxB.y) {
yColliding = true;
}
if (maxA.z > minB.z && minA.z < maxB.z) {
zColliding = true;
}
return (xColliding && yColliding && zColliding);
}
private void CheckCollision(GameObject collider1, GameObject collider2)
{
//CollidingAABBs = new Dictionary<GameObject, HashSet<GameObject>>();
if (collider1 != collider2)
{
if (IsColliding(collider1, collider2))
{ // Check the collision
ResolveCollision(collider1);
//collider1.GetComponent<MeshRenderer>().material.color = Color.red;
///Debug.Log ("COLLISION!!");
if (!CollidingAABBs.ContainsKey(collider1))
{ // If the dictionary does not yet contain an entry for this object
// Create a new list of collisions and add the other object to it
HashSet<GameObject> collisionList = new HashSet<GameObject>();
collisionList.Add(collider2);
// Create a new entry for this object with a list of collisions and add it to the dictionary
CollidingAABBs.Add(collider1, collisionList);
}
else
{ // Otherwise, add the other object to this object's collision list
CollidingAABBs[collider1].Add(collider2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment