Game Development Reference
In-Depth Information
agent:GetMaxForce();
-- Apply force to the physics representation.
agent:ApplyForce(steeringForce);
-- Newtons(kg*m/s^2) divided by mass(kg) results in
-- acceleration(m/s^2).
local acceleration = steeringForce / agent:GetMass();
-- Velocity is measured in meters per second(m/s).
local currentVelocity = agent:GetVelocity();
-- Acceleration(m/s^2) multiplied by seconds results in
-- velocity(m/s).
local newVelocity =
currentVelocity + (acceleration *
deltaTimeInSeconds);
-- Zero out any pitch changes to keep the Agent upright.
-- NOTE: This implies that agents can immediately turn
in any
-- direction.
newVelocity.y = 0;
-- Point the agent in the direction of movement.
agent:SetForward(newVelocity);
end
Before applying any steering force, we remove any steering force in the y axis. If our
agents can fly, this would be undesirable, but as the primary type of agents the sandbox
simulates are humanoid, humans can't move in the y axis unless they are jumping. Next,
we normalize the steering force to a unit vector so that we can scale the steering force by
the agent's maximum allowed force.
Tip
Using a maximum force for all steering calculations isn't required, but it produces desir-
able results. Feel free to play with how forces are applied to agents in order to see differ-
ent types of steering behavior.
Search WWH ::




Custom Search