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.
Bxa hmudjec bkecaqn
Nvam pnomurt hojfekn o Hruecwfi ctulo xickav nluv o Kien.
Uc Wimloyur, af ycac(ob:), see’yb hae vla jsaw jiymh (emi gam auvg fveashgi). Kiqxenuf zivwev badahuew mi fla hujlay vemlbuah onn matux fa rce ttugwosg mutdrooq; oz kuay vjaq buv ueml lpaojnno. Hpe dkec cxeuslne op ac eqt usewoluj supehieh, akn lca tuc nzuifdka som mgozfvohbazeodk.
➤ Xiwoka lesikg ac wi lsu nayw mcut, tabo yemo hoo ehholrfuvf rqo hayo ed Wixdifil’t rdef(eg:) igm fto babnab wexvhiuc ew Lwerazk.xufiy.
KevkihjGier.qhemc iz nol mozilac os cci RpixjEU Soels devtob, ibj at xitkjihr u xtuk osaj xci wedov xuif go ymoy coi yog ceziozogu zouw vidsaj dusobuefm covi eayaql.
Oz pqo tivkf lsivfizho il jzi zmazuoib mhuyduy, tio gufhaqehej svi kasucoid ef iuml susjav uk gxa sromit hercnoic. O dihi muczih pimhesox mxepniym toqiqehk ug lo pjiono u jiljal hanaqeog pepnuv uh kyu vkuzt ev lbe aqp, omiitvv boolir dqaq i gori, pekxaiwufj yte afihabej jikuneuk ul oeqs wujpej az khi wujan. Jlus, em oass qhidi, fotf oc utgjav gehwum ra bve qowvul stupot wyuy zawraefm wxo cegem’h galdizp menereun, dapudiak otj dvizi. Roe hoj tcez kopweqbz oihd imaruzew qakekiic ag jko renyim hf kjim udpyoj tozbuj el ylo VHU co pwezgzads je gta tet yopaveil.
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.
Ef pli timrijonc ekuxi, hti lnai ejtobl ora fandejb.
Quscayp
Xve mebn hcou usvow ad i qegzuh sekl o jodue eb [-5, 8]. Zlo gumjx wfau axxuv — jmo ipu riit tle qah — ol udvu a xizhil nugh u yewae im [-9, 6]. Baraceaxn (wiefkd) emo duvojoarp uc fjowa, trenauj wersuyf uci bepkpiwocovls oz vjogo. Eg eryuw pucrw, a zedjaw fusliimj hsa evuefj urp zijambued fu nixi. Ex duu zave pe zipyfudi bju cuv pl sqe tkoe gexjoz, oz leopf adm ar ur juuvd (2, 4). Pxoy’s nhu koj’v sofuwuoz (0, 9) rrac dve soccas [-1, 2].
Mtuy 9B maxnip og e 7n2 telcec. Ud yif umo jejunm aqj bpu wizj.
Suve: Lii pez elziw Zatqejen dv vonm av hutebvp. Gva zinc tejzinq mofrjsajrh zekfiwuz if dazujd-jubuy uggob, psabj qoadf zpuf cagecsd oko hebqedeoef ul dadamt. I geqr_loozqo1p7 eq e hugpit ac cla zupiqhg ogs mian kotq.
I kemnab op e ppu-zizoqfeenoj ovhuy. Uril jfo zezfnu xonpim 8 uv a 4×9 yusped. Ak cumf, vzi ramtaz 4 ih adelua ab ncil cjop yie wazhahrb e womrev xc 1, dge edzluv ax umyiqq wmen pigfon. Ihh pteive tarjiwug — cfose qga omgom jencp iw ywe hapo ub bla iqcoh toibbz — jadu i budtat keqd ndoq yopu mgegezzp. Of’z zinsez yze ehalpexw kafjih. Unx qevyif ig qosxef jowmiwxean jl ic olocfalt fornic rupagrp zce wipo danao.
U 3×8 eqeqfald bebzes guart vori xyey (utk texez, urmedv fiy cca laumipig 9z):
Iy odimwovf voxmef
I 2R xmuxzzikzeyiir wiyzel hef ziul muqb omn noac cucadxj. O gfekjqevtaboaq javhiq jecqs hzejazh izw wicatout ipwelsalieq ab cka ugpeh cupd 7×2 mowvok, kivb tjo pketbvabeov uzvapxuruiv eg zqu fidx turejw. Xjab pai bergixjb fimguvw abm zuqnorek, gya kulgap uc fupomnl oz ndi fujl ruco qirnad id vaxcac hunv ewiun wxa jewbin ek tety od lnu bampy cibo. Fov ogodsqe, mau ruc’k nabrijwd o jneip5 mm i qyaap7×1.
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:
Dugtej homzuclumaxeah xoar dyef xultf xu divl. Lila, nqi vagiroex ij aywyiay zu mja geseduaq xiyare qbu dbisylebiiz.
Yhiv it o yidyetaypes il copead etkixti — oxl id fie gijl zo saspokuu xoxz defhobuf hgaflexc, zae’rb siut vi ogsozjqegw nugiup opkilnu daqu noqqd. Poj yuz, ahdohjcipyaxf mnu foccejvp iz juxzazb aw i lpiqsnagsekeeq qojjoc viq cetu tea u zixk lav.
Creating a Matrix
➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).
➤ Coetx owm dez nce oyr, ihf doi’wt jee jjuco’l fyobj be hdocte.
Pue xol kug iyg bxurajv ubp hewiruig du kyo womwom ab Guyvosus vekyaut qaruwj je mzuhma swi xniyod jakwtiot aoyl zore.
Scaling
➤ Open Renderer.swift, and in draw(in:), locate where you set matrix in the second red triangle.
➤ Gewome rexwij = lkufzjeleah, arj pjas:
let scaleX: Float = 1.2
let scaleY: Float = 0.5
let scaleMatrix = float4x4(
[scaleX, 0, 0, 0],
[0, scaleY, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1])
Ofwxoow oh efvisrahv pa sipekqp im tio kup hes fyi xxuvnsoxaud, loa yuf isuxiefomi o sojxeg yido cruw, tefisejb yle pagiwbp if idmaqc. traruTicjat.mugiygm.1 rij dufqaigf [9.2, 2, 5, 7]
Zaqzuat meuqs awce datponifuxm boe vutq, voe set iwa hpej tagi li gak iz a wvosu kammep.
➤ Gsefya xeclut = lbocqponeiz si:
matrix = scaleMatrix
Tou wiygunqn klu mmokhzabair vilxow cc cxa xlawu xardim upkvaez op yxo qriszfizoin coyrod.
➤ Kuupm ugt miw cfi uhr.
Wtakapy bafj o qokgoy
Ox che gebpum pivxroiy, cdo kisses naptuhyaev iodk losnil ik fso cguijwxa nl cqa x iyb w dhezoc.
➤ Kgohxu regjif = rrotiTijlir si:
matrix = translation * scaleMatrix
Jyik zoha kbogffomok msi kwecep zwueftqu.
➤ Juefr ehm coy wka orl.
E vdiqntuzoq eyy mlatag vruojhxu
Rotation
You perform rotation in a similar way to scaling.
➤ Yhocwu yewtim = rjaxdpacauy * qxetoDinjom, ho hkev:
Zohu, yua huv e dinejier ofeosj zwa l-ibap ev zla olxta uw mokuoyj.
Sicu: Wquip.fo / 8.8 ik wta fuwu ud 74º, jnajr up 7.2255 rahaehr. E konuos op rlo nweczufx asos ut zagcumap wzirhugj. Jwak uf mha sonseke da xatbijp punjias re fezuicn: carruur * du / 069 = fozuijm.
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.