We'll come back again in this lesson and we're going to talk about you I'd dynamics and dynamics is
one of the few different physics engines that the iOS SDK offers us and we use dynamics for usually
the lower end to kind of interactions.
If you want to do something with heavy physics engine you know interactions then you can rely on a sprite
kit or the scene kit.
But if you have to do some basic animations that are depending on the physics engine then you can use
the dynamics within your views.
So before you go to school and actually show you how we're going to program this let me show you the
project that I'm going to work on.
So we get a sense of what's happening.
So in this project every time I click this little box falls and as it falls it makes a lot of collisions
either with this barrier or with the view boundaries and it counts how many times it has collided with
different objects.
So once again it falls and it makes a lot of collisions and then it lands number three.
Again so on and so forth.
So that's essentially the goal of the project and the use of dynamics.
Now let's see how we're going to do that.
I'm going to go ahead into ex-cult and start a new application called it you might say nomics a story
about the things that I need or does I need a little label a new label.
I'm going to place it up there maybe a little bit larger such as two five six that's too large maybe
1 2 8 1 2 8.
And it is going to show me the number of the collisions which is by default 0.
Maybe if I go on color which is like purple and then I'm going to have another you I view.
So that's going to be you.
I view that I place it here.
That's like the barrier that my box essentially bumps against So I'm going to make that one.
Maybe dark gray color.
I know I'm going to have to add them into my programming interface so I'm going to come in here say.
So this one I'm going to call it my box which is a level of force.
This other one I'm going I call the barrier barrier.
Now we're almost done in here.
There's one more thing I have to do or this is something we probably haven't done before.
So let me show you how we do it.
And that is sitting an ID for an object and we need to see the ID because you want to be able to find
that object by its eye and draw a rectangle around it.
And that's something that we'll see real soon.
Don't worry about that part.
But setting an idea is here in the storyboards so I'm going to go up here in the class and I'm going
to say there is a label for this object and that is going to be called let's say that your oil or anything
that you want as long as it is a distinct unique value.
So unless it is to bury your ID for now.
So that's all I have to do in this story what we're calling site at in here.
Let's go ahead and add a few things that may form out all of this so it is cleaner.
The first thing I want to add is a white dynamic animator as well as a light gravity behavior so I'm
going to say that I'm going to call this one main engine that's like the engine of the entire physics
essentially that's going to be a dynamic animator and I knew that gravity is going to survive and I'm
going to call it dynamic gravity or draw or gravity.
Right.
The whole thing that's going to be a come at you like gravity behavior.
So this one essentially calculates everything in the scene.
This one adds a gravity to the objects that we wanted to have them to.
So let's go ahead to the next thing and I'm actually going to go ahead and add these things.
I should have done it in the view of the law but I'm going to do it in the touch.
And that simply because I wanted this to happen every time I touch somebody.
So I'm going to say let's do it in the touches and.
And what we have to do is we have to initialize this.
I'm going to say Main engine becomes you dynamic animator and then is going to say Give me a reference
if you say reference view itself that if you do it here within this city you essentially then the next
thing is I'm going to have to initialize my dynamic gravity so I'm going to sync my dynamic gravity
becomes you gravity behavior and then is going to say which items do you want to add this to.
I only have one item so I'm going to have to make an array of my only item which is my box.
So now it's been added there and then I'm going to have to add this behavior to my engine.
So you might have different kinds of gravities and you might have different kind of physical forces
and you have to choose which ones you want to add to your engine.
So I'm going to say Main engine dart at the behavior and this behavior which understands the gravity
for the box.
Now this is all live and it will work properly if we only wanted to add a gravity to this object.
Let's go ahead and try that out and see how this would fare.
Up until this point.
So now that the simulator is up if I touch it will see that the box essentially falls to the screen
and that's simply because the gravity keeps pulling it down later on I would add something that every
time that touches and we want to begin this dynamic animation we reset the location of the box right
up here.
But for now this was quite good.
It showed me that it's working but it doesn't cause any collisions.
And that's the most interesting part.
Let's go ahead and doubt.
I'm going to go back to school and say I have a variable called Collision and that collision is going
to be a collision behavior.
This is the behavior that calculates when objects bounce against each other or bump into each other.
And the first kind of collision I want to calculate is the collision against this barrier.
So I'm going to go ahead and say that have your function.
So.
Because the court is going to become a long coat so I'm going add them inside the new function say this
function is actually called Collision with objects and collision with objects does these things you
first find the boundary of the barrier view that we want to collide against.
So I'm going to say this guy and that guy becomes ads about the tweets of boundary and an.
And I'm going to say the boundary that I want to add the identifier for it is that's a name that we
gave for as very your ID.
That's the identifier I'm going to say get it as a..
Copying for and the pads that you're going to make is a path like this for a rectangle made of barrier
object dot frame.
So get the ID from that object get that identifier and produce a barrier based on that which is a busy
spot simply because objects that they want to collide with each other.
They can be calculated based on their look based on the fact that they are a white label.
They have to be calculated based on a busy path.
So I have that one something I did for getting your is we never initialize the collision.
So that's something I have to do it in here.
And for that I'm going to say let's have that collision to become a UI collision behavior of the items
and the item again is only this one guy.
So my box.
So that collision is essentially initialized for my box and then the collision itself is being calculated
based on this particular object.
For now while this is the bare minimum of what you could do to essentially calculate the collision.
The other things that are kind of optional and you could add for instance you could add a list to your
object.
So I'm going to say Let let's call it my behavior or my Buck's behavior or anything of the sort becomes
you UI dynamic Eidem behavior UI dynamic Eidem behavior and the item for it is going to be let's say
in this case my box.
So I'm going to say my box.
So these are the behaviors of specific items not the overall engine as we had it in years.
So the specific behavior they want to have for this object I'm going to say it has an plasticity plasticity
of let's say point six.
It's a lower number than the default value.
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
this one tweet is about.
I mean renamed.
So that's my box behavior.
So that's a better name for it.
So my box behavior also has some optional values I'm going to write this to you remember this.
Optional.
And you can play along with them at some point.
And now you still have two other things that you have to do.
One of them is that we have to call this function at some point and the other one is we have française
Surely this collision behavior to our engine.
So I'm going to come in here and say once I made the collision Let's go ahead and say Main engine that
adds that behavior and add that collision to it because it has to be there so it gets calculated within
the main engine.
And then of course let's call this function.
Every time we had the touches and like I said touches on that is not the best place to put this.
I'm just doing it because I want to simplify my life and I'm teaching this to you guys.
So let's go ahead and run this up again.
And if you run it now we should be able to see the box making a collision with the bat.
And that's exactly what we wanted to see.
Now the only thing that is left us thing is making sure that the box also collides with the boundaries
of our view.
And I'm going to do that in a different function.
Some that come in here and say I haven't you function Let's call this one religion with view.
And in my collision with the view I'm going to have to say religion not translate reference bounce into
boundaries and that is true.
And that's essentially all I have to doing here.
So I'm going to say collision that view is also the function you want to call every time that touches
and and there's still one more step you have to do with I want to show that to you.
Once we've done the application successfully again in here.
So what's the uploads up on the simulator if I touch it makes collisions with a variety of different
objects.
And now that I touch again doesn't anything doesn't really happen.
So it doesn't jump back up there.
So the fix that I'm going to come in here with touches on that say my box Dart Center.
Make it a CG point of this place said something like 200 and a hundred even though it's not very good
you know scientific way of doing it.
You probably want to have exact numbers very you want to place it or you essentially want it to remember
where the original place was.
But I think for me just you know it's suffice to do it like that.
So now it goes.
Makes that up again.
It does again and this time we didn't actually fall.
So that's good.
It shows me is working except that the numbers are probably very good.
So maybe a hundred fifty would do better.
Now the next thing that is the last thing in this project is understanding what happens every time that
the collision is actually called.
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
sky collision not collision delegate's which is of the type you are in collision behavior delegate's
becomes self and I have to add the UI collision behavior or delegate here you want collision behavior.
Delegate's which is not that
yes that's the one you like collision behavior or delegate becomes an other function that other protocol
for your controller.
And now I can have the function that has me even be qualified with something.
And that's the only thing that we still have to do.
We have to know every time that I've made some things so we can actually use that information to do
something.
And you know I'm with you as one of the collision behaviors and the one I want to use is the one that
has an Eden and a boundary and it ends.
So it's going to be this one is between two dynamic items.
This one is a mother as a mother.
This one to this one to a dynamic item on the boundary has ended.
This is the one I want to use so I'm going to say this is my collision behavior.
So let's get most of the data that is facing this from what it does is this it says every time a collision
happens.
If I'm using the delegation here which I am I will find out who has been colliding with what with what
boundary essentially and that I am the object is the one I'm most interested in.
So I'm going to sit.
Let's I'm going to call it my label to become this item as you I that simply because I know that item
that is in the collision is of the type you are labeling here.
And what I want to do is I want to count up the number on it so we know how many collisions are happening.
So to do that I'm going to actually have a count that I'm going to call these collision counter which
is a default of zero on collision counter adds up every time we do find in your collision.
So I'm going to say plus equal one and I'm about to say sure it's on my label.
So my label that text becomes whatever value we find on collision counter.
So with that we should be able to see a new collaging count happening every time a new collision is
actually happening.
Just go ahead and see if that works.
I'm hoping with
so now the numbers are counting go up and 16 was the final number.
Let's try it again.
And of course they have to reset it now.
I can't count the number 31 minus 16.
This time was less.
So let's go ahead and reset the college and counting the touches begin.
And that's right.
Zero.
And I also that I change the color of the labor depending on the value of the coalition Countach I'm
going to say if college encounter modulus to zero and even the numbers do something tells you something
else then say my dog died doing color becomes a white color color dots purple and otherwise it becomes
a siren color for instance a more a different color.
So now we should be able to see a collision counter counting up showing the number and the colors essentially
switching between one to another depending on the number.
So under all the numbers we see so on on the even numbers we see the purple color.
Yes.
So that's about that.
This is you own.
A lot of code and it's just the starting part of it is a lot of other things you can do with it.
And I hope you can use it to do some other them games or basic animations in your applications.
That's a lot of that.
And let's move on to our next.