public List <string> GetCollidedWith() { List <string> collidedWith = new List <string>(); //Checks if entity has a sphere collider or a box collider if ((mask & ComponentTypes.COMPONENT_SPHERE_COLLIDER) == ComponentTypes.COMPONENT_SPHERE_COLLIDER) { //Gets sphere collider component from entity ComponentSphereCollider sphereCollider = (ComponentSphereCollider)componentList.Find(delegate(IComponent e) { return(e.ComponentType == ComponentTypes.COMPONENT_SPHERE_COLLIDER); }); //Gets the collidedWith list from the sphere collider component collidedWith = sphereCollider.CollidedWith; } else { if ((mask & ComponentTypes.COMPONENT_BOX_COLLIDER) == ComponentTypes.COMPONENT_BOX_COLLIDER) { //Gets sphere collider component from entity ComponentBoxCollider boxCollider = (ComponentBoxCollider)componentList.Find(delegate(IComponent e) { return(e.ComponentType == ComponentTypes.COMPONENT_BOX_COLLIDER); }); //Gets the collidedWith list from the sphere collider component collidedWith = boxCollider.CollidedWith; } } return(collidedWith); }
/// <summary> /// Executes a sphere->sphere collision check between two entities /// </summary> /// <param name="entity">Current entity to check collisions for</param> /// <param name="collidedEntity">Potentially collided with entity</param> /// <param name="sphereCollider">The sphere collider of the current entity</param> /// <returns></returns> private bool SphereSphereCollisionCheck(Entity entity, Entity collidedEntity, ComponentSphereCollider sphereCollider) { bool collided = false; //Retrieves sphere collider for potentially collided entity IComponent collidedEntityCollider = collidedEntity.Components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_SPHERE_COLLIDER); }); ComponentSphereCollider collidedSphereCollider = ((ComponentSphereCollider)collidedEntityCollider); //Radius of entity float radius1 = sphereCollider.Radius; //Radius of collided entity float radius2 = collidedSphereCollider.Radius; //Position of entity Vector3 position = entity.GetTransform().Translation; //Position of collided entity Vector3 collidedEntityPosition = collidedEntity.GetTransform().Translation; //Checks if the two radii are intersecting, causing a collision if they are if ((radius1 + radius2) > (collidedEntityPosition - position).Length) { collided = true; return(true); } return(collided); }
public ComponentSphereCollider GetComponentSphereCollider() { //Checks if entity has a sphere collider if ((mask & ComponentTypes.COMPONENT_SPHERE_COLLIDER) == ComponentTypes.COMPONENT_SPHERE_COLLIDER) { //Gets sphere collider component from entity ComponentSphereCollider sphereCollider = (ComponentSphereCollider)componentList.Find(delegate(IComponent e) { return(e.ComponentType == ComponentTypes.COMPONENT_SPHERE_COLLIDER); }); return(sphereCollider); } return(null); }
/// <summary> /// Executes a sphere->box collision check between two entities /// </summary> /// <param name="entity">Current entity to check collisions for</param> /// <param name="collidedEntity">Potentially collided with entity</param> /// <param name="sphereCollider">The sphere collider of the current entity</param> /// <returns></returns> private bool SphereBoxCollisionCheck(Entity entity, Entity collidedEntity, ComponentSphereCollider sphereCollider) { //Retrieves box collider for potentially collided entity IComponent collidedEntityCollider = collidedEntity.Components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_BOX_COLLIDER); }); ComponentBoxCollider collidedBoxCollider = ((ComponentBoxCollider)collidedEntityCollider); //Radius squared of sphere collider component for entity float radiusSquared = sphereCollider.Radius * sphereCollider.Radius; //Position of entity Vector3 position = entity.GetTransform().Translation; //Position of collided entity Vector3 collidedEntityPosition = collidedEntity.GetTransform().Translation; //Width height and depth of box collider component for potentially collided entity Vector3 boxMax = new Vector3(collidedEntityPosition.X + collidedBoxCollider.Width, collidedEntityPosition.Y + collidedBoxCollider.Height, collidedEntityPosition.Z + collidedBoxCollider.Depth); Vector3 boxMin = new Vector3(collidedEntityPosition.X - collidedBoxCollider.Width, collidedEntityPosition.Y - collidedBoxCollider.Height, collidedEntityPosition.Z - collidedBoxCollider.Depth); //Calculates distance between the sphere and the closest point on the bounding box float distance = 0; for (int i = 0; i < 3; i++) { if (position[i] < boxMin[i]) { distance += ((position[i] - boxMin[i]) * (position[i] - boxMin[i])); } else if (position[i] > boxMax[i]) { distance += ((position[i] - boxMax[i]) * (position[i] - boxMax[i])); } } //If distance between sphere and point is less than or equal to radius, returns true, else false return(distance <= radiusSquared); }
public void OnAction() { foreach (Entity entity in entityList) { List <IComponent> components = entity.Components; IComponent transformComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); IComponent boxColliderComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_BOX_COLLIDER); }); ComponentBoxCollider boxCollider = ((ComponentBoxCollider)boxColliderComponent); IComponent sphereColliderComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_SPHERE_COLLIDER); }); ComponentSphereCollider sphereCollider = ((ComponentSphereCollider)sphereColliderComponent); List <string> ignoreCollisions = ((ComponentSphereCollider)sphereColliderComponent).IgnoreCollisionsWith; IComponent aiComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_AI); }); //Stores the old positions of all the moving entities for collision detection Vector3 oldPosition; if (!oldPositions.TryGetValue(entity.Name, out oldPosition)) { oldPosition = entity.GetTransform().Translation; oldPositions.Add(entity.Name, oldPosition); } //Checks if entity has box collider and executes box collision checks if it has if (boxColliderComponent != null) { //Checks if collisions are disabled if (boxCollider.Disabled != true) { foreach (Entity collidedEntity in collidableEntities) { if ((collidedEntity.Mask & BOXMASK) == BOXMASK) { } if ((collidedEntity.Mask & SPHEREMASK) == SPHEREMASK) { } } } } //Checks if entity has sphere collider and executes sphere collision checks if it has if (sphereColliderComponent != null) { //Clears collided with list sphereCollider.CollidedWith.Clear(); //Checks if collisions are disabled if (sphereCollider.Disabled != true) { //Checks if entity has ai component and if player is within fov then sets bool to true, and will be set back to false if a collision is dectected between the entity and the player if (aiComponent != null && ((ComponentAI)aiComponent).PlayerInFov == true) { ((ComponentAI)aiComponent).CanSeePlayer = true; } foreach (Entity collidedEntity in collidableEntities) { ignoreEntity = false; //Checks that entity isn't trying to collide with itself if (entity.Name != collidedEntity.Name) { //Checks that entity isn't trying to collide with ignored entities foreach (string name in ignoreCollisions) { if (collidedEntity.Name.Contains(name)) { ignoreEntity = true; } } //If current collidable entity has a box collider then executes Sphere on Box collision checks if ((collidedEntity.Mask & BOXMASK) == BOXMASK && ignoreEntity != true) { //Current position of current entity Vector2 position = entity.GetTransform().Translation.Xz; //Old position of current entity Vector3 oldPositionV3; oldPositions.TryGetValue(entity.Name, out oldPositionV3); Vector2 oldPositionV2 = oldPositionV3.Xz; //Collision check bool collided = SphereBoxCollisionCheck(collidedEntity, sphereColliderComponent, position, oldPositionV2); //If entity has ai component does a raycast check to see if there is an obstacle between the entity and the target entity if (aiComponent != null && ((ComponentAI)aiComponent).PlayerInFov == true && ((ComponentAI)aiComponent).CanSeePlayer == true) { if (SphereBoxCollisionCheck(collidedEntity, sphereColliderComponent, ((ComponentAI)aiComponent).PlayerPosition.Xz, position)) { ((ComponentAI)aiComponent).CanSeePlayer = false; } } //If entity has collided with this collidable entity, sets the entities position to its old position and adds the collidable entity to the collidedWith list if (collided == true) { oldPositions.TryGetValue(entity.Name, out oldPosition); entity.GetTransform().Translation = oldPosition; sphereCollider.CollidedWith.Add(collidedEntity.Name); collidedEntity.GetCollidedWith().Add(entity.Name); } } //If current collidable entity has a sphere collider then executes Sphere on Sphere collision checks if ((collidedEntity.Mask & SPHEREMASK) == SPHEREMASK && ignoreEntity != true) { //Collision check bool collided = SphereSphereCollisionCheck(entity, collidedEntity, sphereColliderComponent); //If entity has collided with this collidable entity, sets the entities position to its old position and adds the collidable entity to the collidedWith list if (collided == true) { oldPositions.TryGetValue(entity.Name, out oldPosition); entity.GetTransform().Translation = oldPosition; sphereCollider.CollidedWith.Add(collidedEntity.Name); collidedEntity.GetCollidedWith().Add(entity.Name); } } } } } } //Keeps stored old positions up to date every frame oldPosition = entity.GetTransform().Translation; oldPositions.Remove(entity.Name); oldPositions.Add(entity.Name, oldPosition); } }
/// <summary> /// Loops through every assigned entity applying sphere collision logic /// </summary> public void OnAction() { foreach (Entity entity in entityList) { //Retrieves entities component list List <IComponent> components = entity.Components; //Retrieves entities sphere collider IComponent sphereColliderComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_SPHERE_COLLIDER); }); ComponentSphereCollider sphereCollider = ((ComponentSphereCollider)sphereColliderComponent); //Retrives list of entities to ignore collisions with List <string> ignoreCollisions = sphereCollider.IgnoreCollisionsWith; //Stores/retrieves the old positions of all the moving entities for collision detection Vector3 oldPosition; if (!oldPositions.TryGetValue(entity.Name, out oldPosition)) { oldPosition = entity.GetTransform().Translation; oldPositions.Add(entity.Name, oldPosition); } //Checks if collisions are disabled if (sphereCollider.Disabled != true) { //Clears collided with list sphereCollider.CollidedWith.Clear(); //Does collision checks for each collidable entity foreach (Entity collidedEntity in collidableEntities) { ignoreEntity = false; //Checks that the entity isn't trying to collide with itself if (entity.Name != collidedEntity.Name) { //Checks that entity isn't trying to collide with ignored entities foreach (string name in ignoreCollisions) { if (collidedEntity.Name.Contains(name)) { ignoreEntity = true; } } //Does a Sphere->Box collision check if the collidable entity has a box collider if ((collidedEntity.Mask & BOXMASK) == BOXMASK && ignoreEntity != true) { } //Does a Sphere->Sphere collision check if the collidable entity has a sphere collider if ((collidedEntity.Mask & SPHEREMASK) == SPHEREMASK && ignoreEntity != true) { bool collided = SphereSphereCollisionCheck(entity, collidedEntity, sphereCollider); //If entity has collided with this collidable entity, sets the entities position to its old position and adds the collidable entity to the collidedWith list if (collided == true) { oldPositions.TryGetValue(entity.Name, out oldPosition); entity.GetTransform().Translation = oldPosition; sphereCollider.CollidedWith.Add(collidedEntity.Name); collidedEntity.GetCollidedWith().Add(entity.Name); } } } } } } }