 So this is the talk all the parents are waiting for, hopefully this will explain why you do have to do maths to be a video programmer, hopefully. So to not cut into his talking time, please give a very warm welcome to Matthew and Maths and Video Games. Okay, thank you very much. Yeah, so I'm Matthew Scruggs, I am a PhD student at University College London where I study maths, but I'm not talking about anything to do with my research today, I'm going to give you some bits of maths applied to video games. So mostly this talk is retro arcade games, most of which you can find in the arcade here, and I'm going to do some mathematical analysis, so maybe some mathematical over analysis of these games. So first of all, we're going to look at everyone's favourite game, Pac-Man. So if you've not played it, which I hopefully everyone in the room has played Pac-Man before, in Pac-Man, you play as Pac-Man, you have to eat the Pac-Dots before the ghosts kill you, and the dots are arranged in a maze like this. And when playing Pac-Man, the kind of thing you might wonder is whether it's possible to go around and eat all of the dots without having to double back on yourself in various places. And this is something we can work out, and this is related, so the question is we're trying to find out if Pac-Man can go around the maze and only go down each road exactly once and not have to kind of go down a bit and then repeat back on himself if there is a route that does that. And this is related to a very famous problem maths that many of you may have heard of called the Seven Bridges of Kernigsberg. So there's a town called Kernigsberg that had seven bridges. If I colour the map, you can see that the green is the islands, the light bits are the lake, the rivers, and the orange bits are the bridges. So the bridges were arranged like this, and the story goes that in Kernigsberg, the residents of Kernigsberg would like to go for walks and would try to go on a walk and try and cover each bridge exactly once. So maybe something like this, you go over that bridge, over that bridge, around here, there, and then the resident on that walk is then annoyed because they've missed that bridge at the top and the middle, so they've not successfully managed to go over all the bridges exactly once. And this became more and more famous, this problem, because as they tried it more and more, it became more and more clear that it was very difficult to do this. And in the 1700s, I believe, this man, Leonard Euler, came along and he explained why this was impossible. And he did it by first of all simplifying the problem. So he said that actually the shape of the islands, the shape of the bridges, the distances are kind of all irrelevant. All that matters is the islands and how many bridges are between them. So he drew dots for each of the islands, and then drew lines where each of the bridges went. So instead of having any of the geography, you don't care about that, you just know the two connections between this island and this island. And then we can ignore the map and now we are asking if you can draw the black lines on that diagram without taking your pen off the page. So we've made it a slightly different problem. And he worked out that in fact for Kernigsberg, this is impossible. And he did that by looking at a point. So if you look at, if you choose one of the islands, this island has an even number of bridges connected to it. And this means this is really great if it's an even number, because if you go onto the island, you can come off because you can pair it there just because it's an even number. If you come on again, you can go off. So if there are an even number of bridges on the island, you can always go off every time you come on because you can pair up your edges. But if there's an odd number of bridges, say this one here, you're going to come onto the island again and now you're going to be stuck because there's no bridge to pair that with to come off. So if that is your last island, you're very lucky and you can stop because you've reached the end. If that's not your last island, you're going to fail because you've got stuck somewhere. And similarly, if this was the first island, you could have come off that bridge and it would have been fine. But if it's anywhere apart from the first island or the last island, an island with an odd number of bridges is going to be a big problem and make it possible. So back to Kernigsberg. There are five, three, three and three bridges coming off each island. You'll notice five and three are both odd numbers. So we have four islands with an odd number of bridges. So therefore, it's impossible to draw Kernigsberg, to walk around Kernigsberg without going over a bridge more than once because you're going to get stuck on more than two of these bridges. Okay. And now we can apply the same thing to Pac-Man. But first of all, I should update you on the story. So this is a very old story. Kernigsberg is now called Klinengrad. One of the bridges was destroyed during the war. Another one's been built. So Klinengrad now looks like this and there are the islands, there are the bridges. And it's actually now possible. So you've got this kind of graph going on. You'll see now there are two odd islands and two even islands. So if you start on one odd island, finish on a different odd island, you can do it, something like that. So now actually it is possible to walk around what used to be Klineng, what used to be Kernigsberg and visit all of the bridges exactly once. Right. Back to Pac-Man. So we want to ask the same question for Pac-Man. We want to ask, is it possible to go around the Pac-Man map only going down each route once? So again, just like we did with Kernigsberg, we replace all the positions where more than one little routes intersect with dots and draw lines between them and you get this. And the dots that I have coloured red have an odd number of bridges coming off of them. And if we don't need to count them, you can immediately see there's more than two red dots. So it's going to be impossible because you're going to get stuck on all of those red dots. So we have successfully worked out that Pac-Man is not easy. There is no way of completing Pac-Man without having to double backing yourselves. But that's not really enough information to kind of win at Pac-Man. We want to ideally win at Pac-Man. We know we're going to have to do some repeating, but now the next sensible question may be which routes do we want to repeat? And this is known as the Chinese Postman Problem. It has one of those most commonly called the Chinese Postman Problem. And this question is now, given a collection of islands and bridges or Pac-Man routes, what is the shortest route the postman has to walk so he delivers letters down every road? So which edges do I want to repeat in order to make this possible? And one way you can solve this, the way you tend to go about solving this, is you pick some of the edges to double up. So maybe you could double up that edge on the bottom and that edge spiraling up there. And you can kind of pretend that, instead of just repeating that edge down there, you just kind of make a second copy of it. And now all the islands have an even number of bridges, so now you can go around the map. But you can do this in different ways. So for this, for four islands, there are three different ways you can add these edges. So you kind of do those pair, those pair. The one at the bottom, there's a new edge going from the four at the bottom to the four at the top, which isn't actually a road for us. You'd have to go either around the side or around the other side, depending which one was shorter. So that's your few more combinations to check there. And yes, so for four islands, there are three different ways of doing this. This number increases quite quickly. So if you have six islands, you're going to have 15 different ways of matching up to check. 10 islands, you're going to get 945. For Pac-Man, there are 20 odd islands, odd nodes. So for Pac-Man, there are 654 million different ways of pairing these up that you need to check. And you need to find which of those has the shortest X traditions you need to cover, which is quite a lot. But it's enough that I left my old laptop running overnight and it told me that in order to commit Pac-Man in the shortest possible amount of distance, you should repeat the edges I've marked on that graph there, which would look something like this, which doesn't have sound. Imagine the Pac-Man sound playing. So you go around there, go down here, then we're going to repeat that one. So that's the second time going on the edge. Oh, it's okay. It's not important. Then this one repeats. We repeat this one round there. So that is the shortest route to complete Pac-Man. But you will have noticed when watching that that actually there was some choice that I could have made. So in some places, I went up the side and I repeated the edge going over there, but I could have gone over the top first and then down and repeated the other way. So in this, there's a lot of choice as to which order you do this repeating in, which is useful because it meant I just tried different ones until I found one that avoided the ghosts. But it isn't necessarily true that all of these are exactly the same length. So actually, we might be able to do slightly better than that. So if we look a little bit more about how Pac-Man moves, so let's first look if you're turning a corner. If you press the down button in that frame shown there, it takes this many frames to turn a corner. If you press the down button one frame later, it's going to take one frame more to go down. So it's important that when you're moving Pac-Man around, you press down in that frame and not that frame so that you save one frame of movement. Luckily, if you hold the down button, Pac-Man will automatically move in that frame. So holding down, holding the direction when you go around a corner is the best way to corner and Pac-Man. Okay, going straight across like that, you might think it is bleeding obvious that this is going to be the shortest way of doing it. You just go straight across. But actually, if you press down in that frame and then straight on again in that frame, Pac-Man actually jumps one frame across as he tries to turn the corner and then straightens up again. So you can cut one frame of movement out of there. So we can do slightly faster. We can cut some frames out of the video. And then you can look at other things. So here, when you've got crossroads, you've got two choices for how you could do. You could do it with two corners or two straight crosses. Oh, interestingly, for this one, if you're going straight upwards and try and do this trick, you actually take one frame less than you would take if you go straight across because Pac-Man moves slightly differently going up as going across. So if you're going up, you shouldn't do this attempt at cornering trick. So yeah, if you look at these two, then doing them both as corners, you get 50 frames. If you do them both as straight lines, even with the trick, you get 57 frames. So wherever we've got crossroads, we should try and make both of them into turns rather than going straight across at all. And similarly, we can look at positions like this. And the one on the right is the shortest one there because the one on the left, it has a double bit of extra distance where it's in the one on the right. You can turn around at the Pac-Dot before you reach the corners. You save a reasonable amount of travel there. So you should always try and do this whenever you've got positions like that. And similarly, if you've got positions like this, then that one is longer and the other two are the same length. So now we know which edges we want to repeat. We now want to find a way of repeating those edges while using these tricks to get the shortest route possible. And there's a few choices and this is one way of doing this. So here I've got, I'm repeating the same edges before, but I'm using these choices for turning corners to make sure it is actually the shortest amount possible. And here is me attempting that. I'm going to go back and show that again. When I go past the little first upwards, watch very closely because for two frames Pac-Man is facing upwards and he jumps back down again. I did have to use tool assisting to get this one working because I cannot react that quickly. So you see kind of like it lurches upwards in the back again. He's just saved a frame. And again here, saving a frame. And again. Yeah. So there's a problem with what I've done there is I did the math. I worked out the shortest routes. There's a few different ways you could do it. A few different choices you have for the routes. But in all of them, the ghosts will get you. So it's not possible to complete Pac-Man in the shortest distance possible. We now have to ask a different question, what is the shortest route possible that also avoids all the ghosts? And that is something I've not answered yet. Eventually I'm going to sit down for a very, very long weekend and play all of the possibilities like this. And then all the possibilities is one frame longer until one of the works and I will find the shortest. But I've not done that yet. So we're going to leave Pac-Man behind, at least trying to find the shortest route in Pac-Man behind. But there's one other thing that's interesting in Pac-Man. When you go off the left or the right of the screen, you come back on the other side in Pac-Man. So if you go off there, you're going to come back on the other side. And this is a little bit weird because if I walk out of that side of the tent, I'm not going to come straight through that door unless I'm very magic. So Pac-Man is living in a kind of weird space. But actually if you think about it a little bit, you can kind of understand what Pac-Man is actually doing here. So I'm going to mark them with arrows now to kind of show that those two sides match up. So we see that when Pac-Man goes off there kind of facing upwards, if maybe his head is facing upwards, he comes back on the other side facing the same direction. And you can imagine picking up the Pac-Man map and kind of bending it round so that these two arrows meet up. And you can imagine the kind of shape you might get if you bend this around so the arrows meet. Hopefully you're imagining something a bit like this. I'm going to leave this for a second because this took ages. Yes, so actually Pac-Man lives on a cylinder and it's not weird. So he's not walking out of the tent and coming back on the other side. He's just kind of like going around on a tube and coming back on the other side. And this is the shape of Pac-Man's world. He lives on a cylinder. And this is quite common in lots of kind of old retro games. Bubble bubbles, another one where it happens. So in bubble bubble you go off the bottom and fall back on the top. You can also bounce bubbles upwards and come back on the bottom. And this is the same but kind of rotated. If you imagine bending it around again, bubble bubble is on a cylinder, which won't help you play any better, but it's a useful thing to know. Right, so that was when we took a rectangle and we said if Pac-Man goes off the other side, he comes back on the other side facing the same direction. But there are other things we could do. We could say if Pac-Man goes off the right, he comes back on the left but he's now upside down. So we could flip on these arrows over and say, what if we did that? And now you can imagine, so imagine that that rectangle floats out of the board and it's kind of, it's now made of kind of flexible material. I'd like you to imagine taking that flexible material and trying to bend it around so that those two arrows match up on the opposing sides and try and visualize what you get. I'm going to show you shortly, but I want everyone to kind of imagine what kind of thing you're expecting to see first. It would look something like that. So it's like a cylinder but it's got a twist to it. This is called a mobius strip and they're really quite fun. And if you come to the math video later, I'm sure we can show you lots of fun tricks you can do with mobius strips. But as far as going off one side and coming on the other side goes, this is about as much as we can talk about, but there is more we can go to if we think about the game asteroids. So in asteroids, hopefully you all know that you fly around shooting asteroids, if you go off the top you come on the bottom and if you go off the right you come on the left. So now it's like Pac-Man and the left right is making a cylinder but you've also got an extra rule going around the top. So we can draw this something like this. So we've got if you go on one side you come on the other side facing the same way. If you go off the top you come on the bottom facing the same way. And again it seems like a 2D game but you can imagine now taking this off making it into bending material and bending it around to see what shape you get. Again I'd like you to try and visualize that first if you can imagine what's going to happen. So imagine taking that bending around so that the red arrows touch then taking whatever you're going to get and trying to bend it around so the blue arrows touch. Hopefully you're visualizing something like this. You bend them around, you get the cylinder and then you take the cylinder, bend it around and you get a donut or torus. I'm going to call it a torus because I'm a mathematician. You're welcome to call it donut after the talk. So actually asteroids is a three-dimensional game not two-dimensional game because the asteroids are all moving on the surface of this torus. But we could play with this rule. Again just like we do with Pac-Man we flip this around a bit. We could play with this a bit. So how about we take the red arrow on the right and we flip it upside down and for some reason the blue one swapped but it doesn't matter they're still facing the same way. So now if we go off the top we come on the bottom and now if we go off the right we come back on the left but we're going to be upside down. So a game on this might look something like this. So you fly off the top you come back on the bottom but now if you fly off the right you come back upside down on the other side and it's really confusing and hard to play. Let's watch me fly around a little bit. Yeah it's when you go off the side and just appear at the bottom you just always hit asteroids because you cannot tell where you're going because it's really odd. Okay so again we can think of this game and try and work out what kind of surface the asteroids are moving on. So again I'm going to give you some time to think about to see if you can imagine what's going to happen. So you want to take this and now you want to bend this round and see what kind of shape it's going to make if you bend this so that the arrows match up. And maybe it's easier to think about the blue ones first because if you do those blue ones we should know what we're going to get by now and then think how are you going to do the red ones and maybe you can imagine something as fast as that. So you do the blue ones and you get a nice cylinder then you try and bend it round but now we have a problem we can get near the torus and we've got the arrows but arrows are facing two opposite directions and you can think you can twist these around you can kind of bend them about but whatever you do you can't really work out how to get these to match up and actually in three dimensions you can't but you could do this in four dimensions. A drawing of it looks something like this or like a kind of 3d picture is a bit like this so it's you've got your kind of cylinder and then you bend it around and it kind of has to go through itself and then come out from itself kind of inside out. So this is a four dimensional shape called a Klein bottle and that is what the game of asteroids with this rule is going to be played on. Okay, those of you that are ahead of me will have noticed you could do one more rule for your game of asteroids. How about if we swap the top and the bottom so now the left and the right upside down and the top and the bottom are also upside down. So the game is even harder to play now so you go off the top and you come on facing the wrong way and also if you go off the side if you watch the asteroids go off the side you'll see they come on facing the other way upside down. Okay and this one is even harder to visualize what's going on so I'm going to kind of show you what this surface would look like. So you have to imagine that you take your surface you're going to bend it around and you get a mobius strip and then somehow you have to also bend it to the top and the bottom match up. What you get is often drawn a bit like this and in that picture I can't tell what an earth that's meant to be but the best way to visualize what it actually looks like is if you take this here so this is a surface which along the line kind of cuts through itself so it's kind of a weird spirally surface that has a kind of self-interception point and then imagine stretching this out a bit and bending it over and then kind of closing that shut so that the two parts touch so it's that surface you bend it round and then close it off like that and this is if you sit down and think about it later you can work out that this would then have the same kind of behavior as the surface I showed you and it's another four-dimensional shape this is called the real protective plane I almost forgot the name of the shape it's a real protective plane and it has the behavior like that so as far as rectangles go we're kind of done with asteroids for now um but you could go further and you could do some games on hexagons um so for example this is the kind of thing you might draw and here you could kind of bend this around and match up the arrows and see what you get I'm not going to get to imagine this but if you look at the green and the yellow you will see it looks a bit like a tourist just on that half and the top half looks a bit like a tourist and if you bend this one round and stretch out of it you actually get a double tourist so it's kind of a figure of eight with two holes and two donuts glued together um okay um so now a few more things about asteroids before we move on so some of you might have noticed the problem when we bent asteroids into a tourist there was actually a problem with what we did um so you all know when you play asteroids all of those lines the same length and all of those lines are also the same length um it takes the same amount of time fly over the top of the screen as it does over the bottom of the screen but when we bent it into a tourist that circle around the middle is a lot smaller than the circle around the outside um so actually this isn't quite an accurate representation of asteroids and that's because when we were doing it we're doing lots of stretching um so it is actually possible to do this without any stretching um to do that I'd like you to imagine that that is kind of like one around the out one across the rectangle and then it's moving around the other one so you can imagine that you're taking your like one orbit around the rectangle and you're kind of moving it around like that so it's like you've got kind of a cylinder but it's doing this kind of movement and if we trace that out this is the kind of shape that asteroids actually has to live on because now if you follow um around each circle you've got the same distance but also if you follow a point on the circle as it goes round um all of those are following the same amount of distance because it's not rotating as it goes um but at the bottom there the kind of the surface shape cuts back through itself um so this again isn't possible in three dimensions but it is possible you can do this in four dimensions and four dimensions you'd have like some extra space there to kind of bend the tours around itself and this would work so I'm going to claim to you now that actually um asteroids is a four dimensional game um so when you go to the arcade later play asteroids you're actually playing a game in four dimensions right one more game that this happens in um is fun and fantasy um if you remember the PlayStation Final Fantasy games they have the same kind of thing where you go off the top you come on the bottom um you go off the right you come on the left um and again this means that if you bend it round um you will get um again the same kind of animation again you bend it round and you get a donut shape um so again in Final Fantasy when you have this map being like this you would expect the world to look like a donut um unfortunately no one told the game developers this and if you play the game they show you a lovely picture of a sphere there which is definitely not what you're living on um but to finish I kind of I thought I ought to explain to you what's wrong with saying that's a sphere um so I should show you what goes wrong what actually happens when you do this bit so this is the world map you're probably most used to um and I'm sure you know if you fly off that side you come back on this side but unlike asteroids if you fly off there you don't come back on the top you don't kind of go to the south pole and suddenly appear at the north pole um you kind of maybe come along somewhere like that um but it's a little bit more complicated like than that so if we look at asteroids on this map this is actually what we look like if you fly around on a sphere on the map so it all looks kind of normal there but as soon as I start flying upwards you see I do some kind of weird curving and bending and you go to the top and you become huge and move really quickly and this is all because um the earth is a sphere and the map is a flat surface and because the sphere is curved and the map is not curved there's no way of perfectly representing what the sphere would look like um so however you draw a map um ideally you draw a map where angles and the size of things on the map are the same on both the sphere and on the surface um but it's impossible to do this perfectly um so the one I've shown you is called the macata projection this is the most common projection um and this um so because you can't do a map perfectly you have to choose some things you'd like to keep from the map um and some things you're going to have to lose and the macata projection was chosen so that if you are on a boat and you pick a bearing from north and sail straight along that that will be a straight line on the map um so that's actually not straight on the sphere because as you go downwards you should actually curve in a straight line but if you're on a boat being rocked about it seems straight to you so if you follow a bearing it looks like a straight line on the map and that means this map is very useful for navigation but it gets things very very wrong by the north and south poles um particularly the arc dip becomes huge like your ship becomes huge as you go down so an alternative proposed to fix that is the gull peters projection which is a different way of drawing a map um and in this one um the angles are all completely messed up no one has any idea what any what angle is anywhere but the areas of everything is the same so in this one um the relative size of the uk to australia is actually correct Antarctica looks a lot smaller than the map you're used to um so you can fly mass asteroids around this map the gift takes a while to load um and again you can see you can see this time when you go towards the top you don't get any bigger but you kind of get weird and crushed and start moving really fast and really odd directions um so I find this one really hard to play because if you go actually straight upwards you just appear somewhere miles away because you get crushed really flat and then appear somewhere else um yeah so that is the gull peter projection um there are lots of other ways of doing um 3d a sphere onto a flat surface I'm going to show you one more than I will finish um the last one I'm going to show you is the craig projection sometimes called the mecca projection um so this is a map that's centered on mecca um and if you go anywhere in the world say you go to leadbury in the united kingdom um and you put this map down and you measure the from north where you are the angle round to the red dot there that will tell you the angle like the bearing from where you are to where mecca is so if you want to say pray towards mecca this map can show you everywhere in the world which direction you should pray in um so kind of if you take leadbury and draw the arrow there that is the angle on the map shows you the angle in real life that you should face in order to pray in the right direction um although this representation is a bit of a lie you might notice there's no Antarctica um in fact a whole half of the sphere is kind of hidden away actually um would have to go on top of what's currently there um which is what I did when I made this in asteroids um so you can watch me fly around on the surface for a little bit here so you can see if you fire towards the cross in the middle which is where the center of this map is um the the the bolts actually go through the cross um and if you fly around away from it you bend all over the place um and these the bits on the side are kind of infinitely tall as well so you just get lost sometimes um it's not really useful for navigation but for finding the angles to certain places it's really useful um yeah I dial out in this one um right so that is the last bit I have to show you um just a couple more things first of all I've so I call the game I've just shown you masteroids obviously um if you want to have a go at this if you go on that web page on my website you can play all those levels of masteroids I've shown you their all gifts taken from there um and hopefully this evening it may also be coming to the badge I'm going to have a go at getting that across the badge so tomorrow I'm not getting masteroids on the badge um but otherwise that is the end of what I've got to say um so thank you very much for listening your applause sorry um yeah and so I'm going to not do Q&A here I'm going to go over to the massive village I think Hannah Foy is also going to join us for pre her talk Q&A um so if you go from where we are on that bottom of the arrow up to the massive village by the lake there um I'll go over there and chat to anyone that wants to chat do you have that as a mccarto projector as well do I have sorry it's one it is one right um yeah that will be a project I guess that's probably mccater it's probably too small to notice the difference well um as we are out of time as you said before no Q&A so thank you once once again Matthew