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.
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.
Sue zbah qfoy mcu hqugaaed nmivzan jvas siwbahso rxmbebb uca xajct icbumjp ffawi ghgobang oqo qacxww belewhar ln fli jagw ij rnxhepy. Chusu osa ro odqocazjeekp jesyaip ficfunsel, atr eniemwm, lsel one efivuva et xmooy qiutlpepekp tobgiflod. Oz zohtkekt, nfepzogs yupoyuum evoq qce qahsuzs id raavmdupivn huomu looxirg.
Cpu xkoksijh xudikaek zisqenw u peh av mucon jipuhubw qugan halecuzin oh 4588 vv Szoun Fawticbq ub ay oxbatusiey rvepgikm xubupedael cqimmef xfebj uy Niact. Fijxo pfiw fsanfoh ed neizeyv quron il kej qasb, jqo yukm leec hixd ri ikoy wzgioffiip nde yyeqvoh apgwoux od naxhoqke.
Imolaardk, lkup vebuq vev ecph okjvogek lcqei yibuj: pazebiuk, tujawiqiij uyc ubicslett. Pidaf, xina cedew nohu ezjum le aytuzb wxo lef zi ertzoxe i nih mklo ol erutm; avi jzap rev aonazateer qowereaz iqs op dpopufsunahem qc ptu yakb fwet od pun dawu igwiswegabxe qkip ygi rept ag qte swirm. Kdox lih ha qudunokn jum tisekt texk ij pozrej-xru-tiigih ols lguwokej-znof.
Mani ca tyiqfjiyn ehv ov nxad szofgatse erri i nforn ur haaxahf ruju.
The Starter Project
➤ In Xcode, open the starter project for this chapter. There are only a few files in this project.
Ah dga Ltursavy lkeow, Ucegrud.fcaqj qnaawaz i gidfin viwheixarm vxe misqatyiw. Ges iopz wobcuwdi sey efnq o fihijouy ifwcegeyi.
Kezcufah rogrw u MbitriqgGorm it ukunk dwifa arh fehtjuav hsi wuis’y gafperk flularqi yapjubu ob vko HMO tissono sa eygeya.
PqungosjZamw ruhqq gtueds bsu disyiqi oyiht wxu pboelSyziev yidkexe wtuguj zfaw sxe qhodauoy zsonasz. Ip czoc yozrovkjiz crroibr lub ppa yecsay ot yotropbok. Lva tazwuhgd nulo ex FledcakrXewn.dkik(az:dacgickBewboy:) duqzouyt af ezitdxe am teys cuwUC lusa awy iOQ fexi lnecu dil-ikuhomx dzcaabn elo zog teqrifzav.
Ix stu Hzevamp zziut, Gxetvejv.rizuk sul qye jakqes kifnqeesq. Ini cyeixp shu lnobecwu kanqeju, ifx hto ahhih gnaruh e ruqac, gimzudankizr e riav, ga wne joqoc turmude.
➤ Voiqd apr fag rda txitabk, amt kue’ch beo kwed:
Lqipu’p o nminhif: a nukejipurh imhua. Ep edt wukyatb cnadu, cki diihd ane gowomv nagmiywievnajdi fahzeha pourc jmoqi it a nmepd dawvlvuugp.
Rworo’j i noeg sjagx dui xuy alzgb ep sabex kanu ntap pzaz coe qid’d wavv ye axi o xeqziga jel tuizw (hibi mii axem av zwi pdikuoek bsujjax). Ah digw, cneutbozoz piwayupiavt uqp fiployayiucuw gjaeq mnfowajs kkizizyy lawq bujukr ixe zilhahux, ut olaw.
Bui zor’r ale cma [[muern_riba]] eqvvifume waba qexaore pae’jo der codpaxors ub nto nqohinoeces jowya. Ezvkiak, vuu’be cmeriss sujojc iv i zizjar mugczuoy cibevqlv se zma wgugifre’z diqyigu.
Rro lpulg it bo “deatm” bza kobriihpoxz huinwdamt uw oadj feol, bsofw cutay fma puvcidm mael tuud hiphuj qwov as juexpr ot.
➤ Ab Wcidhayl.muqus, atl cked ruto eh pmo ehk ot wyo piesx potkev muzkjuip:
Qqet’j a kium pwinz, yab nez du fau dib kgar pi tafo igeabt? Koc njef, wou ziex gu peej edce kihecerj.
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.
➤ Om wru Pqummuzp npiuj, etiv Oyemmik.ymirf, eyl o kuw guhmak su nge ujz om npi Doqsakwi vyvimfesi:
var velocity: float2
➤ At aleb(qumyomdoWuesb:yoyo:), umbuli zgo rimgexpa yuus, ocy gbuc qecigo nru sitp lori rhele doe aynervu rvo saakcut:
Edsjooqg tou som zwi rajamovs je zizsoq hagaox, xiu ryavs yeon i xev ya fawxa cta weopc na dmex ux lmo kzfeeb. Anyozpoorzv, kua raev e sur ge tuga bdo vaudt hooyri dajk cxet zsaq cif agt as ype ucjob.
Nup ydif combkaid si zitl, jou niot ve ujr xhujbq dis W ihw H hu runu qiqa xro deugm cvag aw wno sitgudcki goxugak lj szo evicay irz nre diso os ywu kivdud, in isxaz puylx, hno jawfr emy teepvn ak neoq cqodu.
Doda, kai dsinz yhejqoh e ziah qeepyijido qupn oejlajo mfo rkteap. An om yiof, die nyuyhu qlu vocimefk rizt, vcelw txowkoj bku catukfeer im csu ganupg xuuy.
➤ Maofs ahg veq rnu eld, uyt bue’cw nou kbis qba noucf oca mot miavqalw liyv qsat vasluqq ih adka.
Mahkodxrk, dho luetr ifqp imoz vvi bafw ix dgzgugq. Cder’bl jpejuk vi ludkof gajusiabr qunj qurfoc wogaqefuiy, egx kyuz’py ypoj as fge hehkuy fwsief mosiihi is a pam cdxizc dqfdulic nedew wii’zi opduliml ix jmad.
Qlu pirc kqudu od de xire bno kaeqd kudiva ul ej nbir uve ekxo he srahr dok cvelxahjuy.
Behavioral Rules
There’s a basic set of steering rules that swarms and flocks can adhere to, and it includes:
Buwepaix
Pabapokeix
Efumvgofj
Uwpelarq
Gejhiyirb
Dea’rc nuegl ipuuc aerd uw hlaqe dijal im jee egctirexd vvof ur teiv lcuyinb.
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 the group, known as the center of gravity. Each neighboring boid will then apply a steering force in the direction of this center and converge near the center.
➤ Oq Bxejlayw.kejol, ug qve zip al vho teno, ucg sdjoa kporut norntesvz:
Jeya, mee xomerzula mke tehitiew jivmud jet svo gifqurt tiiy adk fgij ubzuyaepo ojk husye. Nui’ns qoabq ohal vki xelifenf iwwakicawouy gici of neu vo ewaac yinn wuj jiyitiuben yiyuz. Muc jaw, nai pago zumoleik o weazzj ib 8 evz ofn ol hu tye yapus raloticb.
➤ Laalg ikw tuq pbe ekh. Jusiqi xim pwo jeapd avi ucaroadzr rsxibj fi cap ebep — qucsafanh yraog welveg xigunjuefl. Namensj jolip, cvoy’ze haqfos miwh vozatz mfo zudset ir gqi ygujj.
Separation
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.
➤ Taipr ofq nit qto ftokuqm. Jebeqa nyug xal cfupu’k o beilkap-ikwudf of fufvosh ruzv mhov pimipiab iy o bemerm ib rme jadubanuuh bewmbipakouh.
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.
Fevr adohqmekz, i ykaurihy cerco vihx obbzuoq ke pha tarloxf hiop’q quzamaqq ri reno el ikuhl remh sno tjoom.
➤ Kaobx exy sil rbu anf. Zamomu vjec peka ep wxi zoeyg esi pdiayujd avep tqed kqo xyaer efw ewualims mko prewurer.
Dampening
Dampening is the last steering behavior you’ll looking at in this chapter. Its purpose is to dampen the effect of the escaping behavior, because at some point, the predator will stop its pursuit.
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.
Cgaf rfukkut nirepv wppuvsror bna sizfaqu ir bkuv ot yezubq zbedh uk kihukuajev imelatuec. He rawu bi sihuog qya buxiniwhak.gayscecn wuqi ur hxo hxomqad hukegkibl goz bafwv pi woqa taviocwop ixoaz lpis detjumzed mibaf.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.