Assignment Goals
The primary goals of this assignment are:
- Use a template code project.
- Extend a UE4 Pawn with C++ code to add behavior.
- Use delegate functions to set up collision response.
For this assignment, you'll be creating C++ Pawn, which is UE4's base class for anything that can be controlled by either a user or AI in a scene. Your pawn will be a rolling ball following a simple Boid behavioral model. You'll use an expanded collision volume to detect other nearby Boids, and based on their positions and velocities attempt to follow the avoidance, alignment and cohesion rules. There will also be one user-controlled pawn that you can use to influence flock behavior.
Details
Once again, this assignment leaves a little more of function discovery and engine navigation to you as compared to the previous assignments.
Create a project
- This time, create a C++ project using the Rolling template (with no starter content) called
assn3
, at the top level of your git repository - This project already has a C++ actor for user behavior. Look at its code in
assn3Ball.h
andassn3Ball.cpp
, and experiment a little with how the ball moves when you play the level.
C++ pawn
- Create a new C++ pawn.
- Give it a
UStaticMeshComponent
and copy the initialization of both the mesh and physics parameters from the player pawn code. - Add a new
USphereComponent
to detect nearby neighbors. Set the radius to 750 (you can tweak this later if you want). Don't forget to attach to the Pawn'sRootComponent
.- You can see the collision shape and size by turning on "Collision" in the main viewport "Show" menu
Tracking neighbors
- Create a
TSet<AActor*>
in your AI pawn to hold the list of neighboring actors that overlap yourUSphereComponent
.- This is transient data, and should not be serialized with your pawn. Either don't mark it as a UPROPERTY, or if you do to make it visible for debugging, remember to include the Transient property.
- Initialize this set in your
BeginPlay()
function using theUSphereComponent
'sGetOverlappingActors()
function.- Note that each AI pawn will also appear in its own list of neighbors.
- It will also include the player pawn, so you can herd your flock by driving the player pawn around the scene.
- Create
OnComponentBeginOverlap
andOnComponentEndOverlap
delegate functions to add actors to the set as they come in range, and remove them when they leave. Register them with theUSphereComponent
usingAddDynamic()
in your AI Pawn's constructor.- You can find the signature for these in delegate declarations in
PrimitiveComponent.h
. - You can find a few examples creating and registering
ComponentBeginOverlap
delgegate functions in the engine code.
- You can find the signature for these in delegate declarations in
Neighbor forces
- In your AI Pawn
Tick()
function, loop through the actors in theTSet
to drive the pawn direction. - Add force scale properties to your pawn class for each of the avoidance, cohesion, and alignment forces to control how strong each should be.
- You will need to tune these individually. Too small, and you won't get any motion at all. Too large, and the balls will fly off of the map.
- Use
AddImpulse()
to apply the forces- Look at the player controlled ball for an example.
- Add the forces one at a time to debug the force direction and scale.
- Remove the Z component from any force, you apply.
- For the avoidance force, scale the vector to each neighbor by 1/length3 (in addition to the avoidance scale property) so the closest pawn will push hardest
- For cohesion, apply a force toward the average position of all of the neighbors.
- For the alignment, apply a force in the direction of the difference between the average velocity of the neighbors and your velocity.
Grad Students
Grad students do the regular assignment first, then switch to using AddTorque() like the player ball, rather than AddImpulse(). Since the Torque vector is along the axis of rotation, it should be perpendicular to the direction you want to go.
Extra Credit
For up to 20 points of extra credit, only available if you submit by the original due date, add a capsule as a second neighbor detection object. The capsule should have the pawn at one end, have its long axis aligned with the ball's current direction of motion, and have its length scaled by the current velocity. Use this to modify the avoidance force to only consider other pawns that are in the direction of travel and likely to be hit if you don't steer to avoid them.
Submission
- For full credit, you must commit multiple times during your development.
- Use Sequencer to record behavior of at least a dozen AI balls.
- Add an
assn3.txt
to the top directory. Tell us what works and what doesn't, and anything else you think we should know for grading. Include a link to video demonstrating your project. - Push to your repository, and tag your final commit with an
assn3
tag.