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); } }
/// <summary> /// Register the entity associated with listener /// Subscribes the Listeners to Enter and Exit Collision events /// At this stage the entities do not own a Collider Component /// Creates a list for each listener to link the respective ICollidables /// </summary> /// <param name="listener"></param> public void RegisterListener(ICollisionListener listener) { //Adds a new Links list that contains the links of this listener to the colliders that it owns links.Add(new List <ICollidable>()); //Subscribes Listener CollisionEnter += new CollisionHandler(listener.OnCollisionEnter); CollisionExit += new CollisionHandler(listener.OnCollisionExit); CollisionStay += new CollisionHandler(listener.OnCollisionStay); }
internal void NotifyCollision(PhysicalBody physicalBody) { ICollisionListener collisionListener = Self.GetAIComponent(); if (null != collisionListener) { collisionListener.OnTouch(physicalBody.Self); } }
/// <summary> /// Calls the scene graph to spawn an entity /// Initializes the entity /// </summary> /// <param name="entity">Entity to be spawned</param> /// <param name="position">Position to spawn the entity</param> public void SpawnEntity(IEntity entity) { entity.isActive = true; sceneGraph.SpawnEntity(entity.UID); //If entity is collidable subscribe entity to collision events ICollisionListener listener = entity as ICollisionListener; if (listener != null) { collisionManager.RegisterListener(listener); } }
/// <summary> /// Removes an entity from the scene graph /// Based on its UID /// </summary> /// <param name="entityID">Entity Unique ID</param> public void RemoveEntity(int entityID) { IEntity entity = entityManager.GetEntity(entityID); entity.isActive = false; //If entity is collidable unsubscribe entity to collision events ICollisionListener listener = entity as ICollisionListener; if (listener != null) { collisionManager.UnregisterListener(listener); } sceneGraph.RemoveEntity(entityID); }
public override void Collision(BoxCollider other) { if (other.entity.Equals(this.entity)) { return; } // If the distance between the two collider's center is greater than the combined halfSize's, then a collision has occured. Vector2 centers = new Vector2(Math.Abs(this.center.X - other.center.X), Math.Abs(this.center.Y - other.center.Y)); Vector2 halves = (this.size + other.size) / 2; if ((centers.X < halves.X) && (centers.Y < halves.Y)) { // Inform the Entity that they have been collided with. ICollisionListener collisionEntity = entity as ICollisionListener; collisionEntity.OnCollision(other); } }
/// <summary> /// Unregisters listeners to collision events /// Removes collision info from the current status /// Removes links of the listener and collides from the links list /// This is called after all the colliders have been removed /// </summary> /// <param name="listener">Listener</param> public void UnregisterListener(ICollisionListener listener) { //Remove the listeener links from the links lists Delegate[] list = CollisionEnter.GetInvocationList(); for (int i = 0; i < list.Length; i++) { ICollisionListener t_listener = list[i].Target as ICollisionListener; if (t_listener == listener) { links.RemoveAt(i); break; } } //Unsubscribe to events CollisionEnter -= new CollisionHandler(listener.OnCollisionEnter); CollisionExit -= new CollisionHandler(listener.OnCollisionExit); CollisionStay -= new CollisionHandler(listener.OnCollisionStay); }
public OnCollisionListenerAddedEventContext(Actor actor, ICollisionListener collisionListener) { this.CorrespondingActor = actor; this.CollisionListener = collisionListener; }
/// <summary> /// Subscribe a collision listener /// </summary> /// <param name="listener"></param> public void SubscribeListener(ICollisionListener listener) { // Subscribe passed collision listener to the collision system ((ICollisionPublisher)_collisionSystem).AddListener(listener.OnNewCollision); }
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); } }
public void AddListener(ICollisionListener newListener) { collisionListeners.Add(newListener); }