void FixedUpdate() { for (var i = 0; i < allColliders.Count; i++) { DrawBoundingVolume(allColliders[i], Color.green); } collisionInfoText.text = ""; for (int i = 0; i < colliders.Count; ++i) { CustomCollider colliderA = colliders[i]; for (int j = 0; j < immovablecolliders.Count; ++j) { CustomCollider colliderB = immovablecolliders[j]; CustomCollisionInfo collisionInfo; if (CheckCollision(colliderA, colliderB, out collisionInfo)) { Debug.Log(colliderA.gameObject + " collision with " + colliderB.gameObject); collisionInfoText.text += colliderA.gameObject.name + " collision with " + colliderB.gameObject.name + "\n"; float ax = Mathf.Abs(collisionInfo.intersectionSize.x); float ay = Mathf.Abs(collisionInfo.intersectionSize.y); float az = Mathf.Abs(collisionInfo.intersectionSize.z); float sx = colliderA.transform.position.x < colliderB.transform.position.x ? -1.0f : 1.0f; float sy = colliderA.transform.position.y < colliderB.transform.position.y ? -1.0f : 1.0f; float sz = colliderA.transform.position.z < colliderB.transform.position.z ? -1.0f : 1.0f; Vector3 firstCollisionNorm; if (ax <= ay && ax <= az) { // Debug.Log("X"); firstCollisionNorm = colliderB.U().normalized *sx; } else if (ay <= az) { // Debug.Log("Y"); firstCollisionNorm = colliderB.V().normalized *sy; } else { // Debug.Log("Z"); firstCollisionNorm = colliderB.W().normalized *sz; } if (!colliderA.GetComponent <CustomRigidbody>().disablePhysicsInteractions) { var newVel = Vector3.Reflect(colliderA.GetComponent <CustomRigidbody>().currentVelocity.normalized, firstCollisionNorm) * colliderA.GetComponent <CustomRigidbody>().currentVelocity.magnitude *colliderA.bouncieness *(1 - colliderB.frictionCoefficient); newVel = newVel.magnitude < 0.25f ? Vector3.zero : newVel; // colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.magnitude; if (collisionInfo.intersectionSize.magnitude >= 0.1f) { if (ax <= ay && ax <= az) { colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.x; } else if (ay <= az) { colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.y; } else { colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.z; } } colliderA.GetComponent <CustomRigidbody>().currentVelocity = newVel; } } } for (int j = i + 1; j < colliders.Count; ++j) { CustomCollider colliderB = colliders[j]; CustomCollisionInfo collisionInfo; if (CheckCollision(colliderA, colliderB, out collisionInfo)) { Debug.Log(colliderA.gameObject + " collision with " + colliderB.gameObject); collisionInfoText.text += colliderA.gameObject.name + " collision with " + colliderB.gameObject.name + "\n"; float ax = Mathf.Abs(collisionInfo.intersectionSize.x); float ay = Mathf.Abs(collisionInfo.intersectionSize.y); float az = Mathf.Abs(collisionInfo.intersectionSize.z); float sx = colliderA.transform.position.x < colliderB.transform.position.x ? -1.0f : 1.0f; float sy = colliderA.transform.position.y < colliderB.transform.position.y ? -1.0f : 1.0f; float sz = colliderA.transform.position.z < colliderB.transform.position.z ? -1.0f : 1.0f; Vector3 firstCollisionNorm; if (ax <= ay && ax <= az) { // Debug.Log("X"); firstCollisionNorm = colliderB.W().normalized *sx; } else if (ay <= az) { // Debug.Log("Y"); firstCollisionNorm = colliderB.V().normalized *sy; } else { // Debug.Log("Z"); firstCollisionNorm = colliderB.U().normalized *sz; } // Pulled from Year 2 sphere vs cube simulation assignment float m1, m2, x1, x2; Vector3 v1, v2, v1x, v2x, v1y, v2y, x = (colliderA.transform.position - colliderB.transform.position); var rbA = colliderA.GetComponent <CustomRigidbody>(); var rbB = colliderB.GetComponent <CustomRigidbody>(); x.Normalize(); v1 = rbA.currentVelocity; x1 = Vector3.Dot(x, v1); v1x = x * x1; v1y = v1 - v1x; m1 = rbA.mass; x = x * -1; v2 = rbB.currentVelocity; x2 = Vector3.Dot(x, v2); v2x = x * x2; v2y = v2 - v2x; m2 = rbB.mass; if (!colliderA.GetComponent <CustomRigidbody>().disablePhysicsInteractions) { rbA.currentVelocity = (v1x * (m1 - m2) / (m1 + m2) + v2x * (2 * m2) / (m1 + m2) + v1y); } if (!colliderB.GetComponent <CustomRigidbody>().disablePhysicsInteractions) { rbB.currentVelocity = (v1x * (2 * m1) / (m1 + m2) + v2x * (m2 - m1) / (m1 + m2) + v2y); } /*continue; * * if (colliderA.GetComponent<CustomRigidbody>()) * { * var directionA = colliderA.GetComponent<CustomRigidbody>().currentVelocity.normalized; * var directionB = colliderB.GetComponent<CustomRigidbody>().currentVelocity.normalized; * var newDir = (directionA - directionB).normalized; * var speed = colliderA.GetComponent<CustomRigidbody>().currentVelocity.magnitude + * colliderB.GetComponent<CustomRigidbody>().currentVelocity.magnitude; * * var newVel = * Vector3.Reflect(newDir, -firstCollisionNorm) * speed * colliderA.bouncieness * colliderB.bouncieness; * if (colliderB.GetComponent<CustomRigidbody>()) * newVel *= colliderB.GetComponent<CustomRigidbody>().currentVelocity.magnitude > 1 ? colliderB.GetComponent<CustomRigidbody>().currentVelocity.magnitude : 1; * newVel = newVel.magnitude < 0.25f ? Vector3.zero : newVel; * // colliderA.transform.position += firstCollisionNorm*collisionInfo.intersectionSize.magnitude; * if (collisionInfo.intersectionSize.magnitude >= 0.1f) * { * if (ax <= ay && ax <= az) * colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.x; * else if (ay <= az) * colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.y; * else * colliderA.transform.position += firstCollisionNorm * collisionInfo.intersectionSize.z; * } * * colliderA.GetComponent<CustomRigidbody>().currentVelocity = newVel; * } * if (colliderB.GetComponent<CustomRigidbody>()) * { * var directionA = colliderA.GetComponent<CustomRigidbody>().currentVelocity.normalized; * var directionB = colliderB.GetComponent<CustomRigidbody>().currentVelocity.normalized; * var newDir = (directionA).normalized; * var speed = colliderA.GetComponent<CustomRigidbody>().currentVelocity.magnitude + * colliderB.GetComponent<CustomRigidbody>().currentVelocity.magnitude; * * var newVel = * Vector3.Reflect(newDir, firstCollisionNorm) * speed * colliderA.bouncieness * colliderB.bouncieness; * if (colliderA.GetComponent<CustomRigidbody>()) * newVel *= colliderA.GetComponent<CustomRigidbody>().currentVelocity.magnitude > 1 ? colliderA.GetComponent<CustomRigidbody>().currentVelocity.magnitude : 1; * newVel = newVel.magnitude < 0.25f ? Vector3.zero : newVel; * // colliderB.transform.position += -firstCollisionNorm * collisionInfo.intersectionSize.magnitude; * if (collisionInfo.intersectionSize.magnitude >= 0.1f) * { * if (ax <= ay && ax <= az) * colliderB.transform.position += -firstCollisionNorm * collisionInfo.intersectionSize.x; * else if (ay <= az) * colliderB.transform.position += -firstCollisionNorm * collisionInfo.intersectionSize.y; * else * colliderB.transform.position += -firstCollisionNorm * collisionInfo.intersectionSize.z; * } * * colliderB.GetComponent<CustomRigidbody>().currentVelocity = newVel; * }*/ } } } }