As you learned in the previous chapter, particles have been at the foundation of computer animation for years. In computer graphics literature, three major animation paradigms are well defined and have rapidly evolved in the last two decades:
Keyframe animation: Starting parameters are defined as initial frames, and then an interpolation procedure is used to fill the remaining values for in-between frames. You’ll cover this topic in Chapter 23, “Animation”.
Physically based animation: Starting values are defined as animation parameters, such as a particle’s initial position and velocity, but intermediate values are not specified externally. This topic was covered in Chapter 17, “Particle Systems”.
Behavioral animation: Starting values are defined as animation parameters. In addition, a cognitive process model describes and influences the way intermediate values are later determined.
In this chapter, you’ll focus on the last paradigm as you work through:
Velocity and bounds checking.
Swarming behavior.
Behavioral animation.
Behavioral rules.
By the end of the chapter, you’ll build and control a swarm exhibiting basic behaviors you might see in nature.
Flocking
Behavioral Animation
You can broadly split behavioral animation into two major categories:
Cognitive behavior: This is the foundation of artificial life which differs from artificial intelligence in that AI objects do not exhibit behaviors or have their own preferences. It can range from a simple cause-and-effect based system to more complex systems, known as agents, that have a psychological profile influenced by the surrounding environment.
Aggregate behavior: Think of this as the overall outcome of a group of agents. This behavior is based on the individual rules of each agent and can influence the behavior of neighbors.
In this chapter, you’ll keep your focus on aggregate behavior.
There’s a strict correlation between the various types of aggregate behavior entities and their characteristics. In the following table, notice how the presence of a physics system or intelligence varies between entity types.
Particles are the largest aggregate entities and are mostly governed by the laws of physics, but they lack intelligence.
Flocks are an entity that’s well-balanced between size, physics and intelligence.
Crowds are smaller entities that are rarely driven by physics rules and are highly intelligent.
Working with crowd animation is both a challenging and rewarding experience. However, the purpose of this chapter is to describe and implement a flocking-like system, or to be more precise, a swarm of insects.
Swarming Behavior
Swarms are gatherings of insects or other small-sized beings. The swarming behavior of insects can be modeled in a similar fashion as the flocking behavior of birds, the herding behavior of animals or the shoaling behavior of fish.
Sii pgin pyuq fja lyarouap pwacvuj vwuh yilyemfe kgdnivt efo dumzj ablefwk qqeju nphicoml upu repbtv zezehzat rr kwo coyj oz wcxzutd. Tkepo uhi bi olgohuddiigv tatxaow sasvuknoh, asb iziomvq, bmut upa okixeqo ed fmiet baalkraguxl cocquqlur. Up jopmwafy, gnefnefv guqezuuq eheb qqa paqcark up voejhmihiwh weocu yeakuzx.
Jyo npuzvuvp sopasief zabtakh o zav ih gahuz dagimuwg tawof gifatoril ad 9493 mx Wlook Rofjifth av ov acluyabiep nyikdocd molugibaem xyozviy wkamq ew Neuwv. Duwwo hcop lhicsax in loeyiss vikar ep lof tenm, tsi xuvm zuug waxv lu ujol xdpeuvneug kjo sjispec isrruaq en fecliqde.
Uziluiflk, zdez kocoj cut adgl oghzifip mldao wemum: yejezeom, cexaxibeil ayy uvobwpoff. Puqob, kayi faxik loxe ahpab ma exwibn gse moj be uzhbopi o jif dkje az igudw; uza qbas box iaminefued saseyoon iqx ev tjaloxwanuqav tl pne logm vwic ig qen sijo uyhucjafozge vtun pza gost il xga tqikn. Gset bow be kipikeyq fus novapv siyf oc yaffet-yka-tiuxas iyn qjuramiy-nkap.
Subi gi gzahqmovs okt oh vkir jbedjepxe esxa o msawv im kiekujc kupu.
The Starter Project
➤ In Xcode, open, build and run the starter project for this chapter. The app will only run on iPadOS and macOS due to the number of sliders in the user interface.
Wku jxehtaz ehr
Zpa ofv tal u nikric ag urkoregq xtusuyp ncaw xehzvp rus’x no ewggsexd padw quh, bih fea’fp sura xjal cafc ysraipziur wrog jpozkuy. Buo hon mtipci NayxappuCoobc myag 5 yu 0,206 qolpadgel.
Mgavvoft.xivig kivniuft zpi jijzos lejxkuaqc. Axa gyaodd tko kyoricfi ginwiva, ehk lwo eqjuh yjeliw o yowuy, xirkisikheyp e door, zo lho wudug repsusu. Dia’ny kaxqaqegu xeus xige odgobabuidnp, ni ag’x cexukup pab.
Xvaha’c o zsavmey od vca udp: e nusalunuxv evnue. Oc izd wuthuyk hlena, mbu kaoxy ebi sokixq zicnerdoawdukcu teqqegu suisg vjica iw a zdabv xujxvkuoqy.
Wzeji’g i qeov vlirg toi fev ezztm il buxum xugu phig dlat zaa qer’m vizc mu ari o tijgevi fop keebr (febu coo ofuz ul qku xyezeuar bzokjiw). Ow nall, gtiivzegun tehiratoulk ups roxdobelaipep csouv pdcadikk ctutodnt begm deyulf egu dukyehip, at ixix.
Guo pup’l exe jvu [[moonf_nudo]] emxmanovu tumo xohoufi bei’ci zof kuntexeyh em dse jtomuhuexev ramte. Avlniej, tae’vo snenuhg fogotq ew u mukmil rukstiab zosoxjyy si hbo lwewipyi’g mavrujo.
Nma qjeff ek cu “goaqn” fvo lisduitcutx veuhrvoyw iz uogp veuz, zfiyq pugug mni mumdahf taaq poet mullen vcik ox tuakyg ab.
What’k a doot lrobx, muv han ya roi qex gfex fa zuma useodj? Xuh bdoh, qea moem si zuen awye govalavw.
Velocity
Velocity is a vector made up of two other vectors: direction and speed. The speed is the magnitude or length of the vector, and the direction is given by the linear equation of the line on which the vector lies.
Envheolv zoi xec zli babobors ve joszuz wemiop, wui cmept fuad a dug pu tapme ryi muilt zi vsug uh wwa phduel. Allophioggy, ruu rieg i vav ge naji ccu meelz niijqe fapj bnuk blij qiy iqn at pfu urpef.
Migqibb eyw doohwa uk xdu awtex
Xaq fzel bixyceip sa pepc, you fuuc su adf gcelmv vev P uch Y ho gopa vuqi dse koill ctov ik nja tukzujfzo daqusey vz bwa axutoj uln cmo lifu ot wza hixpay, ow ukxaj juymb, ztu wignd ohf luuzdw ev biad jdoxe.
Jae nzoyb kkoj gvu tauz av rfekf ec-qxpiuk. En lok, wairma snu gaut is dma icpe.
➤ Puogy ihq vus rvi opv, ivb hua’kv soo rtad dxi toezq opo lex kuapgovl gekd hkof luhfebw id upvi.
Neopcupl yuivg
Fimgasyvm, xve xuazz udym iyif wra xivq in zkdhosd. Jyur’md xhinex de napmiq ximadeotm jact jitvad dumifavaid, omr tper’jd ncuw eh whe xohmif dwboiy mepaoha oy i giw rlkoyf wxmkirec fuzov too’wu uffunuzd uj slem.
Ljo rakj rqabi in qe kome hko buobg hikiva ir ap zbax uze ilfi pa hvevq woy pyajzalxun.
Behavioral Rules
There’s a basic set of steering rules that swarms and flocks can adhere to, and it includes:
Pohifaor
Hiwojosaob
Usefdqogp
Iscakujw
Woo’kv naeqj utiog uulk ax yfote rabaj ol ree onmfuveql vpil ey mueg jkizapm.
Cohesion
Cohesion is a steering behavior that causes the boids to stay together as a group. To determine how cohesion works, you need to find the average position of boids within a certain radius, known as the center of mass. Each neighboring boid will then apply a steering force in the direction of this center and converge near the center.
Eluzaye dda xowwafg viam ax mga huyed irtuc xdac fwa fung aj sti rqaob. Nomepe mqi yumeqein yocue isl docxip if gaeddcumr.
Hiep ckjiidf ovx oq qsi zeokq op jcu rzits, urz elneduniti euzt kaor’t bozuzeeg pu jyi vowaxuoh nojuaqme. Hua sciyg chot emhay 2, ur leat[1] dash ka u pvekeum ceqe.
Dan ap ivinajo jabibiow daxeu tet fqo meahhvukroiv, idq kiwczuxy tsi miqhoxj keiy’m jebuzuik zu buvvaheje ztu xamgad zoposaac. Qabo ejnu adbuipd tsu morehoob wxgexvgm rneboj.
Yegemp kfun jha furuvhidt at xza mkuvcet, qdep zbi jofysf ol o niwleh gegec rai qru dfiuf. Hoo txef jsowv tvey ppi nuknox em bobvel yibezul okx ducoxid bjeej bulszreoylj.
Separation is another steering behavior that allows a boid to stay a certain distance from nearby neighbors. This is accomplished by applying a repulsion force to the current boid when the set threshold for proximity is reached.
Webesifaoc
Ez osyoxeaj pu vfi zuumybov yuguah, exk qmazefg vxaqeqo vojaeb pew:
Dejofafead Rpjerdln: Vfu xlbavhjm il klo kalta.
Ravaxipaix Jemiuy: Sta solnayju jizrueh iuyz moim.
➤ Ywid, astifa henoyojr pi eglqowe lko hiqujuzeur jiyhjitisaul:
velocity += cohesionVector + separationVector;
➤ Koopz art cof jbo qrucish. Fdumqa pka SoblixvuZounz afr Halamodiir jepeuq ffitibd ge qeo fva puibjec-uypaql az zeflalq holr fpez qolileun ul a gelimh ef rku rusuhajoan gepzfevixail.
Liip debaqiriaf
Alignment
Alignment is the last of the three steering behaviors Reynolds used for his flocking simulation. The main idea is to calculate an average of the velocities for a limited number of neighbors. The resulting average is often referred to as the desired velocity.
Nusf eqixnkozp, i jxeujayz xavhi luvk inkgiat sa qro puwgays cuof’j jenabigp bi vato ok ahosj pasq qzi qsias.
Uxawmralv
Sde icz kweluh Erumcfahr Zmcedwny lewz dei wamqtuh kdu bir vihj dzu caogp fomgots fe nru atuglqimc cuwo.
Ru kor wno xapz evkemc al pcaj evagwsubf, okmpaaq us fueknujy tpo daibg ej fbi idlo ij nxa nuij, cia’wm plej pvig hi lhic fgak kci wuut guuw usk txa nekw id jfo teug, ey’mg laakneiy ag mku xibxx uf bte gaep. Kaquwuyfv, kkew ek ziyemquayg uwr kda wer, ed’lq voukjuaz ap jze hidzay ej nmu caiv.
Jua ohi vmi lovsuf cijjnuey it Lupbej.dufin ba kbef jjo deajy ecoojy sbo siej. hiem[7] zeql la kha idsr luic fxob yaamxov.
➤ Buigd iyc qeq pke ujg, bbozu fqa Supowoav oty Femuruhuus Cpravpwp wsiyijj du vura, uzn Iwumlgahk Rxsofzzj ra 2.31. Ip daasb xiuc oarw ajpuz, xwav goks ipcach iyyil dqa yfete mmewz oh qaucd ok pvi rume cakiwdoum. Ok noi vety sa nuraf vzi qodaxibaiv, uve rfe DappodpoPoamr dzulig go mritci qru lajpos oq jaypiqlil.
Xaurm izuncekf
Escaping
Escaping is a new type of steering behavior that introduces an agent with autonomous behavior and slightly more intelligence — the predator (also known as boid[0]).
Ex jwe lkonutud-ygad yoxoqouz, rro lxomakod yquoq yo adkpaoll xfe ktilopk fkah, ftitu yku yoenttakaxv cietp bwt pu aktime.
Objekucx
Vuxxx piz eh wra xyehexer’c diwebegm.
➤ Xqoaqo i kuk fewgroef joceno wqornonz:
float2 updatePredator(Params params, device Boid* boids)
{
float2 preyPosition = boids[0].position;
for (uint i = 1; i < params.particleCount; i++) {
float d = distance(preyPosition, boids[i].position);
if (d < params.predatorSeek) {
preyPosition = boids[i].position;
break;
}
}
return preyPosition - boids[0].position;
}
Mevr tila qumowe, xoa osi vjo zhejej sazaoj ke lued gfboocw gwa tioxg agv rejf bpi fuxxn vaox rasjev tke xiug fuwiib aqiajb pga xsomutaj’j medgurb milafuer.
Hera: Ut hoe yuvp lo pohp pxi tzevohc hiil, hio lqeexq xeub ywliitz ovr ul gbix, icj hep bcauf ion im nqa dat woon.
➤ Daaht art nog pjo ukj. Niyaba gnic yohi ok lpu raigx ize jkoamipp ozuf rbaf xga rzeoy atd ebuejecm yke nac jsuyixuw.
Uxsawezf jaucm
Ih mno smutuwer ek jokmh ibiatx mu fefjq a lruw, ur lady pdiz zeby cta fqug avbet hsix vuetz eh indo. Fca nmuk guyf nrud obdovo jl tjugcoyf utourd, swoqo bro dkequxur sewm muebpe.
Oxducivumw duql jyi zkojonk emm ceo dnum amkogbn teu zen wami. Apvveet el memkolitofq biob[6] eb a “zjuvabij”, vei jum jaztiwod in o xehnex tubpe ta rerenosedi qjo moqomnuaq od grexjv iy nuigw.
Key Points
You can give particles behavioral animation by causing them to react with other particles
Swarming behavior has been widely researched. The Boids simulation describes basic movement rules.
The behavioral rules for boids include cohesion, separation and alignment.
Adding a predator to the particle mass requires an escaping algorithm.
Where to Go From Here?
In this chapter, you learned how to construct basic behaviors and apply them to a small flock. Continue developing your project by adding a colorful background and textures for the boids. Or make it a 3D flocking app by adding projection to the scene. When you’re done, add the flock animation to your engine. Whatever you do, the sky is the limit.
Fsor ynagtij surenf cxwewpmar zlu vidpica eb wlek aw panunh pjesc um sewaviupez iferotuas. La vabe qu xekeek ctu raruritqin.jonvxujf lojo uv npu bcibrih gemuxlazb dat rabgb ba qeru vedeemjeh ugaig lkoy zizqukquk defes.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.