/// <summary> /// Initialize a Boid with random rotation, position and velocity. /// </summary> public void Initialize(CFlock _sourceFlock) { // Vector3 randomPosition = _sourceFlock.transform.position + UnityEngine.Random.insideUnitSphere * UnityEngine.Random.Range (0f, 3f); // Vector3 randomRotation = new Vector3 (UnityEngine.Random.Range (0f, 360f), UnityEngine.Random.Range (0f, 360f), UnityEngine.Random.Range (0f, 360f)); // Vector3 randomVelocity = UnityEngine.Random.insideUnitSphere; // // Initialize (randomPosition, randomRotation, randomVelocity, _sourceFlock); Initialize(_sourceFlock.transform.position, Vector3.zero, Vector3.zero, _sourceFlock); }
IEnumerator EmitWithDelay(CFlock _flock, float _delay) { for (int i = 0; i < flockSize; i++) { CBoid newBoid = Instantiate <CBoid>(boidTemplate); newBoid.name = string.Format("Boid {0}", i); _flock.AddTo(newBoid); yield return(new WaitForSeconds(emitDelay)); } }
private Vector3 Separation(Vector3 position, CFlock flock) { // Separation // Method checks for nearby boids and steers away Vector3 steer = Vector3.Zero; int count = 0; // For every boid in the system, check if it's too close foreach (var other in flock.Members) { var otherTransform = (CTransform)Game1.Inst.Scene.GetComponentFromEntity <CTransform>(other); float d = PositionalUtil.Distance(position, otherTransform.Position); // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) if ((d > 0) && (d < flock.SeparationDistance)) { // Calculate vector pointing away from neighbor Vector3 diff = Vector3.Normalize(position - otherTransform.Position); //Vector3 diff = Vector3.Subtract(position, otherTransform.Position); //diff.Normalize(); diff /= d; // Weight by distance (makes closer neighbors more important to steer away from) steer += diff; count++; // Keep track of how many } } // Average -- divide by how many if (count > 0) { steer /= count; } /* * // As long as the vector is greater than 0 * if (steer.X > 0 || steer.Y > 0 || steer.Z > 0) { * // First two lines of code below could be condensed with new PVector setMag() method * // Not using this method until Processing.js catches up * // steer.setMag(maxspeed); * * // Implement Reynolds: Steering = Desired - Velocity * steer.Normalize(); * steer *= (maxspeed); * steer.sub(velocity); * steer.limit(maxforce); * }*/ //steer.Normalize(); return(steer); }
/// <summary> /// See enemies ! /// </summary> private int SeeEnemies(CFlock _flock) { float distance; CBoid enemy; numOfEnemiesSeen = 0; nearestEnemy = null; distanceToNearestEnemy = float.MaxValue; for (int i = 0; i < CFlock.flockCount; i++) { if (CFlock.listOfFlocks [i] == parentFlock) { continue; } enemy = CFlock.listOfFlocks [i].GetFirstMember(); while (enemy != null) { if ((distance = CanISee(enemy)) != float.MaxValue) { numOfEnemiesSeen++; // Test: Closets enemy ? if (distance < minDistanceToEnemies) { distanceToNearestEnemy = distance; nearestEnemy = enemy; } } enemy = enemy.GetNext(); } } return(numOfEnemiesSeen); }
/// <summary> /// Basic boid initialization /// </summary> void Initialize(Vector3 _position, Vector3 _rotation, Vector3 _velocity, CFlock _sourceFlock) { parentFlock = _sourceFlock; myTransform.position = _position; myTransform.eulerAngles = _rotation; Velocity = _velocity; OverallSpeed = Velocity.magnitude; numOfFlockmatesSeen = 0; nearestFlockmate = null; distanceToNearestFlockmate = Mathf.Infinity; numOfEnemiesSeen = 0; nearestEnemy = null; distanceToNearestEnemy = Mathf.Infinity; next = previous = null; initialized = true; }
public static void CreateAnimals(int numFlocks, int worldsize) { var currentScene = Game1.Inst.Scene; int membersPerFlock = (int)(rnd.NextDouble() * 10) + 10; var flockRadius = membersPerFlock; for (int f = 0; f < numFlocks; f++) { int flockId = currentScene.AddEntity(); CFlock flock = new CFlock { Radius = 20, SeparationDistance = 3, AlignmentFactor = 0.1f, CohesionFactor = 0.5f, SeparationFactor = 100.0f, PreferredMovementSpeed = 150f }; double animal = rnd.NextDouble(); string flockAnimal = animal > 0.66 ? "flossy" : animal > 0.33 ? "goose" : "hen"; string deathSound = string.Format("Sounds/Effects/{0}", (flockAnimal == "flossy" ? "SheepDeath" : flockAnimal == "goose" ? "GooseDeath" : "DeathChicken")); int flockX = (int)(rnd.NextDouble() * worldsize * 2 - worldsize); int flockZ = (int)(rnd.NextDouble() * worldsize * 2 - worldsize); CTransform flockTransform = new CTransform { Position = new Vector3(flockX, 0, flockZ) }; for (int i = 0; i < membersPerFlock; i++) { int id = currentScene.AddEntity(); var npcAnim = wiggleAnimation(id); if (flockAnimal.Equals("hen")) { // TODO: Make animals have different animations based on state CAnimation normalAnimation = new CHenNormalAnimation { animFn = npcAnim }; // Set a random offset to animation so not all animals are synced normalAnimation.CurrentKeyframe = rnd.Next(normalAnimation.Keyframes.Count - 1); // Random animation speed between 0.8-1.0 normalAnimation.AnimationSpeed = (float)rnd.NextDouble() * 0.2f + 0.8f; currentScene.AddComponent <C3DRenderable>(id, normalAnimation); } else { CImportedModel modelComponent = new CImportedModel { animFn = npcAnim }; modelComponent.fileName = flockAnimal; modelComponent.model = Game1.Inst.Content.Load <Model>("Models/" + modelComponent.fileName); currentScene.AddComponent <C3DRenderable>(id, modelComponent); } float memberX = flockTransform.Position.X + (float)rnd.NextDouble() * flockRadius * 2 - flockRadius; float memberZ = flockTransform.Position.Z + (float)rnd.NextDouble() * flockRadius * 2 - flockRadius; float y = flockTransform.Position.Y; CTransform transformComponent = new CTransform(); transformComponent.Position = new Vector3(memberX, y, memberZ); transformComponent.Rotation = Matrix.CreateFromAxisAngle(Vector3.UnitY, (float)(Math.PI * (rnd.NextDouble() * 2))); float size = 0.5f; transformComponent.Scale = new Vector3(1f); currentScene.AddComponent(id, transformComponent); currentScene.AddComponent(id, new CBody { InvMass = 0.05f, Aabb = new BoundingBox(-size * new Vector3(1.0f, 2.0f, 1.0f), size * new Vector3(1.0f, 0.0f, 1.0f)), LinDrag = 0.8f, Velocity = Vector3.Zero, Radius = 1f, SpeedMultiplier = size, MaxVelocity = 4, Restitution = 0 }); // health value of npcs, maybe change per species/flock/member? var npcHealth = 1; currentScene.AddComponent(id, new CHealth { MaxHealth = npcHealth, Health = npcHealth, DeathSound = deathSound }); currentScene.AddComponent(id, new CAI { Flock = flockId }); currentScene.AddComponent(id, new CSyncObject()); flock.Members.Add(id); } currentScene.AddComponent(flockId, flock); currentScene.AddComponent(flockId, flockTransform); } }