Introduction
In this tutorial, I will cover the three main rules used to simulate flocking and explain how to implement each one. Before we begin, here’s some terminology I’ll be using:- Agent: A single entity or character.
- Velocity vector: An agent’s current velocity.
- Neighborhood: A certain area around the agent, used to look for other agents.
- Resultant: The vector obtained from the calculations of the rule.
The full source code for this demo can be downloaded here, so this article will only highlight the most important aspects of the implementation. Feel free to download the source if you wish to learn more.
Alignment
Image adapted from Craig Reynolds’ article
Alignment is a behavior that causes a particular agent to line up with agents close by. First, we’ll make a function that takes an agent and returns a velocity vector. public function computeAlignment(myAgent:Agent):Point { }We’ll need two variables: one for storing the vector we’ll compute, and another for keeping track of the number of neighbors of the agent.
var v:Point = new Point(); var neighborCount = 0;[...]
Read more: The Three Simple Rules of Flocking Behaviors: Alignment, Cohesion, and Separation
No comments:
Post a Comment