public override void Collision(CircleCollider other) { if (other.entity.Equals(this.entity)) { return; } // ABVector is the vector between this object and the other object. Vector2 ABVector = other.center - this.center; // Distance Between Centers is the distance between the 2 collider centers. float distanceBetweenCenters = ABVector.Length(); // If this is greater than the sum of the radii, a collision has occured. if (distanceBetweenCenters <= this.radius + other.radius) { // The collision normal is the ABVector, converted into a Unit vector (or normalized) Vector2 normal = ABVector; normal.Normalize(); // We correct interpenetration BEFORE physics has a chance to occur. // The distanceIntersecting is the distance between the centers, minus the sum of the radii. float distanceIntersecting = (distanceBetweenCenters - (radius + other.radius)); // We push each object along the collision normal as far as they have interpenetrated. entity.transform.SetPosition(entity.transform.position + (normal * distanceIntersecting)); // Now that we have corrected interpenetration, we call OnCollision on the entity, // which will perform physics behaviours if it has a PhysicsBody component. ICollisionListener collisionEntity = entity as ICollisionListener; collisionEntity.OnCollision(other, normal); } }
public void CollisionResolution(CircleCollider other, Vector2 normal) { // When two CircleColliders collide, physics occurs. float cv; // Closing Velocity if (other.entity.GetComponent <PhysicsBody>() != null) { // If the colliding entity has a PhysicsBody, we do physics using both our and their velocity. PhysicsBody otherPhysics = (PhysicsBody)other.entity.GetComponent <PhysicsBody>(); // We calculate closing velocity by getting the dot product of the closing velocity direction and the collision normal. cv = restitutionCoefficient * (Vector2.Dot(otherPhysics.velocity - this.velocity, normal)); } else { // If the colliding entity does not have a PhysicsBody, we do physics using only our own velocity. cv = restitutionCoefficient * (Vector2.Dot(this.velocity * 2, normal)); } // Then the reflectionVelocity is the closing velocity multiplied by the collision normal. reflectionVelocity = normal * cv; // Finally, the reflection velocity is applied to the velocity of this object. // This happens here, rather than in Update in order to apply the reflection velocity only whilst the objects are colliding. velocity += reflectionVelocity; }
public virtual void Collision(CircleCollider other) { }