Transcript for:
Physics Engines in Video Games

all right welcome to our week on video games kind of exclusively dedicated here to video games we're going to talk quite a bit about them but um what i want to start with actually to start off this week is talking about something called physics engines which may already sound a bit intimidating if you're not really a science person so what do we mean by a physics engine well a physics engine is basically a software program used to simulate physical phenomena which is a really simple way of saying the physics part of it so what do we mean by that well in most computer games in video games objects are moving and so a physics engine is the software basically that's written into a video game that helps determine how objects move so you'll see here it says physics engines enhance players enjoyment by simulating complex physical characteristics of a virtual world and these tend to be approximations of real world physics because they have to happen quickly in video games you don't have tons and tons of time to do this so in actual physics experiments if someone wants to to figure out how an object is going to move they're going to spend hours and hours and hours on massive super computers to get these very very precise answers you don't usually have that kind of time or if you do you're going to lose interest in video game pretty rapidly so physics engine is designed to approximate this so you can see a basic outline for what a physics engine here does so you the user or player are going to do something and then the physics engine which is the software is going to make some decisions based on okay you did something how does that change position of certain things do those things interact if they do interact where do they end up and then i'm going to show you where they end up to you know display it back on the screen right so that's going to be the render portion of that so yeah let's talk a little bit about physics engines and the kinds of calculations that are involved so the one of the simplest video games ever made pong is essentially a pure physics engine right and so what we're looking at here is angle of incidence and angle of reflection kinds of things uh the same concepts um apply to the game of pool for example or billiards or whatever right so if you take a pool stick and you hit the white ball and you hit the cue ball the cue ball is going to go and it's going to hit a wall of the pool table and then once it hits a wall it's going to bounce off that wall where in the way in which it bounces off that wall is going to actually be very predictable and very calculable and so it's using this kind of thing that people are able to play people are really good at pool are able to play pool i can play pool but not very well because i don't do this well but essentially the idea is all other things being equal right if we have an object here shown in with this blue line and it's the object is traveling along this blue line here and it hits a wall so it's going to create what's called an angle of incidence thus the eye here and so the angle is the uh the degree the angle created by the line of the the object with the normal line so the normal line is the this end line here is this dotted line that's coming in perpendicular to the surface so here you see theta greek letter theta i so the angle of incidence and so that's this distance here right so we talk about a 90 degree angle would be is uh the angle created by the wall and the normal here so this line and this line create a 90 degree angle and so this incoming line here is creating roughly a 45 degree angle right because it's halfway between uh here and here and so the angle of incidence once it hits this wall it's going to bounce off this wall if it were a pool ball or something like that and it's going to bounce off it's going to be reflected off in an angle of reflection and that angle of reflection is going to be equal to the angle of incidence right and so in pong what's happening right is that you've got two people who are bouncing you know two little two little platforms that you're moving back and forth and you're bouncing a ball between them right and so depending on the angle that you hit that ball it's going to affect the angle that it bounces off angle of incidence equals angle of reflection and so you can have some degree of control over the angle at which that ball is going to bounce off your your pong paddle essentially to go to the other computer player and so that's all that's happening that software behind that game right is it's calculating all right if the pool ball comes in and it hits here how is it going to exit it's just going to exit off this direction and just that over and over and over again okay so this is kind of one of the simplest kinds of calculations that you can do in a video game um is this kind of thing but they get more they get more uh more complicated and i'm not going to ask you to do any of these any of this math um mostly what i'm having you what i want you to do is appreciate why making video games is really hard and why i'm having to do a board game for your project instead of a video game so a following body so that would be the next kind of level as it were of complication so here i'm showing you a basketball i think that's a basketball at least sure looks like a basketball and if you drop a ball from a certain height it's going to take a certain amount of time and it's going to drop and it's going to hit the floor because gravity is pulling it down to the floor and it's going to take certain amount of time to do that and when it hits the floor it'll be traveling at a certain speed all of that is calculable basically and unfortunately it's not a simple linear calculation it's exponential because it doesn't travel at a set speed so when you drop a ball right when you first let go it's at speed zero right because you've been holding it still and the ball speeds up as it moves down towards the ground so that by the time it hits the ground it is traveling faster than it was you know in halfway in between so this is called acceleration and this is the same concept with your your car right we talked about your car accelerating if you hit your accelerator you you start going faster and faster and faster um over over time and there are there are some limits to this you know friction and just you know all these other kinds of things we're going to ignore those for the time being but for most objects in on earth they accelerate at a speed of 9.8 meters per second per second which is how we calculate acceleration so that means we think about speed as distance per time so the second wave most many of you probably don't think in meters but you might think in feet so the same if you translate 9.8 meters to feet it's 32 feet per second per second so essentially uh what that means is that let's say you drop a ball and it's at it's at speed zero right so after one second it's going to be it's going to be traveling at 32 feet per second after two seconds it's going to be traveling at 64 feet per second so basically it's it's what's it saying is that it's accelerating its speed is getting faster every second by at a rate of 32 right and so it continues to accelerate so after one second 32 feet per second after two seconds 64 feet per second and so on and so forth so every second its speed in feet per second is increasing and so that's what that number means that weird notation of 32 feet per second per second and so you can do all kinds of calculations for these kinds of things there's the wikipedia page that i have linked to here has some examples of these so you can figure out for example um it's showing down here the distance that an object travels after a certain amount of time the time it takes for an object to travel a certain distance the velocity at a particular time the velocity at a particular distance the average velocity over time and so there's all these equations there's different ways of manipulating this and so on and so forth so that's if you drop a ball but most of the time we're not interested in that for video games what we're interested for video games is things like ballistic trajectory so ballistic trajectory is going to apply to anything any video game where you're jumping right that's that's what happens so when you jump you're going to push off and eventually but as soon as you push off the force of gravity starts pulling you back down and so when you push off you're going to jump and eventually the force of your jump is going to get counteracted by the force of gravity and you're going to fall down right if gravity weren't there you would jump and you would just kind of keep going and so there are again ways of calculating you can see some of them listed here ways of calculating when if you jump at a certain angles at certain speeds you're going to travel certain distances and travel certain heights and there are equations that will calculate this all out for you so for example one of the things that this is showing you is that if you want to travel the furthest distance the best way to do that is to jump at a 45 degree angle assuming that the the force of your initial jump is the same so 45 degree angle will actually help you to travel furthest whereas the same amount of force in your jump if you jump at 30 degrees versus if you jump at 60 degrees you'll actually travel roughly the same distance and this all has to do with angles and times so part of the problem here is right you're not jumping very high and so when you start falling because you haven't fallen from very far you're not traveling horizontally anymore as much and so you don't go quite as far whereas if you're jumping too high then you don't have as much horizontal motion and so there's all these you start getting into vectors basically which is another matthew physics-y kind of thing that like i said i'm not going to make you do any of the math here it's sufficient for you to understand at least conceptually what's going on and so in the background behind all these video games you've got these things running these these softwares that are just running these calculations over and over and over again and like i said there have to be fast so a lot of the time these calculations will ignore things like physics or slight changes in the force of gravity dependent on height right so if you're up on a mountain the force of gravity is slightly less than if you are down on sea level so things like friction or the spin of the earth things that come into play but are very slight most of the video games are going to ignore these things they just don't they don't matter for the intensive purposes of what they're trying to accomplish they need to be able to do these equations quickly so they need to have simple equations essentially right so here's a great example of exactly the kind of thing that i'm talking about right super mario classic platformer game it's all about jumping so every time you hit that that button i think it's the a button it doesn't matter a button to jump right there's a physics engine in the background that's saying all right how high how far and how long are you in the air so every every button that's pushed is saying all right how high are you going to go uh how far is that going to carry you and then when are you going to land and you'll notice right there's other we haven't talked about these yet but there's going to be other complications as well in the case of this little guy right because sometimes he runs into things he hits things that stop him from jumping all the way right and so there'd be some question about well how do we detect that and how do we deal with that and so there's in the background there's a physics engine that's doing all the math for these kinds of things so okay um that i really just kind of wanted to make a quick introduction to physics engines and what's going on i'm gonna stop this lecture here and we're gonna start i'll talk about um some more complicated aspects of this and some applications of this to slightly more complicated video games in the next lecture