Learning Outcome - Flocking Behaviour using differernt approach
About the Project :
I implemented Flocking Behaviour using Sphere Cast instead for Line tracing.
The result of behaviour will be similar to how flock work using line trace, just avoiding will not work well with other physics objects are placed in world.
But I have figured out to assist flocks. Red Colored Arrow at 0:13s indicates the assistants I added for guiding flock to the direction of arrow, to keep them inside the box.
Using Sphere Cast will use more memory than line trace, so it will have performace issues if we increase the flock numbers. This approach won't work in real game, but I just wanted to implement flock using different method.
Below are some details of flocking algorithm from above video.
Flock class : Driver class for all Flock Agents
void AFlock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
for(AFlockAgent* Agent : AllAgents)
{
//get all actor around "Agent" actor
TArray Context = GetNearByActors(Agent);
//calculating cohision,alignment,avoidance based on near by actors
FVector Move = BehaviourComponent->CalculateMove(Agent, Context, this);
Move *= DriveFactor;
//limit the speed of agent over maxspeed
if (Move.SizeSquared() > SquareMaxSpeed)
{
Move = Move.GetClampedToSize(0.0f, 1.0f) * MaxSpeed;
}
FRotator Rotate = BehaviourComponent->CalculateRotate(Agent, Context, this);
Agent->Move(Move, DeltaTime, Rotate);
}
}
//basic sphere overlap to find all actors inside sphere
TArray AFlock::GetNearByActors(class AFlockAgent* Agent)
{
TArray Context;
TArray ContextActors;
TArray> ObjectTypesForSphere;
ObjectTypesForSphere.Add(UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_WorldDynamic));
UClass* SeekAgent = AActor::StaticClass();
TArray IgnoreActors;
IgnoreActors.Add(Agent);
UKismetSystemLibrary::SphereOverlapActors(GetWorld(), Agent->GetActorLocation(), NeighbourRadius, ObjectTypesForSphere, SeekAgent, IgnoreActors, ContextActors);
for (AActor* CollisionActor : ContextActors)
{
Context.Add(CollisionActor->GetActorTransform());
}
return Context;
}
FlockAgent class : Agent class for Flock mesh and colision