Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Doing a flocking algorithm

$
0
0
I am attempting to do a flocking algorithm for my game and was given a sample piece to finish if I wanted to use it. The problem is that I have seen several examples of flocking algorithms on the forums and online but none seem to match what I was given. The comments have me a little perplexed as to what I should do. If there is a similar code to this that would be great but this one has me stumped and I don't know where to begin. I also need to know if this will be acceptable for moving enemies around and towards the player or if I have to make a separate move script for my enemies. This all has to go into a list and I am terrible when it comes to lists. public class Boid { private Vector3 pos, vel; private float rotation; // If it's too close to any of it's neighbors, it calculates an // "avoidance" vector and adds it to its velocity public void avoidNeighbors (List bList) { // 1) Create an "avoidanceVector" that's a Vector3 // 2) Create an int to determine how many neighbors are around this boid (like neighbor count) // 3) Loop through each boid in boidList remembering to exclude this boid // 4) If we're too close (e.g. 50 pixels) - use Vector3.Distance passing it this position and b's position // 5)increment the neighbor count. // 6) update the avoidance vector by += the difference (Vector3.Subtract) between this.pos and b's position // 7) If the neighbor count is > 0 // 8) Change the velocity of this boid by += the avoidanceVector/5000; } public void Update ( Vector3 playerPos, List bList) { // Step 1 - avoid your neighbors // Step 2 - move towards the player by calculating the vector between the Boid and the player Vector3 diff = Vector3.Subtract (playerPos, pos); if (diff.Length () > 1) { vel += Vector3.Normalize (diff) / 50; } // Calculate the rotation angle of this Boid (for drawing) using Atan2 if ((vel.X != 0) || (vel.Y != 0)) { rotation = FMath.Atan2 (vel.Y, vel.X); } // Slow this Boid down if it's going too fast float velLength = vel.Length (); if (velLength > 3.0f) { vel = vel.Normalize (); vel *= 3.0f; } // Update the position of the Boid based on its velocity pos += vel; transform.Position = pos; transform.Rotation = rotation; } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>