So, we have our pseudocode, now you'll need to setup UnrealEngine. I'm going to assume you can manage this part, and create a new blank C++ project, and import the starter content package. On to the code. We're going to need a couple of classes
- BoidData
- UEBoidManager
- UEBoidParameters
BoidData.h
This contains the relevant state information for a single boid. It's position, and it's velocity.
struct UEBOIDS_API BoidData { FVector myPosition; FVector myVelocity; };
UEBoidManager.h
UCLASS(hidecategories = (Tags, Cooking, Actor, HLOD, Mobile, LOD)) class UEBOIDS_API ABoidManager : public AVolume { GENERATED_UCLASS_BODY() public: ABoidManager(); ~ABoidManager(); TArray<BoidData> myBoidData; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UEBoids") UStaticMesh* myMesh; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UEBoids") float myMeshScale = 0.01f; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UEBoids") FUEBoidParameters myParameters; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UEBoids") UInstancedStaticMeshComponent* myMeshComponent; void BeginPlay() override; void Tick(float DeltaSeconds) override; protected: FBox myBounds; FVector myAABBMin, myAABBMax; };
So, let's run through the members.
- myBoidData- A dynamic array of BoidData. We keep this data lean and close in an array to maximise cache efficiency
- myMesh - Lets us specify a static mesh to use for rendering the boids
- myMeshScale - Lets us scale the mesh
- myParameters - A struct of UPROPERTY members for setting parameters
- myMeshComponent - The InstancedStaticMeshComponent we're going to use to render the boids
- myBounds - An FBox we store to save the bounds of the volume
- myAABBMin, myAABBMax - Min and max extents of the Axis-Aligned Bounding Box for the volume. Makes for easier reading later.
UEBoidParameters.h
This struct stores some parameters we use to implement the boids algorithms
- myNumBoids- Pretty obvious, how many boids do we want in our flock!
- myAvoidRange - The distance the boids try to avoid other boids within
- myAvoidWeight - A weighting to apply to the avoidance
- myCohesionRange - The range within which boids will be considered for the cohesion algorithm
- myCohesionWeight - A weight to apply to the cohesion
- myAlignRange - The range within which boids will be considered for alignment
- myAlignWeight - A weight to apply to the alignment
- myVelocityMax - A maximum velocity to clamp the boids to