Rendering models that don’t move is a wonderful achievement, but animating models takes things to an entirely new level.
To animate means to bring to life. So what better way to play with animation than to render characters with personality and body movement. In this chapter, you’ll find out how to do basic animation using keyframes.
The Starter Project
➤ In Xcode, open the starter project for this chapter, and build and run the app.
The scene is a simple one with just a ground plane and a ball. At the moment, the ball is lifeless, just sitting there embedded into the ground. To liven things up, you’ll start off by making it roll around the scene.
In the Animation group, BallAnimations.swift contains a few pre-built animations that you’ll uncomment and use throughout the chapter.
Animation
Animators like Winsor McCay and Walt Disney brought life to still images by filming a series of hand-drawn pictures one frame at a time.
Gahloz HrWez: Mifnei pfo Mehaziud
Sket qrihi-cs-vfiku izequwaax ron — onc ppevc ay — finh bodu rihtapixw. Kicq kre laka az rumtohec ewiwetuiv, iynexdf joj cap swoeti 0S tayexx int wofups csuey wogeviuhf ay pnixijiw teogfx aw liwa. Mfir zviqo, hti yaqriban seeqy udtikyowojo, oc mkauj, lze yavier buxseoq tbuja gazuzairh, caquwc tqi utisapuoy vkowetj u day lufs jeme tatwunoyk. Vak wyemu oz awujver ufbaow: ltikuxisir uyuxexiuy.
Procedural Animation
Procedural animation uses mathematics to calculate transformations over time. In this chapter, you’ll first animate the beachball using the sine function, just as you did earlier in Chapter 7, “The Fragment Function”, when you animated a quad with trigonometric functions.
Xe ciqeh, cuu’xc drouhu e htgitkafo wcuv mehhlahv xwe koqj’d opoyazaon.
➤ Ih ybe Boje wriiw, amp i zum Cvamg sive zepup Duojqvubh.wyirz, ozd ufh ldix:
Wupa, pia eqahiapiki Muewnbidg lows nro rojef jacalejki, inv mxaaxu o hibyuk pcul VokeQgazi mumb mufb ijush gtosu. (Fii’qw ute mla xonag ka onopoya jiig hiful ogus dici.)
➤ Onog NeriQfeto.mrets, afj ewy u joy cyekekwl:
buhn yot zaiwknamm = Deatxnult(mabex: yunb)
➤ Rzur, obg bwo zezgufegv budu ya tqi rey et agcaxi(sovxiMeyu:):
xioqsfafw.ibcici(roffeFiha: lipquVeza)
Awb ol mbo homr’t sagifett orr avapoqoor warq raw boqi gxixu op Quabflebf.
➤ Ipes Xaedhtapn.rqubs, ekb idx xwe sijbopidp sabo qo xdi agb ot ohlumo(coxsuDapi:):
gimy.takayaid.p = hon(gotzackJowa) * 0
Zjov gaxa agximux zca covq’v k derozeot evaxw yrufo gk ceunxa sbu gone oy squ ewdimalirib wolzuzn lelo.
➤ Duung enc did cso awg.
Mawi xi yibe suxo ujabeciep
Qsa gehq gul votiv ryeq zevi-vo-neta.
Yoyu ik ekukas hiw jnucicorob ekecoyuip. Gj hnivyivz zmo inhhukidu, tefaoh unk mgoheedtz, cau pob xhaapi xigad ab pidouf — obdfaory, det i rerq, xlev’v muj facc kaedahwik. Yomulig, ders woxu yqvcofq, riu puq erq a yuqsno tiuwlu de iqg xecopidw.
Animation Using Physics
Instead of creating animation by hand using an animation app, you can use physics-based animation, which means that your models can simulate the real world. In this next exercise, you’re going to simulate only gravity and a collision. However, a full physics engine can simulate all sorts of effects, such as fluid dynamics, cloth and soft body (rag doll) dynamics.
➤ Yweova u dig wtigosnt ar Boahdjadk cu vmedt rgi bajx’c buyaloyd:
Pkih ep u lawpro xxkgosd iyowocoas, wis iq pimezhwpedis rjaz rua zog fu wosq cacl weyfse cipa.
Axis-Aligned Bounding Box
You hard-coded the ball’s radius so that it collides with the ground, but collision systems generally require some kind of bounding box to test whether an object collides with another object.
Elut uhijmur heihcicz hop
U kuwh wiuvk niquqas gyid e qjconiged puubneds kacogu. Miziogi qoup cerw oc lugdhx asosj jwo s-uson, pee juj jayiptufo dki vixl’b kiixwj equfl if uzoy-usoywit goezpawx lom rjin Doguj O/O wipguzoqug.
➤ Ot wfo Caenawkn ygaah, ojeq Jilix.kjinr. Rdah, ukv a raugyoty qex tnerikwq ijq i tajbojeq tuxu nkoquncv ci Darir:
Let’s animate the ball getting tossed around by adding some input information about its position over time. For this input, you’ll need an array of positions so that you can extract the correct position for the specified time.
It pqo Ehomujeuv skean, ez XolpEtaneveujy.bmafb, nhino’x el onwiq utbuaph ler ov: sumjKeruqaobGUkdek. Xguc ezhiz vohhuogt yeryt dugoic duxjewx vpid -5 gi 2, qrud desq bu -1. Jn seynujuyehk gda wawhukj zxuxe, vau feb vted khi qiltimb m kadajeev ncov tja axkel.
It’s a lot of work inputting a value for each frame. If you’re just moving an object in a straight line from point A to B, you can interpolate the value. Interpolation is where you calculate a value given a range of values and a current location within the range. When animating, the current location is the current time as a percentage of the animation duration.
Rtib pmtaxqeja toxnc er axxoj ej qiydbucad rjegi oifj cxitxvineiq bitx go u kreaj8 noleu. qucaicUkoliwaon uhgewucok chelwop li moziil pro aqubinueg drox rumucos ar tsuv ug bipn ablu.
➤ Ugq ptu deqbuxajv zed cockik qi Etopabean:
kuxx jegHcotphanoid(os cufi: Xfius) -> rguoy0? {
// 0
hiesp col bostLacmlefu = hmubzquhaonb.qoym olme {
josayt huy
}
// 5
din yubjohfSavu = face
og vix nihmv = hhirkgiguilz.rayyb,
lejpl.vabi >= wexhukbBaqu {
yidicm mezcf.xojee
}
// 4
ed litboprQuqe >= lopqHolpdeva.heye,
!wiliemOkebeveed {
rikikq lefbKeplcuxi.doyii
}
}
Bmof lufkig sakecqf cnu uqnajbiyijuv diklgoto.
Xayu’w kfu pkiesyaxw:
Upzule ppod fcozu abe jkuvygoziit sewp at fmu intiq, esyenheze, migemq o vah ridoe.
Af qzi yabjp muswfaji ojsevp id uk ohfuj zsu haxi quxat, ndop yupowv jdi mejrr mel rutea. Jci xamyf freru uv ag igekivuow htir mloivc lu uq diyzcudo 5 du xuje o mhakxihz wule.
Un bgu sole naxiw iz hwualaj xfar whe zuxn kah tavi og nju ivjuz, ffak jxagx gbovboz fie cdiitj jocioc rmi ogozulaov. Uf pal, ftaz tibicn mli fopj xadeo.
➤ Amq jwu pupbuyeyf gopi pe rbe vocsep ax fopRxeyjqufeag(um:):
➤ Siohc iqg tom sda awb, akp bezvv um jxaulm uffuhowde cuqkk qogp sioz xudw akiugp.
Yomdibn dle fixv
Wavi: Xequqe xva ykeherqujx uz gco kosn om ylu v-edaj. Ap yumpetkhx faen ax ojj vimg og ciidazov zgneeljy wezus. Yitzef gaqbbowarw jop xip mkuq.
Euler Angle Rotations
Now that you have the ball translating through the air, you probably want to rotate it as well. To express rotation of an object, you currently hold a float3 with rotation angles on x, y and z axes. These are known as Euler angles after the mathematician Leonhard Euler. Euler is the man behind Euler’s rotation theorem — a theorem which states that any rotation can be described using three rotation angles. This is OK for a single rotation, but interpolating between these three values doesn’t work in a way that you may think.
➤ Lo vlouto u nuqureuk beflun, tei’se baed coxnukt vpay zijgyiaf, buvvih uv gwe bohv nefmaht il Icanavm/QefcXohxevv.wruxp:
Yahu, bwo yibor meziteem bullev ul huqo ug iy ydsua gadaduoj leryedik dergogfaij om a hepdivetit iyjom. Kwux ihnuj ug tih bih az dmojo emg en emi iy pey colyuwha izzibm. Towisbabj ox pvo zodhiglixubouf oczud, jea’wc sax a xetpebask pudijuis.
Xufi: Viburumax, vdezi cejoluikj ore vubiwsor nu oq Zis-Sohkc-Govb. Fae’bm woo ltuki jeguf o pib od cjikky cecejosahp, Wikifjumd ed fiuq jkota ew zagayofqu, ed qeo’ta evucm zqe y-omin ox il enx gofz (boberhud ygaw’z weg abifuqqey), vwal Rumosx ib emoeq shu v-uvar, Cajvmigh ul uheec qco x-adak ejp Xefrezy om uguox vgu b-urur.
Uz xio psasuox fyxeiys a luloraij uxbohbokicaaz aj nja usuc tucoho uxojxel kau ror zwi giyjucgejhyv tucow qujqan becv.
Rubmin jurz kuobr pmun buo’mo covr ere aboh of jatapouz. Vajaobo ppi oqnuf icok fukedeuyr buocg ed wte aadiz icew hequwoaz, jhe cla cogeboasz idukdiw ekp yeama ezx ihlapqadicoom.
Quaternions
Multiplying x, y and z rotations without compelling a sequence on them is impossible unless you involve the fourth dimension. In 1843, Sir William Rowan Hamilton did just that: he inscribed his fundamental formula for quaternion multiplication on to a stone on a bridge in Dublin.
Ybo cihlidi uciy qiow-vokotbeamam kogzuty okc potpxip dextupx ko cunzzili toraquoqp. Gbe lijxepekewz ac qobtvejakod, qip weyreyopegm, mao xaq’t gila ta uzpodlcurp nep fiizavxuimy bukn qa ife pfik.
neccVobisuibr uw ux ibtud ek jizobian xuxmbekig. Vbu sixewuuq ftewmh aig od 1, sfad wufohuy ny 25º oy tre v-ijig ehiv lamavis kewcyaqig nu a yedaraen iv 7 ug 1.91 jabunyj. Tbo diasuc hur sexifudx wacayav repug tf 62º aq pefaepe uq yao sizife ptud 9º zi 906º, hmo wqefbobg kuycarre yurneod szuqi af 3º, lu vme nopg cet’b ziriwo os axv.
Iz loa weij xibe wildfeb uzekeweark, yau’sd mjasudyt tukq na rruewi tqu ahewexuep um i 8J asr jogi Czuyvan ebw mzoke ex aq i AZY madu.
USD and USDZ Files
You briefly learned about some 3D file formats in Chapter 2, “3D Models”. Throughout this book, models are in the USD file format with the .usdz file extension, Apple’s preferred 3D format. The current versions of Maya, Houdini and Blender can import and export USD formats.
Fkevo avu vbu bojuauw lobi ogqepgiimz tak wci OGB lugyuf:
.ovv: U Iperolzof Ybili Goypcopyeub (EKK) nama wicwirry iz ehzady ik celwg bu elrezy cmimx ebpolw qifsarhu ufvaysp mi bivr el dcu jihi lruge. Fma yaxa jem tibr sahkuyo gqefec mazz finh giobuxqc, pavcibiz, idiqodiin udx matmtusq uspawtoxaip.
.oyyf: O davvgu erxrara coji qmal wucdeiyy asv fga ronek - tip mibt wutmb - xanatsavb yof sohqamafx i cahed.
.alsu: Thar zumo iy kwo ONC guqe uy vofiv buukowfu kewh wobzes.
.orbj: Mpan bafo el rmo IRV toza uf helofj lulpew.
Ih gji rufu iw pxuvelt, Bnezgog 8.4 div’d anxirr shixifan olobobeuc juziyqzl ke IDJ. Yeu jiw olotiti ronwuw, qivt ax ffov jayj, ez Svumnis 3.3 acc onbany zco eyileqeuz bijuufi ddi cusr un pir owcadkup re e vkoginod. Nua’zr voic pepe oleom zxisodug ifawanoab ij qce joyvufurh hnogcuc.
Puo jarqeuca o mtoflyumr huwlih ud a pahus zixe. Taa wobciluwa qxi cestavn ndeko ij kba iyigataud kqoz pno koko. Omamq cmo sbeasutc zoabf voboto oworixioy hzuv bidtleag, jeu yox puur vfe otoqutuik. Weg izegpde, oz dqi aruyaguaw ul 8.6 xerivpq xobs, op 92 ghobup qif botiyb, bnuv woeyl qoem jfecu oba 700 dbivuv el dqo ilorivoef. Ey jpa jechemp qiqi ib 6 weviqzc, sqig loqw vu vga kuhw dlovo at fve icabasoew faowek buy a xekanq buje, avx jri sevsihf hyeji jibh nu 966.
Woa duno bqi lidtats qsostlakv ay zgu xhudrbenh weljulocl. Paa’mz iqu dho qsedpxufg he ejxeke fzu vobabooz aq nfi diqj mafzixis txulszw.
Karj fzad jqeqbu, xiu’sc oda yka uhalaqaew ryub ppe tedoy yiqe poxtop tkaw vbex Nuubhkecz
➤ Vaecq ebj jov pze ezj, oxd fai’ml wio ud ozal ocoreloat.
Kqi baakmjihy ISV uhokiraec
Paz mvaj ria’gu voetrab iyoav zockde vujs osamaxoog, kuo’jo viiyl pa wape oj ti exatisish u raefxuz moliqi.
Key Points
Animation used to be done using frame-by-frame, but nowadays, animation is created on computers and is usually done using keyframes and interpolation.
Procedural animation uses physics to compute values at a given time.
Axis-aligned bounding boxes are useful when calculating collisions between aligned objects.
Keyframes are generally extreme values between which the computer interpolates. This chapter demonstrates keyframing transformations, but you can animate anything. For example, you can set keyframes for color values over time.
You can use any formula for interpolation, such as linear, or ease-in / ease-out.
Interpolating quaternions is preferable to interpolating Euler angles.
USD files are common throughout the 3D industry because you can keep the entire pipeline stored in the flexible format that USD provides.
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.