During my work on the Boids and Flocking Project, I developed a C++ implementation of the Boids algorithm to simulate flocking behavior using Unreal Engine. Below are some of the key achievements:
Below is a key code snippet demonstrating the core Boids algorithm implementation from the project:
#include "Boid.h"
// Update Boid position based on flocking behavior
void ABoid::UpdateBoid(float DeltaTime)
{
FVector Alignment = CalculateAlignment();
FVector Cohesion = CalculateCohesion();
FVector Separation = CalculateSeparation();
FVector Velocity = Alignment + Cohesion + Separation;
Velocity = Velocity.GetClampedToMaxSize(MaxSpeed);
FVector NewPosition = GetActorLocation() + (Velocity * DeltaTime);
SetActorLocation(NewPosition);
}
FVector ABoid::CalculateAlignment()
{
FVector Align = FVector::ZeroVector;
for (ABoid* Boid : NeighboringBoids)
{
Align += Boid->GetVelocity();
}
Align /= NeighboringBoids.Num();
Align = Align.GetClampedToMaxSize(MaxForce);
return Align;
}
FVector ABoid::CalculateCohesion()
{
FVector CenterOfMass = FVector::ZeroVector;
for (ABoid* Boid : NeighboringBoids)
{
CenterOfMass += Boid->GetActorLocation();
}
CenterOfMass /= NeighboringBoids.Num();
FVector CohesionForce = (CenterOfMass - GetActorLocation()).GetClampedToMaxSize(MaxForce);
return CohesionForce;
}
FVector ABoid::CalculateSeparation()
{
FVector SeparationForce = FVector::ZeroVector;
for (ABoid* Boid : NeighboringBoids)
{
FVector ToBoid = GetActorLocation() - Boid->GetActorLocation();
SeparationForce += ToBoid / ToBoid.SizeSquared();
}
SeparationForce = SeparationForce.GetClampedToMaxSize(MaxForce);
return SeparationForce;
}
Boids and Flocking Project is an innovative simulation project that features the implementation of the Boids algorithm to create realistic flocking behaviors in a 3D environment using Unreal Engine 5. The project showcases advanced AI techniques and efficient code structure.