void DetectSurrounding() { currentDelayCheckingCollision += Time.deltaTime; if (currentDelayCheckingCollision >= delayCheckingCollision) { surrounding = Physics.OverlapSphere(transform.position, detectRadius); surroundingBoids.Clear(); obstaclesList.Clear(); for (int i = 0; i < surrounding.Length; i++) { Boids boids = flock.GetBoidsByID(surrounding[i].gameObject.GetInstanceID()); if (boids != null) { surroundingBoids.Add(boids); } else { obstaclesList.Add(surrounding[i]); } } currentDelayCheckingCollision -= delayCheckingCollision; } }
private void LogDebugInfo() { Console.WriteLine("=============================================================="); Console.WriteLine("DEBUG INFO: "); Console.WriteLine("Boid Count: " + Boids.Count); Console.WriteLine("Separation: " + Boid.Sep); Console.WriteLine("Alignment: " + Boid.Ali); Console.WriteLine("Cohesion: " + Boid.Coh); Console.WriteLine("Follow Leader: " + Boid.Goal); Console.WriteLine("Max Boid Speed: " + Boid.MAX_SPEED); Console.WriteLine("Leader Boid:"); Console.WriteLine(" Position: " + LeaderBoid.Position); Console.WriteLine(" Velocity: " + LeaderBoid.Velocity); Console.WriteLine(" Size: " + LeaderBoid.Size); Console.WriteLine(" Front: " + LeaderBoid.Front); Console.WriteLine(" Right: " + LeaderBoid.Right); Console.WriteLine(" Pitch: " + LeaderBoid.Pitch); Console.WriteLine(" Yaw: " + LeaderBoid.Yaw); Console.WriteLine(""); Console.WriteLine("Boids: "); foreach (Boid b in Boids.OfType <Boid>()) { Console.WriteLine("Boid"); Console.WriteLine(" Position: " + b.Position); Console.WriteLine(" Velocity: " + b.Velocity); Console.WriteLine(" Size: " + b.Size); Console.WriteLine(" Front: " + b.Front); Console.WriteLine(" Right: " + b.Right); Console.WriteLine(" Pitch: " + b.Pitch); Console.WriteLine(" Yaw: " + b.Yaw); } Console.WriteLine("=============================================================="); Console.WriteLine(""); }
protected void Start() { boidsArray = new Boids[numberBoids]; for (int i = 0; i < numberBoids; i++) { Vector3 startPos; if (flockMode == FlockMode.InsideSphere) { startPos = Random.insideUnitSphere * spawnRadius; } else { startPos = Random.insideUnitCircle * spawnRadius; } Boids boids = Instantiate(boidsPrefab, transform.position + startPos, Quaternion.identity); boids.transform.SetParent(boidsContainer); boidsDictionary.Add(boids.gameObject.GetInstanceID(), boids); boidsArray[i] = boids; if (flockMode == FlockMode.OnSphere) { InstantiateBoidsDome(boids); } boids.isSpherical = (flockMode == FlockMode.InsideSphere); //On unit sphere map la position 2D boids.flock = this; } }
private Vector3 Allign(Boids boid) { float perceptionRadius = 30f; Vector3 steering = new Vector3(0, 0, 0); int total = 0; foreach (var boidd in boidList) { float distance = Vector3.Distance(boid.boidObject.transform.GetChild(0).position, boidd.boidObject.transform.GetChild(0).position); if (boidd != boid && distance < perceptionRadius) { steering = steering + boidd.velocity; total = total + 1; } } if (total > 0) { steering = steering / total; steering = steering.normalized * boid.maxSpeed; steering = steering - boid.velocity; if (steering.magnitude > boid.maxForce) { steering = steering.normalized * boid.maxForce; } } return(steering); }
// Separation private Vector3 separate(List <Boids> boids) { Vector3 sum = new Vector3(0, 0, 0); int count = 0; // checks every boid in the system if they're too close for (int i = 0; i < boids.Count; i++) { Boids other = boids[i]; float d = Vector3.Distance(this.transform.position, other.transform.position); if ((d > 0) && (d < settings.SeparationRadius)) { Vector3 diff = this.transform.position - other.transform.position; diff.Normalize(); diff /= d; // weight by distance sum += diff; count++; } } if (count > 0) // average { sum /= (float)count; } return(sum * settings.SeparationForce); }
void InsertIntoNeighbours(Vector3Int argCurrentCell, Boids argBoid) { for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { for (int z = -1; z < 2; z++) { if (x == 0 && y == 0 && z == 0) { continue; } int actualX = argCurrentCell.x + x; int actualY = argCurrentCell.y + y; int actualZ = argCurrentCell.z + z; Vector3Int gridIndex = new Vector3Int(actualX, actualY, actualZ); if (!mCells.ContainsKey(gridIndex)) { mCells.Add((gridIndex), new SpatialGridCell()); } mCells[gridIndex].InsertFromNeighbour(argBoid); } } } }
// Called once per frame void FixedUpdate() { // Limit updates updateCount++; if (updateCount > 20) { // Loop through boids for (int i = 0; i < numberOfBoids; i++) { // Get teh boid Boids boid = boids[i]; // If it exsists and has a rigidbody if (boid != null && boid.rigidbody != null) { Vector3 alignment = Alignment(boid) * alignmentFactor * Time.deltaTime; Vector3 cohesion = Cohesion(boid) * cohesionFactor * Time.deltaTime; Vector3 separation = Separation(boid) * separationFactor * Time.deltaTime; boid.rigidbody.velocity += (alignment + cohesion + separation); } } // Reset the count updateCount = 0; } }
public override Vector2 movement(Boids boid, List <Transform> context, Flock flock) { if (context.Count == 0) { return(Vector2.zero); } Vector2 avoidance_move = Vector2.zero; int _avoid = 0; List <Transform> filterContext = (filter == null) ? context : filter.Filter(boid, context); foreach (Transform element in filterContext) { if (Vector2.SqrMagnitude(element.position - boid.transform.position) < flock.SquareAvoidanceRadius) { _avoid++; avoidance_move += (Vector2)(boid.transform.position - element.transform.position); } } if (_avoid > 0) { avoidance_move /= _avoid; } return(avoidance_move); }
/// <summary> /// Remove a ship from this task force. /// </summary> public void TrimShip() { if (0 != Boids.Count) { ShipFactory.Instance.ReturnAShip(Boids[Boids.Count - 1]); Boids.RemoveAt(Boids.Count - 1); } }
// Start is called before the first frame update void Start() { enemyList.Add(this.gameObject); timeSinceBoidCalc = 0f; spriteRenderer = GetComponent <SpriteRenderer>(); boidScript = GetComponent <Boids>(); moveVector = boidScript.CalculateBoid(enemyList, FetchNeighbors(), target).normalized; }
/// <summary> /// Remove all ships from this task force. /// </summary> public void RemoveAllShips() { foreach (MovingObject ship in Boids) { ShipFactory.Instance.ReturnAShip(ship); } Boids.Clear(); }
void InstantiateBoidsDome(Boids boids) { BoidsDome boidsDome = Instantiate(boidsDomePrefab, boids.transform.position, Quaternion.identity); boids.GetComponent <TrailRenderer>().enabled = false; boidsDome.boids = boids; boidsDome.transform.SetParent(boidsDomeContainer); }
public Boid InstantiateBoid(Vector3 position, bool isFish = false) { Boid boid = Instantiate(boidPrefab, position, Quaternion.identity, boidsParent).GetComponent <Boid>(); Boids.Add(boid); boid.Index = Boids.IndexOf(boid); boid.IsFish = isFish; return(boid); }
private void Setup(Box bounds, List <Point3d> pointsInitial, double pr, double am, double cm, double sm, List <Curve> geo, double gm, double gv) { BoidsInstance = new Boids(bounds, pointsInitial, pr, am, cm, sm, gm, gv, this.Bounce); if (attract) { BoidsInstance.AttractorGeometry = geo; } SetupDone = true; }
// Spawns Scalable Boids protected void SpawnBoid() { Vector3 size = BoidSpawner.transform.localScale; Vector3 position = BoidSpawner.transform.position + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), 0); GameObject temp = Instantiate(BoidPrefab, position, BoidSpawner.transform.rotation); temp.GetComponent <PathFollow>().path = Path.GetComponent <PathPlacer>().path; Boids.Add(temp); }
// Destroys Boid and changes formation public void Reorganize(GameObject DeadBoid) { size--; Boids.Remove(DeadBoid); if (size != 0) { SetFollowers(); } Destroy(DeadBoid); }
public static void atBounds(Boids boid, BoidController boidController) { if (boidController.end.z > boid.transform.position.z) { boid.transform.position = new Vector3(boid.transform.position.x, boid.transform.position.y, boidController.start.z); } if (boidController.start.z < boid.transform.position.z) { boid.transform.position = new Vector3(boid.transform.position.x, boid.transform.position.y, boidController.end.z); } }
public void RemoveBoid(Boids argBoid) { if (argBoid == null) { return; } if (mBoids.Contains(argBoid)) { mBoids.Remove(argBoid); } }
public override Vector2 movement(Boids boid, List <Transform> context, Flock flock) { Vector2 centerOffset = center - (Vector2)boid.transform.position; float t = centerOffset.magnitude / radius; if (t < 0.9f) { return(Vector2.zero); } return(centerOffset * t * t); }
/* if a Boid leave my zone remove it from the neighbors list */ void OnTriggerExit(Collider other) { Boids b = other.GetComponent <Boids> (); if (b != null) { if (neighbors.IndexOf(b) == -1) { neighbors.Remove(b); } } }
public void InstantiateBoids() { GameObject go = Instantiate(boidsPrefab); Boids b = go.GetComponent <Boids> (); b.transform.SetParent(boidsAnchor); boids.Add(b); if (boids.Count < numBoids) { Invoke("InstantiateBoids", spawnDelay); } }
public override List <Transform> Filter(Boids boid, List <Transform> original) { List <Transform> filtered = new List <Transform>(); foreach (Transform element in original) { if (mask == (mask | (1 << element.gameObject.layer))) { filtered.Add(element); } } return(filtered); }
// Update is called once per frame void Update() { Vector2 corner1 = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)); Vector2 corner2 = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 0)); Vector3 pos = transform.position; if (this.transform.position.x < corner1.x) { pos.x += corner2.x - corner1.x; } if (transform.position.x > corner2.x) { pos.x -= corner2.x - corner1.x; } if (this.transform.position.y < corner1.y) { pos.y += corner2.y - corner1.y; } if (transform.position.y > corner2.y) { pos.y -= corner2.y - corner1.y; } transform.position = pos; myBoid = this.gameObject.GetComponent <Boids>(); velocity = GetComponentInChildren <Rigidbody2D>().velocity; if (Master.instance.alignmentOn) { alignment = Alignment(); } if (Master.instance.cohesionOn) { cohesion = Cohesion(); } if (Master.instance.seperationOn) { seperation = Separation(); } velocity = Master.instance.movePoint + (alignment * Master.instance.alignmentValue) + (cohesion * Master.instance.cohesionValue) + (seperation * Master.instance.seperationValue); velocity.Normalize(); GetComponent <Rigidbody2D>().velocity = velocity * Master.instance.speed; }
public override List <Transform> Filter(Boids boid, List <Transform> original) { List <Transform> filtered = new List <Transform>(); foreach (Transform element in original) { Boids boid_AI = element.GetComponent <Boids>(); if (boid_AI != null && boid_AI.BoidFlock == boid.BoidFlock) { filtered.Add(element); } } return(filtered); }
// Spawns Scalable Boids protected void SpawnBoid() { Vector3 size = BoidSpawner.transform.localScale; Vector3 position = BoidSpawner.transform.position + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), 0); GameObject temp = Instantiate(BoidPrefab, position, BoidSpawner.transform.rotation); temp.GetComponent <PathFollow>().path = Path.GetComponent <PathPlacer>().path; foreach (GameObject Boid in Boids) { Boid.GetComponent <Separate>().targets.Add(temp.GetComponent <NPCController>()); temp.GetComponent <Separate>().targets.Add(Boid.GetComponent <NPCController>()); } Boids.Add(temp); }
// Destroys Boid and change targets public void Reorganize(GameObject DeadBoid) { size--; Boids.Remove(DeadBoid); if (size != 0) { if (DeadBoid == leader) { leaderIndex = leader.GetComponent <PathFollow>().current; SetLeader(DeadBoid.GetComponent <NPCController>()); } SetNoTargets(DeadBoid.GetComponent <NPCController>()); } Destroy(DeadBoid); }
void SetupDefaultFlock() { for (int i = 0; i < mNumOfBoids; i++) { GameObject newAgent = Instantiate(mBoidPrefab, new Vector3(Random.Range(18, 22), Random.Range(18, 22), Random.Range(18, 22)), Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360))); newAgent.name = "Boid " + i; newAgent.transform.parent = mAgentContainer.transform; Boids agentScript = newAgent.GetComponent <Boids>(); agentScript.SetManager(this); agentScript.SetShouldFlock(true); AddBoid(agentScript); agentScript.SetTarget(mFlockTarget.transform.position); } }
List <Transform> GetNeighbors(Boids boid) { List <Transform> context = new List <Transform>(); Collider2D[] contextcolliders = Physics2D.OverlapCircleAll(boid.transform.position, neighborRadius); foreach (Collider2D other in contextcolliders) { if (other != boid.BoidCollider) { context.Add(other.transform); } } return(context); }
public void SetupAgentForSpawning(float health, float energy, Vector3 position) { if (mBoidController == null) { mBoidController = this.gameObject.GetComponent <Boids>(); } mBoidController.SetManager(GameConstants.Instance.BoidsManager); GameConstants.Instance.BoidsManager.AddBoid(mBoidController); mHealth = health; mEnergy = energy; transform.position = position; }
public void AddBoid(Boids argBoid) { if (argBoid == null) { return; } if (!mBoids.Contains(argBoid)) { mBoids.Add(argBoid); } if (argBoid.GetTarget() != mFlockTarget.transform.position) { argBoid.SetTarget(mFlockTarget.transform.position); } }
public void addAdditionalUpdateFrameBehavior(Boids boidsController) { if (wantsToLand) { RaycastHit hit; Vector3 origin = new Vector3(transform.position.x, rayDistance, transform.position.z); bool rayHit = Physics.Raycast(origin, new Vector3(0, -1, 0), out hit, rayDistance);//, landLayerMask);; bool foundValidLandingPoint = false; if (rayHit) { if (hit.point.y > boidsController.desiredLandingRange.x && hit.point.y < boidsController.desiredLandingRange.y) { foundValidLandingPoint = true; print("FOUND VALID LANDING POINT!!!! " + hit.transform); } } if (!isLanding && !isLanded && foundValidLandingPoint) { print("Landing Point found"); landingPoint = hit.point; isLanding = true; fixYValue = false; }/* else if (isLanding) { if (hit.distance < 0.2f) { isLanded = true; } } // take off...*/ } }