[SOUND]. Hello. So you just heard about how you can use a physics engine to control the movement and collisions of a variety of objects on the screen. What we're going to do now is, is look into a real example where we develop that into a game with, with nice graphics and sound. And I'm going to begin that inspection by figuring out how we can connect sound events up to this collision events that we've seen before. Let's look into the code of the, the, the example here. [BLANK_AUDIO] So we're modeling this as a little droid character that can be bounced around the screen, and it smashes into crates. So, we got [SOUND] an array of crate uh, [INAUDIBLE] crates, which is an array of physics engine body objects. So these represent all the crates that move around the screen. We've also got another entity which is a physics engine circle called Droid which represents a little character that's going to bounce around the screen. Finally, we've got these walls which the Droid might bounce into and also the crates. So what we're going to try and do is hook up the audio engine so that every time a collision happens, it triggers an appropriate sounds. First of all, in here, you can see we create maxim as usual. and then, we've got two audio players, so let's just look at the variables up at the top there. So these are all the audio variables. It's all we need. [SOUND] We've got no maxim, we've got two single audio players, and then we have an array of audio players. As I said earlier, we had this array of crate and body objects. so for each of those crates, I'm going to have an individual audio player, so that will allow me to simultaneously play sounds for all of the creates. So you can hear all of those creates hitting each other at the same time. then we need a single sound for the, for the droid, because there's only one of those in the screen, and then a sound for the walls. Okay. [BLANK_AUDIO] Here, we basically load, load a file, which I prepared earlier. And then, another files. I've got droid sound and a wall sound. The droid sound is kind of funny vocal sound that I made and the, the wall sound is a clanging metallic sound. You'll hear them shortly. And then I tell them both not to loop [SOUND] because their one shot sample, the [INAUDIBLE], the 20 play all the way through once and then stop when, when, when a collision happens. Next, I create the array of crate sounds. So, they're, they're again, they're just all audio players, but they're stored into an array, so it's more convenient to work with them. And we use the same method and we load a [UNKNOWN] file into each one. Now, it's worth noting here that, that we're actually going to store that copies about [INAUDIBLE] file data but we need to do that, so that we can playback easily and with several the same time. So we load that file up and got all those [INAUDIBLE]. We tell them up to mute either. The next thing is we hook up the collision function, which Marco mentioned earlier, which cool every time a collision happens between two of the, two of the objects in the physics world. So collision is called. It tells us that two objects have collided and it tells us how hard they hit each other if you like. So let's just have a look at that function down here. So here. [SOUND] we've got the, the [INAUDIBLE] two bodies coming in, which are the two colliding bodies, and then we've got the strength of that collision. And we've got some logic here, which basically decides whether there should be a score or not. And Mick's going to talk a little bit more about that later. But then, here's the logic that decides when to play sounds. [SOUND] So, first of all, the first trick is that we know that the walls have a zero mass. The walls are set up having a zero mass. So in order [INAUDIBLE] to figure out whether one of the bodies that's been hit is a wall, we just test the two bodies. So we say b1.getMass, that will tell us the mass. And we test if it's equal to zero. So if b1 is equal to 0 mass, that means it, it's wall, because the walls are the only things with zero mass. And then if b4, we got this double bar side here, which means all, so if b1 or b2 have zero mass, it means at least one of those things that's just collided is a wall. And that means we're going to trigger the wall sound. So you can see we cue the sound up and rewind it to the beginning. And then, we set the speed on the sound. So we're actually going to make the sound more dynamic by assigning a speed based on how hard that, that thing hit the wall. So if it hits the wall really hard, it will play the sound faster. So it will be a high pitch sound. If it hits it slower it's, it's, it's a low pitched sound, which kind of makes sense because it, it's this idea that the sound plays more slowly with a slower collision. And, finally, we play. Notice I'm scaling the, the impulse into a range of zero to one there. because that, the impulse comes in, can be quite high value. So it's, I, I, I did a lot of tests and they come in the range of 100,000. So I'm just scaling it down with that. So again we, we, we've got this thing where you've got different types of data coming into your, your code and you need to know what kind of range that data is in, and then you can scale it into a range that's appropriate for parametizing audio and parametezing graphics. So lot of the tricks and how to make things work properly is all about knowing what range or numbers you're dealing so with the accleratometer what range of numbers do it generate. With the physics engine what range of number does that generate and being able to correctly scale the numbers to map to other things okay so the next bit of logic. there's a quick test to see if either the things are the actual drawing object, so if we've [UNKNOWN] the droid that means we've triggered the droid sound. So we queue it up and again set the speed in the same way and then hit play. And then find with that logic is which of the crate sounds need to be played. Okay. So, this loop here is the same four loop we've seen a few times now. which loops through the array of crates. And tests each of the, the bodies that have come in. So the, b1 and b2 are the two bodies that have collided. We want to check if either of those is one of this, is one of those crates. If it is, then we cue it up, and trigger it. And I've done a bit of fine tuning here. I've got 10,000 here instead of 100,000 because I found that the crates in this game tended to collide more slowly than other things. So I've kind of scaled down the, the, the number there a bit to reflect that. So again, it's a bit of tweaking. It's not always super precise. It's about playing around with it and tweaking it so it works how you want. Okay, so the speed is set like that. So a slightly different way of setting the speed. Finally I'll play it, okay, so now what we're going to do is just run this on the Android device. And, you'll be able to see and hear it. That's just building now. So I just lift it up so everybody takes the screen, because this one works in Okay, so there's my boyd. Mick is going to talk to you more about the graphics and how to make those really good. but at the moment, I'm just, just worried about the sound. So, if he, if he hits the walls, [SOUND] [INAUDIBLE] that's the crates colliding. So that metallic sound is, is, is the, the wall sound. And then the tappy sound is the crates colliding. So you can see if he hits the wall really fast, if I fling him against the wall, [SOUND] [INAUDIBLE] [SOUND]. Okay. One more into the wall, just to demonstrate that. [SOUND] [SOUND]. >> Sounds pretty good. [MUSIC]