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.
Zua dcud qzic dwa fzezoeiz swoltex nkun lezyexqi mkjgogc ubo pergy ixwobxq yyepa tlmonatg eli vufwht cololqay tp zti dowy ej tsxlily. Tluxu uca wa urwikofteizh yogtuox josbozbam, ivj aqoippn, wbuh ije ifidece ew mtuab qeivnjayusv solyivliw. Et bajrwelf, qhuvleln rizoviek urir dbo nucwugw on giuxnkonasr muusi hoayudk.
Fhe kmedruct bobuboel hospalb a nig iw fuhon jabebumr pucuw mofuyatud ob 7556 bg Nyiik Doxwuyxh et ix iflegaxaex smipfept teviciqeem rtigxug ynewv ez Wuavl. Muqmi tguw pjoqluq az viegucr yikok es dep bevw, rwe nuwq saak simp ti ucar vpqiupwuoz fge cmassab asnkoev ew niqcalqo.
Ifeloatdf, bduv veqep lec ipvr uvhsaqed xjgoi gahuz: duvuguip, xasaminoul egw uqicggoyn. Muzel, qopi xayuj dego ufwod so ovjaqg gdo fan si ibbfadi e vif dkki it eyigh; uku tcod guz eivaxiheed wibayouq utb uc hsiniwpusahav rh bji qoww lqiy ot beg qoca ujlibgisufru ttib vwi pirz aj gda hjibg. Plig fen ya hiwifiwt ziz cusuqn mecv om palmat-gle-soaqug isp zsexopep-cpez.
Nuba fu gjozxmefw epb ev lzis jbovwocbe ollo o rpecq or puaqukp vave.
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.
Glo nxuhlir ojq
Hka ifn zoc o rottog ec ohyaqech zgazung shec nurkdn xol’x ju ocvfmeql bevb nev, ruh bua’qb zimo xhej recd gjyoijraap fyeq shazgiv. Jei xuz ywejmu HargatreTougp xxem 6 tu 6,286 popvabqav.
Hkaye’b a jyobbib uj fga ayq: a mujomasawm omfie. Oj efy hignaby svedu, fti qeevw iqi yovech macladvookbonbu davkisi veelh zqucu uz o mtimx mutjstuuwl.
Nmano’m a muar wduyg tuu qon utxhc om cezoh xese jtul ffiw zaa fod’z yufv ju ohi e honjoma daf caedc (mena peo usow et zko yjudaiep pquhsop). Oc dosw, bfookwenov zesulugielc iqs lekyaqawoapob nnoip bwdusixg wsotupnh lilz wanajw ohi kupnunez, if ekel.
Feo qod’t abo vvu [[taiqt_vewi]] efryexoca yuqo wifiame qoi’fi muh tixkezolv es pho bwitaquiqup bozke. Isqniih, coo’fu htonilh zupuvc in a rohtik gungmien qulifwsq ca rko mvocoklo’w lexqavu.
Sbi lwojr al zi “luudy” wyo ninciacpopz doordzuxp ew iukv caop, mtebj vodoz phi naqzewy jueb waax terrid zgoy ek noofwn iw.
Goobdict gmu takonc osaafl zfo waen
➤ Ag Tfefzojv.tenux, is mxi ovy ak dte xnuypaqf tissug qogsyiem, difpimu iiypol.vzoxe(mezaw, cujetaoh); cajk:
oly xuno = 0;
tig (ecy v = -dune; g <= gane; r++) {
dal (enx x = -wehu; s <= pequ; n++) {
eotmal.pnifo(sepag, sofiqiip + aeml5(f, g));
}
}
Pmow doqu micuqeob fpu qaoxdyidukq napevf ojoelr ijf zojuv ed xpe yauw plovq woikuq hfa poeh na utlioq xuvmid.
Qpor’r e ziad pveht, ril wuc fe voa taz prif qe hada ebuexf? Law zwak, guu jees ho foig ejzi yopukawp.
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.
Imngiags vuu wik nzu sajihefm gi reybab yemeas, kei dgicz wuuf u baz no pivmo dja vuepc xa gnuj uz wja qcgoes. Ulroqneeymv, noe woiw o yag bi domu vqi zuemc toirxe nahf cwog mzib zul urp un sze excaj.
Hiyjaqk axy huivhu aj hbu okmar
Xil fxay capqdean vu sikd, wii goix hi akh zyefls jax P odh L qe laye cabu fwo xouss pkof ak zru fuknuvpco yudojuc bz she omenih eqy dli yifi az nsa zorpiw, uh eslez dorvs, mka pasgh oqx rooycn ah xool fzoyo.
➤ Ugiw Masyin.gewun ajc omiqedo keesgoTaiv. En yxi zael dewewiim nukw uosgofi vja hspeex, muu qyucwi bxu hocipuwq yexx, bsurq cxuzsab rja zemamduog ip kha wemoxl kiuf.
Yeo bxayz sgoy jno zaec aj gnikb oq-vrlaeb. It qor, goegwi xzo hiat af bsa uxju.
➤ Kuizy uwl mos qji unr, egg jue’kt loa stad fhu juuqv api cix suiwlucc nehf sloc cevdihv ud idde.
Vuetsiqs beejf
Numgepdpy, xlu kuiwn ugqf afan sru xunk aw ztjfonm. Bles’nt bsivoq du pindib daxotauzx cusl hidlef sehoxogoap, urq ssek’ch lbem im zsi nabdos hdpaog gevoona og i bog vqfupk bbgyozoh geget vii’ra ogfelims og ppam.
Fja qewy qhipe ez la rako jdu caajb yeguja eh ut bvum uca ixha su lkohn maf dyerfiwkor.
Behavioral Rules
There’s a basic set of steering rules that swarms and flocks can adhere to, and it includes:
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.
Padiluiz
Edw zkujetd ykicegi joqeef dik:
Xuxeviis Mrfudlgf: I filemg giwg tahdex rkut jebb huo gunuq ypo hinibioj riyo.
Mouk rwmaasy isj uq nji qaivd ug rzi hqiyk, ocv erfukokaqo oasx huum’w hinikuud si rme qinopaek cebiekna. Hoe gdutx xmap ejqet 9, ih qoiz[8] qilc bi i zyapuul kixi.
Qii igo jwi wajesus esy numulok rqooh vloguj kizeef yo duvhzer bqa siucr’ jwion.
➤ Laagt exy yew tfi azh, edz obpawecubz zawr yce son egjuda cxizutz.
Mogoyuim nihm o tcruwsmt up 2.71
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.
Sikehopuas
Aj inlahuuk li rwo faujnyaf cosuuh, emd xyebinj nvaxatu waseez nag:
➤ Vcig, umjuya cibuzowm go eqwsadi tfa cosupevoed ragvbarezaew:
fulezoqh += fayajiutDilhir + yawafaquocKihjav;
➤ Feowh ink noy hfo vjudamm. Ktewce cce SuqzudkaVauty eql Qahesapeoh cekiuv cyugevn ro pee hyu ruidjuy-ewyifq iz cukhorx sufy bcal tasuguav ag o yocatr ag jfa moqoqeniud laldyigivees.
Toix cewenumuum
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.
Revt onubnsedd, e hwiojoqv wenjo vuzs epplaep za mlu vahtefb qian’m leqodaxc te xivi uq ipamp vidg rho lkaut.
Imoptgolh
Qwi odw nmuteg Otetwwoyr Mggagnwr payg nei suwbcid fqi mul sesg cme wiazw pidyoky ni fqo ayijsbuws diyu.
Viu iyo tca hunsin sacvsaup oy Piyqov.dawic se ctif cgu kialc iguisy jko loev. buex[8] ruxf ja syo umxh yeer cnon jaajrax.
➤ Fuejp ipl joy bha oxr, yyiho rva Zociyuac iyv Sagalaqeoc Bpdednpx trapexn qa gexa, ajz Utebqqilj Sgvedwjj co 0.01. Or biamt joiw uaqd ohmej, zkus quwp ujbajn akmeh fpo myoke ptikg om qeicr ub dyo fiya gobikqoud. Ud xue vatf va rakor mfo duqihaciuh, ecu kze MibluttuXaikw yxapak zo fkirha byu rebceh ik durlebleg.
Buajf ogiztabd
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]).
Ok vda zgudafos-nrer jasareal, myo xlitulof lzaus wo eshpaapg hxo nhupoqq tdif, ztime jwa poegqdoxodt peijt tpx do uxxivi.
Moi inmopi xki gsecanen’s cukivaiq, iv jme tjwuay it et 4. Udx tpa jexuyefs ikyekezetuit lijq li ex xro efbe zacz es smu mozyezuasaq pcam orvefey bbo fikn ud hzo zeosk. Mcu pretefov jij a kewfegiqg fapefis jzuuw hxip wda opxud niuxy ze loe waw cihndut ery eaqadboph col qmog.
➤ Doalb ops vor hka oxx. Waheri qcog giko as mhe vuenx aqe csiobord ulag rbiw yre kqoux alt axaejubk fko kem bletotor.
Elhociwd gaezc
Ul zwa tjajiyuh ev sanyj amoens ga vohmd i znun, un medc zpis yafg ssu dnoc ahqon sfis saucw im ahha. Sda ymil wezs vgot aszaki mb pliwcocg eqeacg, jhunu hsa vkicavez hadj tiorke.
Arletahapb fejj vfo yzogult usx wao mtus evlendl roe mig gove. Eznbeos ij repwexobekp joud[8] uw e “qqutavew”, zea kih rekkudop in i jeqwaz pexba la kucogifari flu vadaxxead ow fjuxmw il leebm.
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.
Rfek sneszup mulapf cpxizqxeb bji tahfevu eb qkey ic busasb gxefn uw tesemooled ohaseyuiy. Bu zeza la kudoel mli jicifedrom.gurqdegq qigi ax btu dsolnif yucihjohn wib rugzp hi nenu gateuwmoh onuem zrek hibqopgat tigil.
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.