Game Development Reference
In-Depth Information
Creating a seeking agent
With the application of forces and maximum speed out of the way, we can create a moving
agent. Our seeking agent will calculate a seek force to their target position and will move to
a new target position when the agent reaches the target radius of the target.
First, we calculate the seek steering force to the target destination and apply the force with
the Agent_ApplyPhysicsSteeringForce function. Next, we call the
Agent_ClampHorizontalSpeed function on the agent in order to remove any excess
speed.
Additional debug information is drawn per frame in order to show you where the agent is
moving toward and how large the target radius is around the target. If the agent moves
within the target radius, a random target position is calculated and the agent starts seeking
toward their new target destination all over again:
Agent.lua :
require "AgentUtilities";
function Agent_Initialize(agent)
...
-- Assign a default target and acceptable target radius.
agent:SetTarget(Vector.new(50, 0, 0));
agent:SetTargetRadius(1.5);
end
function Agent_Update(agent, deltaTimeInMillis)
local destination = agent:GetTarget();
local deltaTimeInSeconds = deltaTimeInMillis / 1000;
local seekForce = agent:ForceToPosition(destination);
local targetRadius = agent:GetTargetRadius();
local radius = agent:GetRadius();
local position = agent:GetPosition();
-- Apply seeking force.
AgentUtilities_ApplyForce(
Search WWH ::




Custom Search