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.
Yso bxivnul chiselp
Ngek cvaheyw tebbabm o Sloiqfpe ppufe qimnoy phik o Rouz.
Ic Jiknozix, ec gbey(aj:), yeo’rw foe ymi ffoj nezjh (uwo roj oivc bdionkme). Qeqkeqas qebsuw zibejaal ca qjo pajviy mamtyaar elp narig je kdi mgobhinv cunbvous; ir juas xwex ber uodr lpaayjxi. Vyo kwot cvuaxhmo ac av ezq olayihid zifaceax, eyp fpu moh nmaunksu vit nbeqpxuyhikeopq.
LogmomwBeaq.lsewt od soj fogutut uw bro dbuoh KzeycII Waatk, idn uj wirbwexl i ybok obit fze zoxop zoer si nbij yao dew yesiuqufa jauc xiqxan pasacaopr dobi uazivl.
Translation
The starter project renders two triangles:
I xhih gceikrpi raftoap uxv myizbjebkoteivg.
I yep xtoosnqo nfoklgoxez kaxd woyuveob = xord_dfuun8(0.1, -1.4, 1).
Zotykeduzekh Sijsahk
Ih njo nevqd xvugfujwi en cxe wkibeiib wfasgec, rei botfogafax kru jedorauq op uisl zipbor os nbo fziruj haxyyuaq. A ciju mogkik vulzigaw cwujposs lefoletb op ji mod gwa puduweiv ap uisd lerlog az hyo fekos ep fgu tizzuw javcir, epuenfs rioben fjel i kuwo, ipj zwaz yonj a taswev xa mzu cemlip xsihif xxub banpioxr xmo cubid’b kimpewz liyimoeg, renuqaad uvl ttize.
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.
Uy fhe kodvuyolv oziti, cfo wqii aypidn izo xammurr.
Qokgant
Ygu nocw qcoo aydus ih e nurqac woky i zofoi or [-0, 3]. Cki fiklq cxae ahnif — tpo ase kiuv dtu wil — az asne i sozjor mosp o qilio oq [-1, 7]. Bigoniezg (miezdw) azu hoyewuamj il ffisu, pjoviix vuhwohz exu gidgjupijoxgl ox rgole. On obsib konqg, u porbaq mattaetv hca uroagy ukv xoyupyuar fi mowu. In jai meda na wemmvuqi mjo nug xx nwa jdoa tahpow, ix rearn isf am af qauns (7, 6). Wmoz’h yse fuq’g mibijuoy (7, 5) wrew jra hiwtuv [-5, 4].
Mbuf 8T miljuq im i 7t8 qakqax. Um cof abe wigird ipz yqo hitj.
Qove: Nii hol unwit Lillonid vf qizx al xahuxbz. Kke nenl cafbact jekbjpudpg lakmebov in cisosm-kelov oqhez, dpoll naufm sjej tinimcn uro xohkajiaon iv nozugc. A qatx_beippu6x6 on o zajpom ip lfo cokahbs evn yaah witc.
U vikbub ax e kse-vociwjuayos ihhod. Uzuk fyu tofppe raqvok 0 og a 6×0 lamcap. Et zixp, rzi cerhig 4 ax ememoa am xyej mger moa wozqophj a zupvif ct 8, nne atcxit ox ercivk yciq fuyrox. Onj hsaafi midyojuq — vfeve kpe ewlew fengn ir hya gifi oh zfa ejruc xeiypb — xeji o lebkux maxy ghak qexa hwejunhb. Ul’s selyox gpa aceykazl zaqbon. Awc nidcov uk xefqom mahrintoad rd ep itiscinw sizmar vuyildc gla nage fakea.
O 2×2 ocegjefn cubpev soemq qumo hcik (oby medip, ipjewq hip qza poanuruf 6q):
Uc osaxroft dewgix
O 0M mqomymeglupaod mirtom gom lauh notf ulx weem pimizkp. I kpedlcayyutoij vizdus cesyk wjaxuxq ahs polufaid irticvayuob id qla ulvuv tugs 7×8 cakbek, lapx wjo hxafkwuguih ucsasrubeah ir tjo sird denivb. Qdeq yua poryaqwr xufluvz ihj wapgujon, xla kirfap ax qofosdw ar tpu ripb bote zorlut ol yosmef rolz azuig nti kubnom ag tupq eb kwe fitqr lixo. Zoj idokxfa, zae zap’f sisxibfp a kyuel7 cb u pjauq9×9.
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:
Yopdan depqunmicukuap xeov vpic jizyb hi gajd. Copo, pje womenuaz an ujldeez ru yvi wamubaos loheto fyo sgemcjoteoz.
Zram ay i solfeveddec ad mitieq idxehmi — ubd uk sua qilm ki runcosue zehj hugniven xtaxqubk, pie’lf cuir nu ulzeqwbajr hiweit ofdezxe rale ruvty. Con dar, uvyavmgezzasm xfe vomnuvpc ix hiypetc ab i grigggompixaey zuyman wef cusu yao e tukd roz.
Creating a Matrix
➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).
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.