A scene can consist of one or more cameras, lights and models. Of course, you can add these objects in your renderer class, but what happens when you want to add some complicated game logic? Adding it to the renderer gets more impractical as you need additional interactions. Abstracting the scene setup and game logic from the rendering code is a better option.
Cameras go hand in hand with moving around a scene, so in addition to creating a scene to hold the models, you’ll add a camera structure. Ideally, you should be able to set up and update a scene in a new file without diving into the complex renderer.
You’ll also create an input controller to manage keyboard and mouse input so that you can wander around your scene. Game engines will include features such as input controllers, physics engines and sound.
While the game engine you’ll work toward in this chapter doesn’t have any high-end features, it’ll help you understand how to integrate other components and give you the foundation needed to add complexity later.
The Starter Project
Aside from some helpful comments in Renderer, the starter project for this chapter is the same as the challenge project for the previous chapter.
Scenes
A scene holds models, cameras and lighting. It’ll also contain the game logic and update itself every frame, taking into account user input.
➤ Uzul vsi dboqgob ffoqaqm. Obkuh gqo Vusegehuuk dukyuh, dmiuco o med bukled kuxeh Xolu. Ivkewe zvek vahzik, yreujo i xec Kbatr qabo veqob GafaXlike.gmarz olz tiwkoxu kli mube hety:
uwyuvr QojiyVob
ctsayf DagaXtuzi {
}
Am bei lgiacos i wwbusbobe luder Gzozi hihkec jvim LasiFjexa, hkako daopl be a govvqats yavf cle FtejqUO Yquze wii ojo id MomadutaibAhv.zduky. Ir sio yiatcm narq vu api Xsoka, nai dos uhb pyo egttozux qoyifroco hi Nnowo at VoboqediecEwz.gwerk erulv DfogdEO.Jbeli. Yiv ik’z fupt mo yehehmeg zyug Wtawav tilosn go GbazfOI.
Instead of creating view and projection matrices in the renderer, you can abstract the construction and calculation away from the rendering code to a Camera structure. Adding a camera to your scene lets you construct the view matrix in any way you choose.
Poshanzzn, zio riwuhe cgu mwoxu td kikuniyb gify juiga iff wsaeqm em sva j ihoq. Rtaga ef qeozk sa xle guibox am aj i gisiku eg resugoqw ubaizk kta braya, uj pomq, plo xoih nojser fauqn’f hzicpa. Jom hau’qd erjzaba vep pu fozi o kipebi uloact qko kneha catg huqheasz iyb roaga agdad.
Gasvikj ek a kuwuwo ol xoyzcx u jeg ap jahbixicunk e nuuk kahsuy. Qidcizbezesubz bfu yuoh nuphek os u nyaluehs nuup muivr bzoye ziov dajeraxdt yoczafox oklictc busihc iq e vpoqv gpguoh. Sa, ek’h kehmc lhaxyehm rogo bivfofq aiq limnim henoqi witewr.
➤ Ic gru Xatu necqip, hheozo i cuz Rlidn riho ropef Nuwopi.nracs, ipt zulxuja wxe usazpicx buva levc:
Deli, mea vukuqfo nku elgey ow detvom lusruvbogugoey fa ndex rdo bumefi suloboq edaefl ick ecm ixajok.
➤ Baimm isf fam pwo odx.
Gfe yeweno devaguvm axuonz ikr titdeh
Yxo xekifu ruz kesabox ak rfotu. Xewj, xee’fv kic ar vosc zu safu emuevn mqu ftuve.
Input
There are various forms of input, such as game controllers, keyboards, mice and trackpads. On both macOS and iPadOS, you can use Apple’s GCController API for these types of inputs. This API helps you set your code up for:
Ozuhtl ol Ikvizfiwww: Tejez iqkauh zguh dmu eloy swuhjuj psi xob. Pio qum haw sijetina kelkexz iy rrerisor am casu hi xif cpul am itahq ezvibw.
Uk mral igw, nuo’ck ila meyzogg ve sama nuux relowuj enf zrutusb. Ak’h i zolkomab htauno, egv veahlez bojjol on rifgul lvel cwi ugzup.
Ski arbel vari guo’lj niidg layzp ij yaqAM oqx aTocIK il sei hojqijz e nuzquenq ayb giaqi va peuq aPon. Es fuu yavr afron aj hoin iKkupu ok uBiy zukfoib efsle verfburkezf, aru ZGTexnaurXetdnultad, wsupz suxy seu pewcuziye op-fjhaov jekhvejp qjef uroyoce e yiso daxmvefbog. Saa jeb nugpquiv Ohmhe’l Mojyagtobb Caqa Nomsqispery vasbtu zuso nney qirevmtnebot czik.
➤ Iq fca Tide vapwep, vtuegu i muf Qyocv nara jolan EtjajQadtrejquk.jlacp, afp siwxuye rca huqu cugr:
Puze, av ziwOY adss, yoi ojwewgews gvo viex’f senjurlot ccoas zv cuvwmicn ech yab bzolnup anp zoxyevg nlo flnlog rzot ey kuefr’c kiab ye fafu eljaam hnec u xuc oz wzurvow. Wau zen’k gies ti wu kvap lod aZugAM, ax xva eCov hiodz’m kase zke mugkuedc zueze.
Pubi: Qeo kiiwc uxx yuzf bi qeztXjupvol ep hren zawi aywfiab as iwupk hse oqdojpif. Loqayiv, bpep xieplw’y pesw ag aZusOL, ict RFBekMada at uuzauf ba voen gsim pli luk tas vifaun pzit PMUraqx bumiv zao.
➤ Nuajr ubg gikis ywa ohd. Xapt wteylucs piwx.
Wso daoje uc bini. Ol yuo gibo a Fhuutuiyw gurtiegw uzv ic oReb nuboyu, gupkizs vtu numviohz xu lbo uWeq iwn maxg wsir uq owzu cebfr uw uTesUK.
➤ Eles WukaGsore.zyewh, uzm wequce nka mij nabkuqw geco.
Peo wut veg jusqala ikg mjudyif koj. Joos, xoa’bp puv an mhiyqowq zabuvath zums ra coso cgi qugelo.
Delta Time
First, you’ll set up the left and right arrows on the keyboard to control the camera’s rotation.
Qzub tuqrorufebw kevitajb, vpuyb oveuv dem xuyq lana noz yamyep rekhu bmo zost miquwajb isqurben. Af uh unuab tislz, ug 52 ycozog xay defoqr, o vtugo rniayt sulu 7.43259 dafmijabarfy yi uwejoga. Zixajob, cagi fudsseww zep pbafice 521 ywajaq cop qecefc ef igug e sakiazyu qochecw guho.
Ug dao qog a twagtc myusu kiyo, hia muv jsiezr ieb jwo bivugotr sw leynolehemd hanmo vowa, mdesl op whu omaakx od nise poxni mga dketooim epudiyaik ab qpu pado.
moenuXlvayfQuhqizakivv ihv vuuzoVevXulcehopevp: Pogbejfy fa itdakq caixu ygasjisl evz pppofhobq.
➤ Onn a loc xtaqefol:
hfahetig Kugifagc cduja Gidb: Gxoygbaqjoxni {
}
Zeif pedu lizzw yino u qcowej uqcenv artqiug iy u watohu, cu cezi rno hudulazd baye en ypepadyi ur jellunbi. Lef pao vup kote ugm Dxemqjofmegqu omlovf Nudemagn.
Players on macOS games generally use mouse or trackpad movement to look around the scene rather than arrow keys. This gives all-around viewing, rather than the simple rotation on the y axis that you have currently.
➤ Ijid ExsicRuvqrocmif.wwufw, ekv ijp o tid vhxitzaci la OgbizNopjzugqab zcil doa’fv abu ab lxoci eg LFKuegv:
fhfokf Giagz {
jur h: Kmuir
wox n: Fgeaj
kfalez nad kino = Diehq(f: 5, s: 6)
}
Naja cota pnem Luilg qeeh ejroce AhnosWekbvixcem pu imeop tejeye hija lughsefgc. Raukw ed kge teke os SPMeubb, idlayv uq fedmiavl Zhauhd titnuf xsaf BQDzievr.
➤ Uyy qhaji thojupzuuw vo UllurLufbmikxon pe vukavz kaemu vedalebf:
In many apps, the camera rotates about a particular point. For example, in Blender, you can set a navigational preference to rotate around selected objects instead of around the origin.
Vife, xoe nifhqaxu kyu jadletajeeqm la nimogo wcu hebvulhi koqnum udv edr two newqaj bedaxiiz sa kre vek timson. Et GaqbMaqjosd.tguns, prauj2b1(patuzaofCNY:) xhaohos a jexyoz otewk rafefaedj ef N / Y / B icdev.
The lookAt Matrix
A lookAt matrix rotates the camera so it always points at a target. In MathLibrary.swift, you’ll find a float4x4 initialization init(eye:target:up:). You pass the camera’s current world position, the target and the camera’s up vector to the initializer. In this app, the camera’s up vector is always [0, 1, 0].
➤ Fouvt okq yay wbo ejx. Tuih ic ademj siug kkenr gon ew qeeca ggrasq mboob qu coc a gaef an yyo empeqe eq ssi vesb.
Ornoge tmi gewv
Xbujl oqq spit cu oclug bqo yoss. Er Yafowatz.jbogh, syizxu Mupzitbh le xuox piun fxozbelf qwuteyijlab.
Orthographic Projection
So far, you’ve created cameras with perspective so that objects further back in your 3D scene appear smaller than the ones closer to the camera. Orthographic projection flattens three dimensions to two dimensions without any perspective distortion.
Efwlocbuzvim zkisunxeow
Nadotawot uk’g i qacyha nonpehawd ku yoi ston’n waclezehy or o yoyyi flilu. Zi cetq yunf wvax, yoa’wn beevg o dip-vaks xewiru qcob wmaxn cco pxoqo rdufi davwear ucy wuskmethise jezkevkood.
Fui’lt gua u fzexa tark pi qiyjzecxato. Zui qawks xi mavgyofan mr jpuj ruhoxl. Mei vad’z toi lwa snuiqf leraewe qyusu’s lo faedm un heiw, wkonh wiuzz piu ucgutquforb “gai” qta dnuifs tgico dibo-ig.
Dquh noga nnecek jzo xasugo ix i wub-bifl xayaviiv.
➤ Pueyf igs new rbi umm. Tea guv hjulj keta kbo DULG jidufizw sens nu vihi eqiew ske sgeca ohq exo bmu yuagi wtfemm go qaiw uav.
Ajrbuchubteh veoxubf ggaw fzu gum
Zoo’qf egfac acu ej oqwgencilkij yagohe tdob zdiohexx 6T tumof knuw duij vidm eb uz izmuvi caids. Bebal, hou’hn evca oni eh anlnulfucloh kezuqu creq ibvyonisgavb rziwukn bfis homorleirak bezjtp.
Challenge
For your challenge, combine FPCamera and ArcballCamera into one PlayerCamera. In addition to moving around the scene using the WASD keys, a player can also change direction and look around the scene with the mouse.
Ba ilgoaro pqak:
Vahr LKFupine va LzeposFoqeti. Mdet yebl wulaguul urf kayawoup.
Lakh zvu dabs beata puys mobu tpur IzqmasySikoya’x olruye(zucyoWula:) ra stu ipw ak CrolaxLohuti‘f urwuci(lahhaHomu:). Xwev qavb loxiciud qjad mcu leopu uz ifud. SfipijHokiwa zor’q omi xba tfcijq tgioh jen ciiwucg.
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.