//checking zombie to zombie collisions. //Model as a sphere (in reality just a cylinder but since all at same height it only checks for a circle radius around character private void checkZombietoZombie(Zombie z1, Zombie z2) { //creating appropriate shapes Sphere p1 = new Collisions.Sphere(z1.Position, z1.Velocity, z1.modelRadius); Sphere p2 = new Collisions.Sphere(z2.Position, z2.Velocity, z2.modelRadius); Contact c = p1.Collides(p2); if (c != null) { if (c.DeepestPoint.Length() > 0) { z1.Position -= c.DeepestPoint - c.ContactPoint; z2.Position += c.DeepestPoint - c.ContactPoint; } } }
private void CheckCollisions(bool Extinguisher) { #region Player collisions Sphere heroSphere = new Sphere(Player.Position, Player.Velocity, Player.modelRadius); List<Primitive> primitivesNearby = new List<Primitive>(); LevelQuadTree.RetrieveNearbyObjects(heroSphere, ref primitivesNearby,2); /*foreach (Primitive bx in primitivesNearby) { if (!TotalNearbyBoxes.Contains(bx)) TotalNearbyBoxes.Add(bx); }*/ Contact EndingContact = heroSphere.Collides(EscapeSpot); if (EndingContact != null && (Player.ItemsList.ContainsKey(key1) || Player.ItemsList.ContainsKey(key2))) { GameStates.GameStates.ZombieGameState = GameStates.GameStates.GameState.End; } primitivesNearby.AddRange(fireHazards); foreach (Primitive p in primitivesNearby) { Contact c = heroSphere.Collides(p as Box); if (c != null) { ResolveStaticCollision(c, Player, heroSphere); if(fireDamageDelay <=0) { if(((Box)p).Tag == "Fire1" || ((Box)p).Tag == "Fire2" || ((Box)p).Tag == "Fire3" || ((Box)p).Tag == "Fire4") { Player.TakeDamage(25); fireDamageDelay = 5; } } } } if (Extinguisher) { Sphere ExtSphere = new Sphere(Player.Position + (Player.Velocity / Player.Velocity.Length()) * 20, Vector3.One, 10); for (int i = 0; i < fireHazards.Count;i++) { Sphere boxSphere = new Sphere(fireHazards[i].Position, Vector3.One, fireHazards[i].Size.X / 2); Contact contact = ExtSphere.Collides(boxSphere); if (contact != null) { if (fireHazards[i].Tag == "Fire1") { FireEmitter.particleGroups[0].controller.LifeSpan -= 5; if (FireEmitter.particleGroups[0].controller.LifeSpan <= 0) { FireEmitter.Stop(); fireHazards.Remove(fireHazards[i]); } } if (fireHazards.Count > 0) { if (fireHazards[i].Tag == "Fire2") { FireEmitter2.particleGroups[0].controller.LifeSpan -= 5; if (FireEmitter2.particleGroups[0].controller.LifeSpan <= 0) { FireEmitter2.Stop(); fireHazards.Remove(fireHazards[i]); } } if (fireHazards[i].Tag == "Fire3") { FireEmitter3.particleGroups[0].controller.LifeSpan -= 5; if (FireEmitter3.particleGroups[0].controller.LifeSpan <= 0) { FireEmitter3.Stop(); fireHazards.Remove(fireHazards[i]); } } if (fireHazards[i].Tag == "Fire4") { FireEmitter4.particleGroups[0].controller.LifeSpan -= 5; if (FireEmitter4.particleGroups[0].controller.LifeSpan <= 0) { FireEmitter4.Stop(); fireHazards.Remove(fireHazards[i]); } } } } } } #endregion #region Zombie collisions foreach (Zombie z in zombies) { // Check for zombies in sight radius and zombies who are not wandering if ((z.Position - Player.Position).Length() < SIGHT_RADIUS || z.BehaviouralState != BehaviourState.Wander) { Sphere zombieSphere = new Sphere(z.Position, z.Velocity, z.modelRadius); List<Primitive> primitives = new List<Primitive>(); LevelQuadTree.RetrieveNearbyObjects(zombieSphere, ref primitives); primitives.AddRange(fireHazards); foreach (Primitive p in primitives) { Contact c = zombieSphere.Collides(p as Box); if (c != null) { if (z.BehaviouralState == BehaviourState.Wander) z.Velocity = Vector3.Cross( z.Velocity, Vector3.Up); ResolveStaticCollision(c, z, zombieSphere); } } } } #endregion foreach (Zombie z1 in zombies) { if ((z1.Position - Player.Position).Length() < SIGHT_RADIUS || z1.BehaviouralState != BehaviourState.Wander) { checkZombietoPlayer(z1); foreach (Zombie z2 in zombies) { if (!z2.Equals(z1) && ((z2.Position - Player.Position).Length() < SIGHT_RADIUS || z2.BehaviouralState != BehaviourState.Wander)) { checkZombietoZombie(z1, z2); } } } } checkItemCollisions(); }
//checking zombie to character private void checkZombietoPlayer(Zombie z) { Sphere p1 = new Collisions.Sphere(z.Position, z.Velocity, z.modelRadius); Sphere p2 = new Collisions.Sphere(Player.Position, Player.Velocity, Player.modelRadius); Contact c = p1.Collides(p2); if (c != null) { if (c.DeepestPoint.Length() > 0) { if (Player.Stance == AnimationStance.Standing)//if standing, dont push player, only affect zombie { z.Position -= c.DeepestPoint - c.ContactPoint; Player.Position += c.DeepestPoint - c.ContactPoint; } else//push player back when walking { z.Position -= c.DeepestPoint - c.ContactPoint; } } } }
// Creates a bounding sphere with the specified radius. Any Zombie intersecting the // bounding sphere will be alerted to the Hero's presence private void CastSoundWave(float radius) { if (radius > 0) { Sphere soundSphere = new Sphere(Player.Position, new Vector3(), radius); foreach (Zombie z in zombies) { Sphere zs = new Sphere(z.Position, z.Velocity, z.modelRadius); if (zs.Collides(soundSphere) != null) z.Alert(Player); } } }