1 00:00:00,270 --> 00:00:06,360 We'll come back again in this lesson and we're going to talk about you I'd dynamics and dynamics is 2 00:00:06,360 --> 00:00:12,960 one of the few different physics engines that the iOS SDK offers us and we use dynamics for usually 3 00:00:12,960 --> 00:00:16,160 the lower end to kind of interactions. 4 00:00:16,320 --> 00:00:21,540 If you want to do something with heavy physics engine you know interactions then you can rely on a sprite 5 00:00:21,570 --> 00:00:22,910 kit or the scene kit. 6 00:00:23,060 --> 00:00:29,040 But if you have to do some basic animations that are depending on the physics engine then you can use 7 00:00:29,040 --> 00:00:32,240 the dynamics within your views. 8 00:00:32,280 --> 00:00:37,050 So before you go to school and actually show you how we're going to program this let me show you the 9 00:00:37,050 --> 00:00:38,620 project that I'm going to work on. 10 00:00:38,640 --> 00:00:40,740 So we get a sense of what's happening. 11 00:00:40,920 --> 00:00:46,670 So in this project every time I click this little box falls and as it falls it makes a lot of collisions 12 00:00:46,710 --> 00:00:53,970 either with this barrier or with the view boundaries and it counts how many times it has collided with 13 00:00:53,970 --> 00:00:55,230 different objects. 14 00:00:55,230 --> 00:01:00,920 So once again it falls and it makes a lot of collisions and then it lands number three. 15 00:01:01,030 --> 00:01:02,950 Again so on and so forth. 16 00:01:02,950 --> 00:01:07,760 So that's essentially the goal of the project and the use of dynamics. 17 00:01:07,770 --> 00:01:09,530 Now let's see how we're going to do that. 18 00:01:09,540 --> 00:01:15,780 I'm going to go ahead into ex-cult and start a new application called it you might say nomics a story 19 00:01:15,780 --> 00:01:20,930 about the things that I need or does I need a little label a new label. 20 00:01:20,950 --> 00:01:26,730 I'm going to place it up there maybe a little bit larger such as two five six that's too large maybe 21 00:01:26,730 --> 00:01:30,810 1 2 8 1 2 8. 22 00:01:31,590 --> 00:01:37,000 And it is going to show me the number of the collisions which is by default 0. 23 00:01:37,320 --> 00:01:43,450 Maybe if I go on color which is like purple and then I'm going to have another you I view. 24 00:01:43,530 --> 00:01:44,810 So that's going to be you. 25 00:01:44,850 --> 00:01:47,710 I view that I place it here. 26 00:01:47,730 --> 00:01:55,300 That's like the barrier that my box essentially bumps against So I'm going to make that one. 27 00:01:55,590 --> 00:01:57,910 Maybe dark gray color. 28 00:01:58,170 --> 00:02:03,940 I know I'm going to have to add them into my programming interface so I'm going to come in here say. 29 00:02:04,260 --> 00:02:10,700 So this one I'm going to call it my box which is a level of force. 30 00:02:10,920 --> 00:02:16,140 This other one I'm going I call the barrier barrier. 31 00:02:16,290 --> 00:02:18,000 Now we're almost done in here. 32 00:02:18,000 --> 00:02:22,690 There's one more thing I have to do or this is something we probably haven't done before. 33 00:02:22,740 --> 00:02:24,720 So let me show you how we do it. 34 00:02:24,720 --> 00:02:30,600 And that is sitting an ID for an object and we need to see the ID because you want to be able to find 35 00:02:30,600 --> 00:02:34,720 that object by its eye and draw a rectangle around it. 36 00:02:34,770 --> 00:02:36,720 And that's something that we'll see real soon. 37 00:02:36,720 --> 00:02:37,980 Don't worry about that part. 38 00:02:38,130 --> 00:02:42,600 But setting an idea is here in the storyboards so I'm going to go up here in the class and I'm going 39 00:02:42,600 --> 00:02:50,100 to say there is a label for this object and that is going to be called let's say that your oil or anything 40 00:02:50,100 --> 00:02:53,970 that you want as long as it is a distinct unique value. 41 00:02:54,080 --> 00:02:57,280 So unless it is to bury your ID for now. 42 00:02:57,280 --> 00:03:01,740 So that's all I have to do in this story what we're calling site at in here. 43 00:03:01,770 --> 00:03:07,170 Let's go ahead and add a few things that may form out all of this so it is cleaner. 44 00:03:07,170 --> 00:03:14,870 The first thing I want to add is a white dynamic animator as well as a light gravity behavior so I'm 45 00:03:14,880 --> 00:03:22,050 going to say that I'm going to call this one main engine that's like the engine of the entire physics 46 00:03:22,060 --> 00:03:29,640 essentially that's going to be a dynamic animator and I knew that gravity is going to survive and I'm 47 00:03:29,640 --> 00:03:34,420 going to call it dynamic gravity or draw or gravity. 48 00:03:34,480 --> 00:03:34,740 Right. 49 00:03:34,740 --> 00:03:39,380 The whole thing that's going to be a come at you like gravity behavior. 50 00:03:39,690 --> 00:03:43,650 So this one essentially calculates everything in the scene. 51 00:03:43,680 --> 00:03:48,300 This one adds a gravity to the objects that we wanted to have them to. 52 00:03:48,530 --> 00:03:53,550 So let's go ahead to the next thing and I'm actually going to go ahead and add these things. 53 00:03:53,700 --> 00:03:56,920 I should have done it in the view of the law but I'm going to do it in the touch. 54 00:03:56,990 --> 00:04:02,130 And that simply because I wanted this to happen every time I touch somebody. 55 00:04:02,130 --> 00:04:04,490 So I'm going to say let's do it in the touches and. 56 00:04:04,670 --> 00:04:07,480 And what we have to do is we have to initialize this. 57 00:04:07,560 --> 00:04:15,300 I'm going to say Main engine becomes you dynamic animator and then is going to say Give me a reference 58 00:04:15,300 --> 00:04:23,460 if you say reference view itself that if you do it here within this city you essentially then the next 59 00:04:23,460 --> 00:04:28,830 thing is I'm going to have to initialize my dynamic gravity so I'm going to sync my dynamic gravity 60 00:04:29,340 --> 00:04:36,830 becomes you gravity behavior and then is going to say which items do you want to add this to. 61 00:04:36,860 --> 00:04:42,370 I only have one item so I'm going to have to make an array of my only item which is my box. 62 00:04:42,420 --> 00:04:49,080 So now it's been added there and then I'm going to have to add this behavior to my engine. 63 00:04:49,110 --> 00:04:54,600 So you might have different kinds of gravities and you might have different kind of physical forces 64 00:04:54,840 --> 00:04:57,800 and you have to choose which ones you want to add to your engine. 65 00:04:57,960 --> 00:05:06,180 So I'm going to say Main engine dart at the behavior and this behavior which understands the gravity 66 00:05:06,180 --> 00:05:08,170 for the box. 67 00:05:08,160 --> 00:05:14,020 Now this is all live and it will work properly if we only wanted to add a gravity to this object. 68 00:05:14,160 --> 00:05:17,420 Let's go ahead and try that out and see how this would fare. 69 00:05:17,460 --> 00:05:19,730 Up until this point. 70 00:05:19,830 --> 00:05:25,230 So now that the simulator is up if I touch it will see that the box essentially falls to the screen 71 00:05:25,260 --> 00:05:31,080 and that's simply because the gravity keeps pulling it down later on I would add something that every 72 00:05:31,080 --> 00:05:37,590 time that touches and we want to begin this dynamic animation we reset the location of the box right 73 00:05:37,590 --> 00:05:38,240 up here. 74 00:05:38,400 --> 00:05:40,440 But for now this was quite good. 75 00:05:40,440 --> 00:05:44,000 It showed me that it's working but it doesn't cause any collisions. 76 00:05:44,010 --> 00:05:45,910 And that's the most interesting part. 77 00:05:46,020 --> 00:05:47,550 Let's go ahead and doubt. 78 00:05:47,670 --> 00:05:54,390 I'm going to go back to school and say I have a variable called Collision and that collision is going 79 00:05:54,390 --> 00:05:57,170 to be a collision behavior. 80 00:05:57,290 --> 00:06:03,910 This is the behavior that calculates when objects bounce against each other or bump into each other. 81 00:06:04,170 --> 00:06:08,420 And the first kind of collision I want to calculate is the collision against this barrier. 82 00:06:08,430 --> 00:06:11,530 So I'm going to go ahead and say that have your function. 83 00:06:11,520 --> 00:06:11,920 So. 84 00:06:11,930 --> 00:06:17,130 Because the court is going to become a long coat so I'm going add them inside the new function say this 85 00:06:17,130 --> 00:06:24,510 function is actually called Collision with objects and collision with objects does these things you 86 00:06:24,660 --> 00:06:29,540 first find the boundary of the barrier view that we want to collide against. 87 00:06:29,550 --> 00:06:40,030 So I'm going to say this guy and that guy becomes ads about the tweets of boundary and an. 88 00:06:40,620 --> 00:06:45,870 And I'm going to say the boundary that I want to add the identifier for it is that's a name that we 89 00:06:45,870 --> 00:06:49,170 gave for as very your ID. 90 00:06:49,370 --> 00:06:53,640 That's the identifier I'm going to say get it as a.. 91 00:06:53,910 --> 00:07:05,070 Copying for and the pads that you're going to make is a path like this for a rectangle made of barrier 92 00:07:05,100 --> 00:07:07,260 object dot frame. 93 00:07:07,620 --> 00:07:16,440 So get the ID from that object get that identifier and produce a barrier based on that which is a busy 94 00:07:16,440 --> 00:07:20,320 spot simply because objects that they want to collide with each other. 95 00:07:20,460 --> 00:07:26,260 They can be calculated based on their look based on the fact that they are a white label. 96 00:07:26,400 --> 00:07:29,910 They have to be calculated based on a busy path. 97 00:07:30,090 --> 00:07:36,180 So I have that one something I did for getting your is we never initialize the collision. 98 00:07:36,180 --> 00:07:38,790 So that's something I have to do it in here. 99 00:07:39,030 --> 00:07:47,640 And for that I'm going to say let's have that collision to become a UI collision behavior of the items 100 00:07:47,760 --> 00:07:50,400 and the item again is only this one guy. 101 00:07:50,400 --> 00:07:52,360 So my box. 102 00:07:52,710 --> 00:07:59,790 So that collision is essentially initialized for my box and then the collision itself is being calculated 103 00:08:00,210 --> 00:08:02,820 based on this particular object. 104 00:08:02,830 --> 00:08:08,470 For now while this is the bare minimum of what you could do to essentially calculate the collision. 105 00:08:08,600 --> 00:08:13,920 The other things that are kind of optional and you could add for instance you could add a list to your 106 00:08:13,920 --> 00:08:14,640 object. 107 00:08:14,700 --> 00:08:23,820 So I'm going to say Let let's call it my behavior or my Buck's behavior or anything of the sort becomes 108 00:08:23,870 --> 00:08:34,050 you UI dynamic Eidem behavior UI dynamic Eidem behavior and the item for it is going to be let's say 109 00:08:34,050 --> 00:08:35,980 in this case my box. 110 00:08:36,090 --> 00:08:38,530 So I'm going to say my box. 111 00:08:38,550 --> 00:08:45,380 So these are the behaviors of specific items not the overall engine as we had it in years. 112 00:08:45,480 --> 00:08:53,220 So the specific behavior they want to have for this object I'm going to say it has an plasticity plasticity 113 00:08:53,640 --> 00:08:56,130 of let's say point six. 114 00:08:56,280 --> 00:08:59,340 It's a lower number than the default value. 115 00:08:59,520 --> 00:09:06,970 And I'm going after say Main engine you have to add this behavior to it as I'm going to say as the behavior 116 00:09:07,350 --> 00:09:09,050 this one tweet is about. 117 00:09:09,290 --> 00:09:10,280 I mean renamed. 118 00:09:10,290 --> 00:09:12,280 So that's my box behavior. 119 00:09:12,290 --> 00:09:14,650 So that's a better name for it. 120 00:09:14,700 --> 00:09:20,590 So my box behavior also has some optional values I'm going to write this to you remember this. 121 00:09:20,670 --> 00:09:21,510 Optional. 122 00:09:21,750 --> 00:09:24,150 And you can play along with them at some point. 123 00:09:24,300 --> 00:09:26,910 And now you still have two other things that you have to do. 124 00:09:26,910 --> 00:09:32,470 One of them is that we have to call this function at some point and the other one is we have française 125 00:09:32,530 --> 00:09:36,150 Surely this collision behavior to our engine. 126 00:09:36,210 --> 00:09:42,200 So I'm going to come in here and say once I made the collision Let's go ahead and say Main engine that 127 00:09:42,350 --> 00:09:49,200 adds that behavior and add that collision to it because it has to be there so it gets calculated within 128 00:09:49,200 --> 00:09:50,350 the main engine. 129 00:09:50,550 --> 00:09:52,760 And then of course let's call this function. 130 00:09:52,800 --> 00:09:58,320 Every time we had the touches and like I said touches on that is not the best place to put this. 131 00:09:58,320 --> 00:10:03,300 I'm just doing it because I want to simplify my life and I'm teaching this to you guys. 132 00:10:03,350 --> 00:10:05,320 So let's go ahead and run this up again. 133 00:10:05,430 --> 00:10:11,650 And if you run it now we should be able to see the box making a collision with the bat. 134 00:10:11,870 --> 00:10:14,030 And that's exactly what we wanted to see. 135 00:10:14,120 --> 00:10:20,780 Now the only thing that is left us thing is making sure that the box also collides with the boundaries 136 00:10:20,780 --> 00:10:21,680 of our view. 137 00:10:21,740 --> 00:10:23,930 And I'm going to do that in a different function. 138 00:10:23,930 --> 00:10:30,790 Some that come in here and say I haven't you function Let's call this one religion with view. 139 00:10:31,000 --> 00:10:38,270 And in my collision with the view I'm going to have to say religion not translate reference bounce into 140 00:10:38,270 --> 00:10:40,420 boundaries and that is true. 141 00:10:40,550 --> 00:10:43,160 And that's essentially all I have to doing here. 142 00:10:43,220 --> 00:10:47,970 So I'm going to say collision that view is also the function you want to call every time that touches 143 00:10:48,000 --> 00:10:52,680 and and there's still one more step you have to do with I want to show that to you. 144 00:10:52,700 --> 00:10:56,670 Once we've done the application successfully again in here. 145 00:10:56,780 --> 00:11:03,230 So what's the uploads up on the simulator if I touch it makes collisions with a variety of different 146 00:11:03,230 --> 00:11:03,950 objects. 147 00:11:04,220 --> 00:11:07,650 And now that I touch again doesn't anything doesn't really happen. 148 00:11:07,670 --> 00:11:09,900 So it doesn't jump back up there. 149 00:11:09,950 --> 00:11:15,970 So the fix that I'm going to come in here with touches on that say my box Dart Center. 150 00:11:16,040 --> 00:11:25,190 Make it a CG point of this place said something like 200 and a hundred even though it's not very good 151 00:11:25,250 --> 00:11:26,860 you know scientific way of doing it. 152 00:11:26,860 --> 00:11:32,210 You probably want to have exact numbers very you want to place it or you essentially want it to remember 153 00:11:32,210 --> 00:11:34,050 where the original place was. 154 00:11:34,130 --> 00:11:38,330 But I think for me just you know it's suffice to do it like that. 155 00:11:38,330 --> 00:11:40,770 So now it goes. 156 00:11:40,940 --> 00:11:43,730 Makes that up again. 157 00:11:43,760 --> 00:11:45,960 It does again and this time we didn't actually fall. 158 00:11:45,980 --> 00:11:46,940 So that's good. 159 00:11:46,940 --> 00:11:50,620 It shows me is working except that the numbers are probably very good. 160 00:11:50,620 --> 00:11:53,360 So maybe a hundred fifty would do better. 161 00:11:53,360 --> 00:12:00,110 Now the next thing that is the last thing in this project is understanding what happens every time that 162 00:12:00,110 --> 00:12:02,090 the collision is actually called. 163 00:12:02,240 --> 00:12:07,950 So to do that I'm going to come in here and say that I have the collision I'm going to have to say this 164 00:12:07,960 --> 00:12:16,220 sky collision not collision delegate's which is of the type you are in collision behavior delegate's 165 00:12:16,310 --> 00:12:24,920 becomes self and I have to add the UI collision behavior or delegate here you want collision behavior. 166 00:12:25,070 --> 00:12:28,420 Delegate's which is not that 167 00:12:31,290 --> 00:12:37,460 yes that's the one you like collision behavior or delegate becomes an other function that other protocol 168 00:12:37,520 --> 00:12:38,920 for your controller. 169 00:12:38,940 --> 00:12:44,780 And now I can have the function that has me even be qualified with something. 170 00:12:44,940 --> 00:12:46,980 And that's the only thing that we still have to do. 171 00:12:46,980 --> 00:12:53,100 We have to know every time that I've made some things so we can actually use that information to do 172 00:12:53,100 --> 00:12:53,790 something. 173 00:12:54,030 --> 00:12:58,710 And you know I'm with you as one of the collision behaviors and the one I want to use is the one that 174 00:12:58,740 --> 00:13:02,060 has an Eden and a boundary and it ends. 175 00:13:02,280 --> 00:13:06,430 So it's going to be this one is between two dynamic items. 176 00:13:06,480 --> 00:13:10,400 This one is a mother as a mother. 177 00:13:10,410 --> 00:13:14,790 This one to this one to a dynamic item on the boundary has ended. 178 00:13:14,790 --> 00:13:20,350 This is the one I want to use so I'm going to say this is my collision behavior. 179 00:13:20,520 --> 00:13:26,730 So let's get most of the data that is facing this from what it does is this it says every time a collision 180 00:13:26,730 --> 00:13:27,720 happens. 181 00:13:27,720 --> 00:13:35,090 If I'm using the delegation here which I am I will find out who has been colliding with what with what 182 00:13:35,100 --> 00:13:39,950 boundary essentially and that I am the object is the one I'm most interested in. 183 00:13:40,110 --> 00:13:40,900 So I'm going to sit. 184 00:13:40,930 --> 00:13:51,810 Let's I'm going to call it my label to become this item as you I that simply because I know that item 185 00:13:51,810 --> 00:13:56,660 that is in the collision is of the type you are labeling here. 186 00:13:56,800 --> 00:14:02,170 And what I want to do is I want to count up the number on it so we know how many collisions are happening. 187 00:14:02,190 --> 00:14:07,290 So to do that I'm going to actually have a count that I'm going to call these collision counter which 188 00:14:07,290 --> 00:14:14,930 is a default of zero on collision counter adds up every time we do find in your collision. 189 00:14:14,960 --> 00:14:19,560 So I'm going to say plus equal one and I'm about to say sure it's on my label. 190 00:14:19,590 --> 00:14:27,710 So my label that text becomes whatever value we find on collision counter. 191 00:14:27,900 --> 00:14:34,620 So with that we should be able to see a new collaging count happening every time a new collision is 192 00:14:34,620 --> 00:14:35,880 actually happening. 193 00:14:35,890 --> 00:14:38,210 Just go ahead and see if that works. 194 00:14:38,310 --> 00:14:39,210 I'm hoping with 195 00:14:44,980 --> 00:14:50,020 so now the numbers are counting go up and 16 was the final number. 196 00:14:50,020 --> 00:14:51,320 Let's try it again. 197 00:14:51,430 --> 00:14:53,420 And of course they have to reset it now. 198 00:14:53,500 --> 00:14:56,220 I can't count the number 31 minus 16. 199 00:14:56,380 --> 00:14:57,480 This time was less. 200 00:14:57,520 --> 00:15:01,700 So let's go ahead and reset the college and counting the touches begin. 201 00:15:01,720 --> 00:15:02,830 And that's right. 202 00:15:03,010 --> 00:15:03,790 Zero. 203 00:15:03,820 --> 00:15:09,910 And I also that I change the color of the labor depending on the value of the coalition Countach I'm 204 00:15:09,910 --> 00:15:17,650 going to say if college encounter modulus to zero and even the numbers do something tells you something 205 00:15:17,650 --> 00:15:29,890 else then say my dog died doing color becomes a white color color dots purple and otherwise it becomes 206 00:15:30,610 --> 00:15:34,380 a siren color for instance a more a different color. 207 00:15:34,660 --> 00:15:41,830 So now we should be able to see a collision counter counting up showing the number and the colors essentially 208 00:15:41,830 --> 00:15:45,000 switching between one to another depending on the number. 209 00:15:45,220 --> 00:15:51,350 So under all the numbers we see so on on the even numbers we see the purple color. 210 00:15:52,030 --> 00:15:53,200 Yes. 211 00:15:53,200 --> 00:15:54,550 So that's about that. 212 00:15:54,550 --> 00:15:56,110 This is you own. 213 00:15:56,170 --> 00:16:02,360 A lot of code and it's just the starting part of it is a lot of other things you can do with it. 214 00:16:02,620 --> 00:16:09,700 And I hope you can use it to do some other them games or basic animations in your applications. 215 00:16:09,760 --> 00:16:10,690 That's a lot of that. 216 00:16:10,690 --> 00:16:12,240 And let's move on to our next.