private List <Agent> FindNeighbors(List <Agent> Agents) { List <Agent> nearby = new List <Agent>(); float a1 = -FlockAngle; float a2 = FlockAngle; Vector2 dir = FlockTools.GetSafeNormal(Velocity); foreach (Agent a in Agents) { if (AgentId != a.AgentId) { Vector2 toNeighbor = Vector2.Subtract(a.Position, Position); float dsq = toNeighbor.LengthSquared(); if (dsq < FlockDistanceSqared) { toNeighbor.Normalize(); float dotProduct = Vector2.Dot(dir, toNeighbor); float theta = (float)Math.Acos(dotProduct); if (theta < FlockAngle) { nearby.Add(a); } } } } NumNeighbors = nearby.Count; return(nearby); }
void Flit(float CurrentTime, float DeltaTime) { //POrientation = MathHelper.WrapAngle(POrientation); PerlinBeat = MathHelper.Clamp(PerlinBeat + FlockTools.GetRandomFloat(-0.05f, 0.05f), -1f, 1f); float perlinR = (Noise.GetNoise(CurrentTime * 100, 0, 0)) * DeltaTime * PerlinBeat; PTheta = MathHelper.WrapAngle(PTheta + perlinR); POrientation += perlinR; }
public Agent() { AgentId = CurrentAgentId++; MaxForceSqared = MaxForce * MaxForce; MaxSpeedSquared = MaxSpeed * MaxSpeed; Position = new Vector2(FlockTools.GetRandomFloat(SirFlocksalotGame.ScreenSize.X), FlockTools.GetRandomFloat(SirFlocksalotGame.ScreenSize.Y)); float QuarterSpeed = MaxSpeed * 0.25f; Velocity = FlockTools.GetRandomVector2(-QuarterSpeed, QuarterSpeed, -QuarterSpeed, QuarterSpeed); Orientation = Velocity.Heading(); }
public void CreateFlock(List <Texture2D> PetalTextures, Texture2D RogueTexture) { for (int i = 0; i < NumFlock; i++) { Agents.Add(new FlockAgent(FlockTools.Pick(PetalTextures))); } for (int i = 0; i < NumRogue; i++) { Agents.Add(new RogueAgent(RogueTexture)); } }
public virtual void Update(float CurrentTime, float DeltaTime, float TimeModifier, List <Agent> Agents) { FlockTools.Limit(ref Acceleration, MaxForceSqared, MaxForce); Velocity += Acceleration; FlockTools.Limit(ref Velocity, MaxSpeedSquared, MaxSpeed); if (Velocity.LengthSquared() > 1) { Orientation = Velocity.Heading(); } Position += Velocity * DeltaTime; FlockTools.WrapVector(ref Position, SirFlocksalotGame.ScreenSize.ToVector2(), 100); Acceleration *= 0.9f; }
public FlockAgent(Texture2D AgentTexture) : base() { Texture = AgentTexture; MaxForce = 10; MaxForceSqared = MaxForce * MaxForce; FlockDistance = 80 + FlockTools.GetRandomFloat(30.0f); FlockDistanceSqared = FlockDistance * FlockDistance; FlockAngle = (float)Math.PI - FlockTools.GetRandomFloat((float)Math.PI * 0.5f); CohesionWeight = 0.3f + FlockTools.GetRandomFloat(0.3f) - 0.1f; SeparationWeight = 0.2f + FlockTools.GetRandomFloat(0.25f) - 0.1f; AlignmentWeight = 0.3f + FlockTools.GetRandomFloat(0.25f) - 0.05f; PTheta = FlockTools.GetRandomFloat(MathHelper.TwoPi); PerlinBeat = FlockTools.GetRandomFloat(-0.01f, 0.01f); }
void Wander(float CurrentTime, float DeltaTime) { Vector2 forward_target = new Vector2((float)Math.Cos(Orientation), (float)Math.Sin(Orientation)); forward_target *= WanderDistance; WanderDelta = MathHelper.Clamp(WanderDelta + FlockTools.GetRandomFloat(-1, 1), -10, 10); float value = Noise.GetNoise(CurrentTime * WanderDelta * WanderRate, 0, 0) * WanderAmp; WanderTheta += MathHelper.WrapAngle(WanderTheta + value * DeltaTime); float x = WanderRadius * (float)Math.Cos(WanderTheta) - WanderRadius * (float)Math.Sin(WanderTheta); float y = WanderRadius * (float)Math.Cos(WanderTheta) + WanderRadius * (float)Math.Sin(WanderTheta); Vector2 wander_target = new Vector2(forward_target.X + x, forward_target.Y + y); wander_target.Normalize(); Acceleration += wander_target * WanderStrength; }
Vector2 Flock(List <Agent> Neighbors) { if (Neighbors.Count == 0) { return(Vector2.Zero); } Vector2 steer = Vector2.Zero; Vector2 alignment = Vector2.Zero; Vector2 separation = Vector2.Zero; Vector2 cohesion = Vector2.Zero; Vector2 cv = Vector2.Zero; foreach (Agent a in Neighbors) { float distSq = Vector2.DistanceSquared(Position, a.Position); float t = FlockTools.Map(distSq, 0, FlockDistanceSqared, 1, 0); Vector2 dir = Vector2.Multiply(a.Velocity, t); if (a.IsRogue) { steer += Seek(a.Position + a.Velocity * 10) * CohesionWeight; return(steer); } alignment += dir; Vector2 sep = Vector2.Subtract(Position, a.Position); float r = sep.LengthSquared(); sep.Normalize(); sep *= 1.0f / r; separation += sep; cv += a.Position; } alignment /= Neighbors.Count; alignment.Normalize(); steer += alignment * AlignmentWeight; cv /= Neighbors.Count; cohesion = Seek(cv); cohesion.Normalize(); steer += cohesion * CohesionWeight; separation.Normalize(); steer += separation * SeparationWeight; return(steer); }
private void CreateHistory() { for (int i = Past.Count - 1; i >= 0; i--) { Past[i].Color.A -= 5; if (Past[i].Color.A < 5) { Past.RemoveAt(i); } } if (Past.Count > 0) { int index = FlockTools.GetRandomInteger(Past.Count); Color PickedColor = FlockTools.Pick(new List <Color> { Color.DarkSeaGreen, Color.DarkTurquoise, Color.DarkRed, Color.LightYellow, Color.White, Color.FloralWhite }) * 0.5f; PickedColor.A = Past[index].Color.A; Past[index].Color = PickedColor; } Past.Add(new PastPosition() { Position = Position + FlockTools.GetRandomVector2(-2, 2, -2, 2), Color = Color.White }); }