public virtual void CheckForCollisions() { // Get a reference to the entity list. List <IEntity> entityList = SceneManager.Instance.CurrentScene.Entities.Entities; // Then we check for collisions against all other types of collider. for (int i = 0; i < entityList.Count; i++) { if (entityList[i].GetComponent <BoxCollider>() != null) { BoxCollider otherCollider = (BoxCollider)entityList[i].GetComponent <BoxCollider>(); Collision(otherCollider); } if (entityList[i].GetComponent <PlaneCollider>() != null) { PlaneCollider otherCollider = (PlaneCollider)entityList[i].GetComponent <PlaneCollider>(); Collision(otherCollider); } if (entityList[i].GetComponent <CircleCollider>() != null) { CircleCollider otherCollider = (CircleCollider)entityList[i].GetComponent <CircleCollider>(); Collision(otherCollider); } } }
public void CollisionResolution(PlaneCollider other, Vector2 normal) { //Console.WriteLine("Physics with Plane"); float cv; // Closing Velocity //Console.WriteLine("Velocity: " + this.velocity); //Console.WriteLine("Dot Product: " + Vector2.Dot(this.velocity * 2, normal)); cv = restitutionCoefficient * (Vector2.Dot(this.velocity * 2, normal)); //Console.WriteLine("CV: " + cv); reflectionVelocity = -normal * cv; // Console.WriteLine(normal); //Console.WriteLine("Reflection Velocity: " + reflectionVelocity); velocity += reflectionVelocity; }
public virtual void Collision(PlaneCollider other) { }
public override void Collision(PlaneCollider other) { // Create a new Vector2, with position equal to the Plane's position. Vector2 testVector = other.position; // TestVariable is what the distance is checked against. float testVariable; if (other.horizontal) { if (this.position.X < other.position.X || this.position.X > other.position.X + other.width) { return; } // If the Plane is horizontal // Shift the TestVector along the X-Axis so it is equal to this colliders center. testVector.X = this.center.X; testVariable = this.size.Y / 2; } else { // If the plane is Vertical // Shift the Testvector along the Y-Axis so it is equal to this colliders center. testVector.Y = this.center.Y; testVariable = this.size.X / 2; } if (Vector2.Distance(this.center, testVector) <= testVariable) { // Collision normal Vector2 normal; if (other.horizontal) { if (testVector.Y >= this.center.Y) { // If we're above the plane, the normal is directly up. normal = -Vector2.UnitY; // Correct interpenetration. entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.center.Y + testVariable), testVector))); } else { // If we're below the plane, the normal is directly down. normal = Vector2.UnitY; // Correct interpenetration. entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.position.Y), testVector))); } } else { if (testVector.X <= testVariable) { // If we're to the right of the plane normal = Vector2.UnitX; // Correct interpenetration. entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X, this.position.Y), testVector))); } else { normal = -Vector2.UnitX; entity.transform.SetPosition(entity.transform.position + (normal * Vector2.Distance(new Vector2(this.center.X + testVariable, this.center.Y), testVector))); } } // Resolve the collision. ICollisionListener collisionEntity = entity as ICollisionListener; collisionEntity.OnCollision(other, normal); } }