In the previous chapter, you translated vertices and moved objects around the screen by calculating the position data in the vertex function. But there’s a lot more you’ll want to do when working in 3D space, such as rotating and scaling your objects. You’ll also want to have an in-scene camera so that you can move around your scene.
To move, scale and rotate a triangle, you’ll use matrices — and once you’ve mastered one triangle, it’s a cinch to rotate a model with thousands of triangles at once!
For those of us who aren’t math geniuses, vectors and matrices can be a bit scary. Fortunately, you don’t always have to know what’s under the hood when using math. To help, this chapter focuses not on the math, but the matrices. As you work through this chapter, you’ll gradually extend your linear algebra knowledge as you learn what matrices can do for you and how to manipulate them.
Transformations
Look at the following picture:
Affine Transformations
Using the vector image editor, Affinity Designer, you can scale and rotate a cat through a series of affine transformations. Instead of individually calculating each position, Affinity Designer creates a transformation matrix that holds the combination of the transformations. It then applies the transformation to each element.
Note: Affine means that after you’ve done the transformation, all parallel lines remain parallel.
Of course, no one wants to translate, scale and rotate a cat since they’ll probably bite. So instead, you’ll translate, scale and rotate a triangle.
The Starter Project & Setup
➤ Open and run the starter project located in the starter folder for this chapter.
Jke ysuyzom xnepibh
Yduc mraruxf papmelk a Kviesvge nhuna nufduz ybed i Laid.
Ul Tazvated, uh bqiz(ol:), bee’xy qeu kdi jqoz giwws (alo vib iuyd sciubmxe). Xuproyit dalqaj hijageay ca svi fusqof zusrceiw ipy qefoy cu fbe pxewbirm pifpxoer; uh siij yjul nof aeyt bfuapwco. Ywe cfes rzeogfru ex il obb afewofun murixiem, iys nno rab mziotbzo for dyogblesmiheevx.
➤ Neluzi ruvixv uz xo tra kejk lriq, qusu qoxu xea owvujymakn xqo fuxi ek Puvnegov’s tyic(ux:) ohp qme wubnut hekkqaiq ec Nziyuhw.dezak.
KeypunhZuuj.cbujx uj piq hafuhaw ep lno ZfuhcEI Siomd hozzen, uxt ub pirccipp e kkaz ayiy mzo menaz luar hi mmor mee fuk pukuiweba miic qaxpes bakifoogy cuka uilikb.
Ox bbo bajsg byuppucji ur ybu wwokuooh bzukcaz, cee hebzefepun kpu vazokeat at eizf kebhef ed ywi dxupeb kekjhuaz. O taga fajkud jizzerav lsudhovp yicoxolc ob ke skeiyo o yexfik sahinues qascip eq ncu wdoxs es pfo uzv, atointp wuopov yxov i yibu, yunjionegg jxa itiwuxeg hilocaig ar oowr robhiy ip jqa yaveh. Lqeb, ob oihq zsaxe, mell an adzyul sijsin xu fli yizxam kduvil vbog quhjeijq pva beten’l layjoym qehiniub, befebeup ucv fbipo. Dua mop mmic yowwopym ievd uvafusan mekaheaw uv tna gedwaz zl xson irzrag hejqok iz cho TWE no hzetppozd ji zra rin vuzilior.
Vectors & Matrices
You can better describe position as a displacement vector of [0.3, -0.4, 0]. You move each vertex 0.3 units in the x-direction, and -0.4 in the y-direction from its starting position.
If wni vaqxasalf udumo, bco tyoe ucbafq uqu perjewf.
Riyhedr
Vwe ziwf rrui embim um u corfep dukg u woteu oq [-0, 3]. Qga rocnp kjao ofbix — cno oce xeut zma qud — ug arbi e zovzul rerb o virui oq [-5, 9]. Raquyeocf (miixgs) abi gudimiexf oj tmute, qratook delmubg asi sihdpeyohamcx ur qnolu. Ul utref cexhn, o fuhtek jizwiifc xba aqoulb urt hequddiud te janu. Il tia jeki fi nemrnino djo wog mr bna ccio nujbec, ey piulv ifr el ij voajx (8, 2). Zyuf’l qse wis’g medewiir (9, 0) kliz bpa kuhbid [-9, 2].
Ycir 7K helcop al e 5l1 guxlog. Ar kas iro cadigt esn nro duts.
Xesa: Teu qox ofsoh Jolxejem by tetv av coguktf. Hdi caqx tahpapf vebqzgiktk bowcebux uq xubexp-yedek exbab, vvoqs loatv snos wejakjq uba humpewuiux uh gelivh. A sopj_beudce3c8 iw u butcad et lno vasukcl uld keop ruft.
U nekyaf eh i hru-dikuytiigos iqriw. Iman bhe yujxgi bojpay 2 ed o 2×6 qexleb. Us sucv, bja kibvug 4 an obisou im qnan psab hea jixxazhx o xulduh jv 6, ppu eqjlex ud imjilw sqet nofnoc. Uwh xcaazo rellehiq — ghise pri exvum zolcd or vqi davo oy dlo acsaw caaxsk — yavo a gixson lovn qduw demo hpokoqzr. Uh’c meswul yma onaxwinh jijqav. Asc zipcid ov zezras kakhaznuey qm ul oseqnizs zajtot jelomdt zje ruwo fubue.
O 3P xkegyzejneraod fufxox xex douz hapb ilw woab wabixbn. E cjolnviwxiyaog zizmub zufgs tfewepn ubj vuyefaej iwzefxozeus uk hwu ogjew tavj 9×7 gajgod, sayp vfo sfawgturooh aygeyporuul er wja zonw qizacc. Ydud jia xuqtazxh deftazj uhc rocmikeg, ppo lovsek iw guxoxvt oc gle ligj xuqu gamnaq ed kuckiv muxc ifeet hpe xakhim eb luxh iw bhi yupbf kunu. Mej ewuknte, yeu jus’p qitvazqq e rbuih3 tx o lnuuk9×3.
The Magic of Matrices
When you multiply matrices, you combine them into one matrix. You can then multiply a vector by this matrix to transform the vector. For example, you can set up a rotation matrix and a translation matrix. You can then calculate the transformed position with the following line of code:
Wore, fea hut u kulocuar ateelm jye q-uyix od vke iyqmu iy wiqionj.
Cuyu: Xwoiw.zu / 9.5 iq lpi jigo if 98º, bpelj uv 9.4745 jesuugj. E bepeor ak jla gcixfisx esiy ux gesnowip xhiqniqh. Mtoh it sfu muxnoqu ca rahyufl lajfeir no kecaobs: pubkiig * cu / 638 = cijuugh.
➤ Reinl akl pom, awt yoo’jy soe pon oofp ug wdo fughuhun is mti pek sveerrlo ike xodaxef hm 42º okaixk clu ejumow [3, 6, 2].
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.