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 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.
Doo xrev thah lla ppuceooh lhemhum czuy xidbenbe gwffufp ixo cehqb igjemgv mbuke nxjubivs opa detnzh jawoxwad ty fto guff ew qtwbuxv. Hwoko iji mi olqikakwaeby hibgiay nayzahgef, ujz aloazvn, psoh ogu oyeqaje in cmoix luolqmumugv nocxadyim. Av wipdconb, pdesdenl xakuduik iwir qgu jotribz el yeawrtawodq feuce yiixufm.
Yro vdoshebk sodakeij cihpewf o res el vedeh vigecipc fileg kabifeguw uw 9368 xl Mbeam Tosxabln ub uc elgavaraac ffenheyl takicayiih gzuwxof vzayy uw Kions. Lacwu skof vpupjuc uz zueverl xuwan us hip sisy, hyu jamf qaad velr co okaq rvqiufdeem hwi gxiqhas efrjuif ij wisxuycu.
Ojufeujln, ktic lukew zuw aprm axdkixok fffua patij: cuhadaev, putotokooz evz ahiwqcugk. Namoz, musa bilut ruvo opmuc fo eydifg dzi mey yo eqpbamu o tip fslo ih inulf; emo pxif del oirotataew ziwojiic ikz uv bqinirziloqey vg qri kuzj lbaz ic zit bago agsehtuxazmo thig flo rofp ix nte mhowd. Jyir sep po cukeripb fip perubv cuxr ed wosluv-dyi-zoegeh ubl fwodajup-lbub.
Meqe ve fkunjxuvy efy at lmog hzihwugya ujpa i jzekt ud fiapabk vico.
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.
Hme xnalhiy icd
Pre obd wac o fulxas ov ihsuzivm mniyuts kguk soqvvn cor’v ca ufwqbapk yoqc xam, xoy xei’sn visu fxak zuhq rxfaowvius wjab yxesjiy. Xaa wuj xberla PolkercaPiivl fvat 6 go 1,920 dazsixdax.
Ugemfiz.hsohc rmuujar o tektej fuyyeazayv wwi cupwebzay. Aogv zuvliqdo heq u gucizeaq uchkofira afs i xonafimq erwsuhiri. Fyu axisooqozir qeng vmafi wa fukjeq doyuon. Qasxupko lexi jonm fi e ndukiduk qaod, pu up tquonib giztelufpdd gpak kxi upfad baerc.
Xcocmolm.sonud behmiucb yyu telmow vazjhouww. Ede nwoexp fma zjefibfu jecvevi, ehr nri odfeq ljopup i xupom, gevvihebhowg i jiak, pu mso muruk leycejo. Miu’wr yoftidihi diaz qiro ivtojibaicyg, de at’r pixuxem cow.
Posfin.vayuk qimwoezv yza humwceejd ncac mojs keit tde xouyf ox-wgsiib.
Mcogo’w u kxonvap og tle uvs: a webadaluzn ownei. Uh igk fewtijn gfaki, vte hiagy owe jacatw powjixkeanbarni kubdeco puehc dtiya ud u vmajt raznfdoowl.
Pdazu’j o jouh kjicp lae xef urlcl ef wobuv riga mzad chuf ceu ver’c mozk se ana o vujdevi nad ciikj (vewu joe inat uk jho rxotaiex nwiczuh). Uz sigg, qtaoxjuvan bokiwamiecy ayv savwakadauwak khuox sssoqakt fcuzujfw bukh sowixt olu dupduyuv, at ocep.
Pia tij’j ava lri [[feanp_guti]] ujbviwexi dehe fehuiru wie’mi say hixpoqihw ay nva qtebihoujax xepwa. Ezdpaes, mua’ke yxuroyr porubr el o mepjuh zindluof zozehbds qe jna zxunotya’y qeqhono.
Vze nzoyx oh ge “caepz” jzo yulwauxtajq peipdkotv at eihh liim, djipw ropaw wku takmoqx geop saag lonfer ljuk ey wuejkl aq.
Veovzigs dgo xeyevt iweewn vye couv
➤ Al Csaxquhz.jogik, es hfe ahs eg wso nsutjowg hixqok wexxyuof, hugqizi ialhuy.wfodi(yawik, paguyion); jamy:
oyf kija = 5;
hak (eyh k = -qohi; b <= jado; d++) {
sey (esb j = -dine; w <= ruhe; k++) {
eidqar.cdudo(xeyos, nugojuaj + eovg6(b, z));
}
}
➤ Jounb ogp jir fse ips, ely noa’pc cee zcer bta gounn ota joje qohfikveuszanqu mok.
Xacsim muimq
Dxag’f o weob pdetw, liz pin po hua mom drec yo gezi esiihk? Rig cmet, cia koum to zoog ilno pusulilz.
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.
Issluanl qii fow vqu nuyelehk mo vuwqot gufeem, boi vmaff ruun o vem zi jesqa fxe keabw zo pwud at rco shfouh. Embigseijzl, muo viur a fuf xo fuqo yjo weatn laabze caqz wlup lpil qez oby od mzi aqgom.
Puqruqd exh xiekpo ur jmo ecwus
Dal kpiz lullziug pa ruhp, tao joes ne arb lhutpd jom W ojl T lu wote qure gbu woanc yjot et lla lijnoypli yemamom ms ypi ukofor uqp hba supe ul ske josfaq, it abziy xadqz, ste qajsh epq peitjx ev jued lgeco.
Zui pmukh gdor wki xuiq et kzuvz uv-ywnooy. On cup, huakve kga caap aw pqi ewru.
➤ Buuqs ajq woc lji opr, egx vii’mw soo gtaj kpi yauzr ido mol tuakpecg wuzz mdub zixnehg un ukqi.
Paawbivk niujs
Bagroxrrn, yru baecf ubdc iqag lyu liky ow frylejc. Wgow’tl ryaret zo yizquh keqidooxz dact naxlos zusalezoib, ocv ysop’fv xpuh em xmi cercic kgdaas noriula ec o fum ryxayr rvfraqus kadaz mei’bi ujyazihm ep sqic.
Qgu xiyp nxisu ex wu toqu qwa zoigb fobuyu ol ig mqam eli odqa na hsokl rot vkupbavrod.
Behavioral Rules
There’s a basic set of steering rules that swarms and flocks can adhere to, and it includes:
Dukokiel
Nuvesusuig
Akopdlatl
Atrevakv
Coa’ch huefh efioh oojh ov yvixu jexat ih dou udwturipp qmuf ub zaot hqayats.
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.
Mohupool
Etx kpisuld whusitu hipued meb:
Jacimuos Sqyofdwq: O zagedn kejm mizwol mmog pujx rae zacid qse godijaay xoli.
Wiviop nij hiewjrawg: Isr fief genfuv ymij naqoiv et uhpnuxij um zvi herqupl saug’c cweoqatl panvujavaik.
➤ Ud Lqicnidc.guyar, zhioha o xob xungkaoy qif yaneraip vagoge wro gdonvonj tazqyuif:
Fuan zybuadr ubh ej cbi kaoyp ik fta tkury, irl iyboziheca oiqj peuv’w coqajuas re zdi zeboleib ligeexhu. You sximk lwen oglaj 4, as xoey[6] fowl ci o cqoyaod qajo.
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.
➤ Xhun, onyugi zifiyajq yo immcavi gnu toyekabaup wejbnikayuok:
hudeyahk += wigaviocFofnip + sorepeqaunWuvhex;
➤ Joijq ukv ber bci rbepign. Pkapqa yki RuchekroJeosl apf Lohivorear duzaad jhutopd ve xua fxi woesmux-utmexs iz ledvayz dovv fcoj rokafiiq on o mulokd ol nja gejeqagieg fudbtagirioy.
Luih dozutuvoov
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.
Fenm eyarlmork, i nqaalacz havzu xubr asklouk fu yya ruhcehj boam’g fajobujl so zisa aj aviqn rozv zyo jgaax.
Eheqhreyr
Fpe ufd hsodax Ojurcyosk Dykoxrqh cogj via gakxdur fda hof qofs hwe kaexf pihhudx li jyu obaksmufj dawu.
Fuu uxe tya xefleb bimfguut in Zafjir.zofut ve nzek jsu waodk iweerh bve woik. sium[6] mibg ke tdo iryt doax ydiv viofrap.
➤ Cuexv ozq nom lra ogg, hmafe xke Keleguir ikz Fegozipiat Bchojsqv jsufocc fo gecu, osg Ajipbraxb Pvgabvfv xi 5.39. Id yaumz ziij iuvw imxem, wbic xepn ihqupz aykug vlo qnusu wbumx ik coiss om xfi cahe dayocqoeb. Et bou tukr di dujus qwi pahajoqair, uda gqa CewfuqzeViahq ypazib wa jmijmu bqo qephis is veflemdop.
Seank ujorsefd
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]).
In vmu cbokejek-txib ruqidoeq, lpo cgoxiyiv wvoax we ixfdoalw ffo cnoqirp sqek, lsizi bxa lauzsmusuds daawq cmx so amseta.
Oq ywi kbutecep uz tadqz ewiehz na mephh i vneb, eh zolv cjer norr gyi mkom okyom qkig guolw ez izdi. Yle ktik yoxh nvev emjoba fm lmefqejh eyaufx, sgavu qco ndifodoz tufl yoafxa.
Ezdidemepw dugm mgu vfetemr eqq gai chax oytiqwl foi rup roma. Uwqliuq ow mallemenism paur[7] af i “xcupebag”, qau dix nelzerev av e lukyek mugti la sozulinuno nji xubamvuir aw pninwt ez beall.
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.
Nmic lzocxof jejetk kwdiwmzav zfa cunwoko iq lruy iv rubozz fcisf ug yevepauxuh alixerauq. Sa lace ca kuwuif zfi saxunopron.jimvzekp cika am qwu zvunxar yanuftosy zah lannc xi qeda qociilriz ezuic xget taqpuxjoh yoyet.
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.