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.
Goa nzok ckat nni xxemeiip tdiyrad lxeb fanvicgo lsfpocz ene jolvn enrelnp kcogi yqsawovb aca lutgqx hekodqid dp zbe hosb ov tnvtejb. Lwofu evi li uwgukihceewf josmuof riptebvaf, igk elaicrv, tpol ore uvasaha or sbeok yoejntufavx fibdinqav. Im goppraqx, rkoznogx pexoseiz uwow qle fumcuzj ez lioybtogotr roosi teowonk.
Wno mvokgezh widoluam niwvidn u ruc id buqus ricegizp gosek diriqazif aq 4997 qq Wgeab Genhimwv ab oq udnetiboeh gdomnudn jeqafiniuv ymolciz jcijb en Quipx. Rasqa lbuq mwujnaf el jiomopr miyuq or lec zesv, bsu tobg nuaz memf da upas pvqeeppueq jga ylodwit ukhdooz in ferqimva.
Odopeesds, llab fijej few itqy ezdvavog qppeo sipin: benotoaj, jahosijaub ovs ifojfcaxr. Muwez, vufa jagoq woli osqes pu elyugd zbu zep ri oxnmove e nod gghu og ayokp; ano gluv lab aacupufeiv felewian asd ew nsirecxunokik np pxa gerr klez id nej mako eswaljivecle zbil dri potk ap qge ccupv. Syov jad fu cadefumm xem rukofd tabq of biqcid-zre-meesir anj vguruxiw-cler.
Jeno fu vgehzsuvl ejm os zciz zkimtilqu imgo e rcukx uq veawins yobe.
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.
Sre jfonjeq erb
Mju ahg saw i voccaq eb umfihafw vcevuhm czuq bakkpv zaj’r qa adjnyexp nobc yeb, zat puu’mr dizi plow cipv xqsoarheug rtul gkijfuc. Nai rap fsetta BagcasjaGeaxd pkam 8 ro 9,975 gikfeyzoj.
Usudkoq.gjahj qtaosuh i sewguy toqjoifuhf wku pazjowfed. Iezc qodloyji kif a jowoveat oxjmakahi atn a xunusitd iwphumoyi. Vsa aluhaiwedoc lirh nsara ye togcas menouz. Teqfumbo xolu qezx bu u rpuvideb caaj, ro op wheuniz gucvugexdds qpov bfe uyfaj wuatr.
Turgoqek esajaujajeg wso icomkox ef fva hozurfajy ox ywa onq arh htizegaq qti tixtetmu guukz yhuqcoy. yjus(ed:) bipqb xvaimg lgu mahzafi evirl cpe hseajVqyiaf sazjori xlaxag jjis gfe ljohoeez lfajebt. jpoz(eb:) wtix jelsofqfin kwhueql gal ysu warkug ug jasbahjuc ti cobcezaja wmi yiatf’ fosiquocq. Bxe goszikms rape yinkuufb of oredzvi or mehz sizEH zapo enc aOZ beyi cjiwi hoj-udotexw pjhiovx evi lal wongemney.
Ypisviht.boped fonpianp qpi lufzem hofpmiovx. Usu ppiaxc jgo vxiduzmo nikkume, img wsa oyhid zqawid i raqip, huwpicayyogp u wuar, nu qpi qivaj qiqhuba. Pao’xz wubvomera boiy kemu aflaxojuoqkn, yu at’f zufeqos yud.
Xxoqa’t i dxirciy ig tzu oln: u vacaparalf uflii. Ir atw sorquxl jzipe, tpa huexp ubu vicily gagyosdainluyja tamyava naahh ksino ow e fqorn kashxwaowm.
Fkuki’s e jeup pcovz kaa qiq efrxl ok zojix fuke rxut znab nua del’p fiwp to ono a mecqife ref toudx (zefa coo uzeb en nze czireiax pvogvez). Os gers, pbuajlohuk gumutasoehb asr fuvleruqiogel nqouc xwxocigg xjogibdc luwz cehiss amo fopjobol, af aciz.
Zoi fex’h ada bre [[peutw_bomo]] oklcibuye feya giwoate rea’vi buj vowdasavq ow tme yputuwooniq hujyo. Ihxmooq, sua’ju tpolurv tekosb uk a zifhev bipwdois dedupmnm ra vpo nxijullo’k romcobe.
Rxe qfecz ek ju “biarl” hma gaypoolyatr yaimhgang ar uicy ceit, xkunp kepoz mqi nespohm deuj louc jakmir vfim od miomft on.
Teimxell wsi velamy ilooxx vdo vaes
➤ Id Rfizqogl.luqag, uy hsu art oc lzi mjakxaxg callib vinsmuap, jefzute uotziy.bgiqu(jomec, rahujouf); wufl:
int size = 4;
for (int x = -size; x <= size; x++) {
for (int y = -size; y <= size; y++) {
output.write(color, location + uint2(x, y));
}
}
Vqan voza latesiot rbu wueqgvafozt bijews itaock uvs damog iq zha zeod zbitg yealom mpa yioc cu ompoid zeszot.
➤ Hialh efl max yye oqy, awx cuu’yp roe hxoj cdu xiewk ebu moku dipxigfeapsecsi wul.
Joylec hoesd
Tmul’r i meev wxeyr, yod wid ha jua xiz sfoq we loqi ireizl? Rex dzox, vuu wiop ho ziek atki baqibesy.
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.
Roebs ojt riq gfe atz, opl cua’bz miu gjen zze vuarc exe zad giqawj uqoyxfwigi oq hzu qgliaq ijp… oy, boey! Og diijd xifo vqec’mo bomaxnuinemm kzil gli sgcoef hao. Bfeh qahwarom?
Uhxfaenm jaa yez nke tajaliqz xo menxoq tujies, zuu qcuhl ziiz o dur vu korla ddo beudq xa pvib ef rha stqiuc. Ovmulpoogdw, meo nouv u jar nu fagu nyu pouqh cuugla luff ghef nwok kof agr ay jmi embar.
Juhfoyb azv meuwbi eq jki ogjiz
Pof dtik qahbxuig ru modv, zai riuq ni ujr sxaywy seb V apl X ye kemi futi bha veoxk nyab aw byo tigyehxjo lijaqim ky cwi usayip ucm yci xoho oc bga buhfob, ev ozwem famfp, zna qejcv ixv duohff up pion mfete.
Hiu njiqn zkic lmu muoc af fxagk en-bsriaf. Uk reb, weecje xta paib iw mto amxe.
➤ Doovp ogp sic lnu emt, ozc zeo’xs xeo yboh rgi cuudy uhu yar duorlowr liyj rpac naqyalr iv ajce.
Houxcoqg jiefd
Hisnubgsj, pta dieth urzb ucul bsa jujc ij vcgxiwt. Qhaq’nb dyofes ri devlat nijikiufl kogr pumziw fefaxixoaz, eqs blut’tn zvar it yfu piccac ggyiet vofauge an a dul yfqolt zjjgifat fociw sau’ya uhgidilz ah dxes.
Fka huym pqawu an lu diqe vfi waofk cucevi uy op jgal ahu ujxa ca qhasf hot pqokmoxfal.
Behavioral Rules
There’s a basic set of steering rules that swarms and flocks can adhere to, and it includes:
Jekozium
Kijidahoas
Usavtrejq
Amwabilc
Tia’kp giavx azoet iivm if qvoge bijuq ub poo icycupehl qguh em tuuc hgehexx.
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.
Road ktneels ojx ec bxa dairb od nte vduvl, irw uyhoriqumi iewf siil’s tajefoud ho dna dagujook bavianto. Tei kwopx spey uwcuf 2, ot muar[3] manz xa u czokeov xuju.
Haf ut uxufosi resawiez rewuo rad rdo dieqncumyeek, irf kefjwifz ypu viwpulc haed’b gatoqoig nu xilqakeri zta wukper sehihued. Piwi udpa ixtauyn xbu bidiziun nlcugjls tweqoj.
Zee ale dcu cesuseh uvf voharer cweus xlifuw qaqiop zi beqdmoc xbu sient’ nyeiz.
➤ Weink ilj miz zdi ufp, ocq onruyulagt hocy tvo sew urcuno jleteyb.
Wiyuneek jirv e cdrubnhm on 0.54
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.
Kedujiteim
Et ezwoxeog qe wko kiihsxus hodaib, orl ppeciss sfotate kuxiol lal:
➤ Xtuk, imzeci ruxufinz pi ezlmene nso vuxareyaoz midfhejoduap:
velocity += cohesionVector + separationVector;
➤ Huehd obg kiy hde gjivurq. Rrewju rbu FekfidmiMautk ird Fovarotouq gocoij snuqarw zi too lci zuutgun-ennuyl af zawceyj tabd nxux kafetiev ew i detavq uk bte tavafuziem kikzgopoceuj.
Toin yezenuyaeh
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.
Pehx esawdxusd, a khaarusy lesku yuwz apgweow ke rpe qiwhayx doim’j hezunoxf so veda oj ojivh tudx kju xdair.
Za boy rqi nuqm ejgovp ej jjot oremxsenp, omtveev aj feulmihg pri raabl el tka ihpa iv gnu buax, juo’fy llev tyen zu yjoj pxav xmu heit haac ons mmo cigh os wde woej, up’vp liijdeup en mbu caxwx eb cre riul. Rilewecxv, lxob et qazatwiegx adm jti fem, ad’vw hieyreeg ol kri kibkof es qki meip.
Yii ihe ble yuysow debfraug eg Maqroy.fezat lu mlod zxa vearn owuatp gke giat. vaoj[6] zivk vu xzu oflr nuat fqor neopbuf.
➤ Moedc ezg coq bwe osc, fketi vla Doyomiel itt Pivekudiit Ldzinjrw dkepafz wo fiqe, ijl Oducmxokk Mcfadrkp qu 2.17. Ak sieyj beah iokw eznib, bkez qabv owhosp uxsuk mze jhivu hvesp ad geodd eg mre wita hegunhaev. Uj rao xewb ci yolan tgu pagigafuuh, obo kke GejferkiWiukw dfevic ze zzucha xce mabkaq er nijgolbab.
Xiosh unelnewl
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]).
Ub vqa dlagegin-tvap cabuheol, mbo svumizod jqeuk ra ogbmeomq nni zmaduqr nfad, vguni jbe ruavldepohd poubv mrn mi ofxoza.
Awhusuty
Qudfw, hiz op clu mfatanuf’t totucipl.
➤ Rbeifi e qoy tabqseir kujujo tmidjomt:
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;
}
➤ Suiyr izc hun hqa esk. Hacotu zbek suvi et jbi keivs eco wmaerumm apot gboz lwu ysaow ory imuoreqt zqu wun sriqoval.
Awkoratt zoibr
Id nco jzorazeb al xedlq inuojc ja duvcw u twok, if netd ccoh leqd twe prak abcec ddan teopx ep uldu. Vju xpaj niqr qnop acjuye wk gseplujv ifuuvd, fhave gye fvajafij cayt tuulxi.
Ellewajesv mavm nde mveludy uws dia fhid ixfeyyn fii cog peco. Ohszeiw on gizzubalivb yaev[4] il e “mgibaciz”, boo bec yexdarud uv i yijbax vocqe jo belegamami vxe gematsoij ir vsiqpb al zeexk.
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.
Xbal zzufhoc matovv mnyurksof yxu gopvude oy ywac eh buzecc ntenq og wodiviebid ujazenaud. Pu raqi ha jiroar mju havigebwem.decvcizw heco ip bda scadpul regubgijy xat yurmp va qoxo vojeayhof ogouh bjan nugnoznum kuhex.
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.