[SOUND]. This week I'm going to talk about Physics Engines. Now, physics engines have a really powerful tool to get animation into action in in, in interactive programs. The great things about physics engines is that they use mathematics, physics, to simulate the movement of objects. So you can get objects that move just like they do in the real world. And really if you've got a physic sense you don't have to write much of the code at all for that animation, because the physics engine will handle a lot of it for you. They're very hard code to write yourself, but once you've got all that mass encapsulated. You can just create objects and they will behave much as they would in the real world. You're going to work with a physics engine called Box2D, which does everything in two dimensions. So, it's just slightly simpler than a three dimensional physics engine. And it suits our, our needs very well because it's, we're all writing 2D 2D programs, in this course. It's been used very extensively. Probably most famously in the game Angry Birds which, you've probably heard of. Considering it's one of the most successful video games of all time, by now, I expect. so, what is a physics engine? A physics engine works with objects, and it moves objects around. So here we have a circle and rectangle, and these objects are moved by forces. So as in the physical world, forces are types of objects and that causes them to move around. We'll talk in detail about both of these things in this lecture. But first I need to sort of talk a bit about how the whole world is represented. Now, you actually need to have an explicit representation of the physics world as well as the objects in it. And I'll come back to how you create it. But before I say that there's something a little different about how the physics world is represented than what we've done up until now. Up to now we've upcentered everything in terms of pixels on the screen, and that's fine. That's, we're dealing with the screen. We can work all our distances can be our pixels. But this is the real world isn't broken up into squares. And if you really want to simulate the real world properly. You have to seem like there's a continuous world, things don't change that much. You just get rid of the grid. But you're still using x and y position. these are no longer whole numbers., they can be fractional. but the x and y are still our fundamental representation of positions and directions to the work. But I'll talk a little bit about how to discuss x and y but deeper mathematically. Because that will help a little bit when discussing physics engines. I won't get heavily mathematical, I'll just prepare you with a few things. X and y aren't really separate things. They, they were just a way of representing a position of an object on the screen. And we can combine them together into a single thing we call a vector represented by this line in this diagram. So here we have a vector. Well it's showing now like v. We can think of it as an x and a y, that's our position. And we can also think of it has having direction and a length. It looks like an arrow, it has it's pointing in a certain direction and it looks align, it has a certain length. We called the length align filled with two hallow bars, across it, we might need that notation for much. And once we've got vectors, they're not just the representations the x and y's. Mathematically vectors also include a bunch mathematical operations we can do with them, some of which are quite powerful. So, for example if we negate a vector, do minus v, which is just minus x minus y, it points in the opposite direction. If we multiple the vector by a number, say the vector multipled by 1.5 is 1.5 times x, 1.5 times y. We change the length of the vector gets longer in this case and then we can go further now. If we've got a vector representing a position, we've got another vector. And we put that vector at starting at the end points of the end point of the original one p, except v starts at the end point of p. We can take it, it can take us to a new position. And that position is actually the resulting of taking p, taking v and adding them together. So we can add a vector v to vector p and get a new vector p2, and if we do that, we can do the opposite and subtract it off. So if P is px, py, p2 is p2x, p2y And we know from what we just done that p2 is equal to p plus v. Well that means that v is equal to p2 minus v. And so we can subtract off p, p for p2 again, by subtracting p2, p2 from P2x. And that, if you look at the diagram here, V is the vector that goes from point p to point p2. So by subtracting one, the vector of one point from the vector of another point we get the vector that goes between those two. And I use that in a couple of slides to do a calculation. So back to our physics engine, [COUGH], as I said, we've got objects. We've got forces applied to them, and they exist in world. Let's look first at the objects. So we've got a physics object and this is a simplified representation of an object. It has a shape, a simple shape. Or say that we mostly deal with circles and rectangle and occasionally we'll deal with some more complex objects. But I think for the example I think just in circles and rectangles. It's got a position, it's got an angle, it's going to take [UNKNOWN]. And it's got a mass, which is a physical quantity which is how much it resists forces. So you need for, if it's got a high mass, you'll need more force to push it. So these are the things that define physics objects. Physics object exist within a world. So, this is how you create a world. Create a new physics world by, it's an object, you have available physics equals new physics. And you give it the size of the world, which is the width and height of the, of the, the window normally. And we use this, which, basically this is a representation of your program. So by passing, putting, passing this into your physics world, you're linking it to your program. Then, the, the next thing we do to the physics world is set the density. That's quite important because it's used to set the mass of objects, so. You don't need to set the mass of an object explicitly. It will just calculate from how big the object is based on the density, so the bigger an object, the more mass it's going to be. But the density is going to control how how massive each of your objects is. So if you increase the density, then objects are going to resist movement more, you're going to need bigger forces, they're going to move a bit less. The exception to that is if you set density to zero. If you set density to zero, then you're obviously not going to get affected by any forces. so they'r not going to move, they're going to be static and in the physics implementation. You automatically it creates for you static objects at the sides of the screen, so things bounce off the sides of the screen. So now we've seen created that physics world, we can start creating objects. And that's a little bit like drawing objects in, in processing. We've got a function of physics .createRect, so it's the physics object that's creating the rectangle. We're creating a rectangular object. And just as in processing, to create a rectangle we need four numbers, but it's a little bit different, like in placing the first two numbers at the top left. But the next two numbers are the bottom right of the rectangle, not the height and width, so that' something to remember. Is that it's the top left and bottom right rather than the top left weidth and height. We can also create circule objects. Again a different function creates circle. That's three parameters, x and y positioned at the center of the circle. And the radius of the circle, how big the circle is. And the great thing is, once we've created these physics objects, they will just be part of the simulation. The physics world will just carry on simulating and you don't really need to do much at all. And you'll already get some quiet compelling interaction. The only problem is, it's just the physics world, there's nothing graphical on screen. So, for, for your physics objects you also need to create a graphical object. Now a graphics objects is much what we, is what we're used to. They've complex shapes, they've got colors. They could be images, so this is exactly what we're drawing. Now, we're not necessarily going to have a representation of an object in there. but we're just going to use all the same drawing trechniques that we've been using up to now. And I'll show you an example of how to do that. The most important thing you need to do is link your physics to your graphics. So you need to get the precision and rotation of your of your physics bodies. And then apply that to whatever graphics you're going to do. So, first we get the object's position, body.getWorldCenter. So that's the center of the object in world's coordinates. So if you remember I went back we've got the pixel coordinates that we use on screen. And we've got a set of vector coordinates that we're using behind the scenes in the physics engine. And sometimes you want those to be different. In fact, most of the time you're likely to want it you're having one unit per pixel is a bit small. roughly the physics world works in meter. So one unit is a meter in terms of physical simulation. So having one meter per pixel seems really small, so you might want 10 or even 100. So, to get those coordinates back into pixels, you call physics.worldToScreen, so world coordinates to screen coordinates, screen coordinates to pixels. The result of that is a vector, of the Vec2 type, so it's a variable of Vec2, which is a vector, and it has a position. You can also get the angle of the object, physics.getAngle, and you pass in the object itself. And then you can use these for translate and rotate, just as we saw last week. so, you need to get out the x and y positions of the vector, plus .x, plus .y, and put them in the translate, and then you rotate by the angle. I've converted that in to radiance, again as we saw last week. So we don't, as the original angles and degrees, and I've putting in a minus sign because the directions. In the physics world, it's different from directions in the, in the screen worlds. but just put that in, and then we can do whatever we like to do. In this case, I'm drawing an image. And you must remember to put pressure pot matrix around those to make sure that your objects move independently. [MUSIC].