// Newton's third law of motion: for every action, there is an equal and opposite reaction public static void ResolveCollision(Rigidbody a, Rigidbody b) { // Find the direction of collision, all changes only occur in this direction Vector3 direction = a.Position - b.Position; direction.Normalize(); // Determine the relative velocity in the direction of collision // Use a dot product to get the "projection", then multiply by the direction // Note the - sign since we are working with respect to a Vector3 relativeVelocity = a.Velocity - b.Velocity; Vector3 relativeVelocityPerp = -Vector3.Dot(relativeVelocity, direction) * direction; // Finally the impulse Force acting on the object is given thus // Formula = V_n / (m1 + m2) * m1m2 = V_n / (1/m1 + 1/m2) // Note: system fails if m1 or m2 = 0; but we don't allow that (see Rigidbody) Vector3 impulse = relativeVelocityPerp / ((1 / a.Mass) + (1 / b.Mass)); // Finally add the impulse to both objects (third law of motion) // the 2 comes from (1 + e1e2) where e1 and e2 are the coefficients of restitution a.Impulse += 2 * impulse; b.Impulse -= 2 * impulse; //float epsilon = (a.CoRestitution * b.CoRestitution); //a.Impulse += (-(1 + epsilon) * (Vector3(collidingNormal)) / (a.Mass + b.Mass)) * (a.Mass * b.Mass) * impule; //b.Impulse -= (-(1 + epsilon) * (Vector3(collidingNormal)) / (a.Mass + b.Mass)) * (a.Mass * b.Mass) * impulse; // F = ((-(1 + e)*V_n)/(m1 + m2))*(m1*m2) }
// checks if block did collide with sphere public bool DidCollide(Rigidbody sphere) { // test for collision with each triangles on the cube for (int i = 0; i < 12; i++) { Vector3 normal = normals[i / 2]; if (Physics.TestSphereTriangle(sphere, triangles[i])) { CurrentState = State.Moved; OnBreakEvent(); //add physics to Block depending on BlockType? switch (BlockType) { case Type.Glass: break; case Type.Wood: break; case Type.Stone: break; default: break; } //Impulse = sphere.Velocity / 2.0f; //Acceleration = new Vector3(0.0f, -9.81f, 0.0f); return true; } } return false; }