Wrapping up Physics2DPlugin
In this chapter, we will jump right into the Physics2DPlugin that enables us to create 2d physics related animations. In this lecture, we wrap up our talk about the Physics2DPlugin as we create multiple animations.
Video Transcript
00:00I wanted to end off this chapter
00:02with a little homework assignment
00:04and as we were doing the last lesson,
00:06I deliberately built out our last few lessons.
00:10I built our div separately from our creation of the slide
00:14because I wanted to have access to it immediately.
00:17Now, the reason was is because I wanted to be able
00:19to add a lot of items at the same time
00:21without needing to give it an ID
00:23because I just have a reference to that specific div.
00:27So what I want to do next
00:28and that will be out to your homework
00:30is I want to basically take this logic
00:33and literally just pull it out
00:35and I'm just going to create here or even better,
00:38I'm going to leave it right here
00:39and I'm just going to create here another parameter
00:42and this parameter I will ask for the total items
00:47or item count, let's call it count
00:50and I'm going to set here just to make sure
00:52that count is equal to something
00:54that if count doesn't come back as a number
00:57or comes back as nothing
00:59or comes back as zero,
00:59then I'm going to make sure that we're going to send
01:01at least one into it.
01:03Now, what I want to do next is I'm going to make sure
01:07that I already set this variable div,
01:08I don't need to set it more than once
01:10and I'm going to go ahead and create our for var i
01:14equals zero, i is smaller than count
01:17and I'm going to grow our loop
01:21and all I really have to do here
01:22is just set that div value
01:24and push this a little bit inwards
01:27and close my loop.
01:28So no matter how many items I send in,
01:31we will be creating this animation over and over now.
01:34The only problem I have here
01:35is I want to create here some sort of
01:37probably a little bit of randomness
01:40inside of these items.
01:41So I'm going to go ahead here
01:42and I'm going to create a little variable
01:44and I'm going to call it anim time
01:48and I'm going to create here another variable
01:55what are you going to call it, anim time
01:57and we could have your one for velocity
02:00so I'm going to go here for velocity
02:04and let's do one also for the angle
02:07and I'm going to leave it,
02:08I'm just going to do all these items here
02:10but I'm not going to go beyond it
02:11and these are the four items
02:13that I want to create specific variations for them.
02:16So one is we have our div,
02:18let's create an animation time
02:19so for our animation time I'm going to go ahead
02:22and I'm just going to make sure
02:23that we have at least a half a second
02:26and I'm going to add to that math.random
02:29times two and a half seconds
02:31so really our capacity is between
02:33a half a second to three seconds
02:35we don't really know how long each animation will take
02:38and then we're just going to paste it right in there
02:40into that area where we put our seconds
02:43and I'm going to go ahead
02:44and do the exact same thing also for velocity.
02:46I'm going to decide the range of my velocity
02:49by defining by how much is our starting point
02:52so I'm going to set my starting point to be minus 200
02:55or let's see even minus 400 for that matter
02:58and I'm going to combine to it the math random
03:02but this time times 800
03:04making sure that my range is between minus 400 and 800
03:09and I could just make sure also
03:10that all this is an integer
03:11so I'm going to parse int all this value
03:14that we get back here and turn it into an integer.
03:18Last but not least or we could have used the math seal
03:21if we wanted to math floor
03:23so we're making sure that we have a value here
03:25that is an integer of sorts
03:28or a number that is not a fraction.
03:31Last but not least we also want to do this
03:33for our what was it for our angle
03:37but before that let me pass into our velocity the velocity
03:40and let's do the exact same thing for our angle
03:43where for our angle itself we want to decide
03:45what will be our angles
03:47what is the range of the angles
03:49that will be acceptable for us
03:50so I'm going to go ahead and say that for me
03:53the values would probably be something
03:54between minus 90 and plus 90
03:57so I'm going to go ahead there and set it to be a value
04:00that would be something in between minus 90 and 90
04:04and I'm going to send it in as of an integer
04:07even though I didn't have to
04:08I'm going to send it in as a full value
04:11and all that's left for me to do
04:12is set that angle value to be that angle
04:15and if I wanted to and if you wanted to
04:16you could do the same thing
04:18also for the gravity and the fraction
04:19but I'm not going to do that
04:20because I just want to showcase the application
04:23so if I just run my application one more time
04:25if I've done everything right
04:27then we should just see that one animation
04:29which is exactly what's happening
04:30but each time it's going to go
04:32in a completely different direction
04:34all that's left for me to do
04:35to really give life to this animation
04:37is go where we call the do physics plugin
04:40and send in how many items do I want to create
04:42and I want to create 50 items
04:45and if I go ahead and create 50 items
04:46we should see 50 animations happening
04:49at the same time
04:51the only problem is that part of our particles
04:54are not even seen because they're out of the screen
04:56and another thing is that our animation completes
04:58with all the items still on the stage
05:01so let's go back to the code
05:02and just tweak a little bit the variables
05:04to make them look better
05:06so I'm going to go back into the editor
05:08and the first thing that I want to do
05:09is I'm going to change the duration of time
05:12that the animation takes place
05:14because the more time it has
05:15the more chances it has to leave the screen
05:18so I'm going to make sure that our minimum is three seconds
05:20and I'm going to give it a larger span
05:22so I'll say between three to 11 seconds
05:26now in the section of velocity
05:28what I want to do is I want to make sure
05:30that my value is probably even smaller
05:33so I'm going to even go just in a positive velocity
05:37where it's going to go upwards in speed
05:39so I'm just going to go ahead here
05:40and just make a smaller range
05:42and just set that range to be
05:44a range of zero to 400
05:46so if I go ahead here and I just save this
05:49go back into my application and click on refresh
05:51you should already see a large difference
05:53and we can see that our animations
05:56are more or less like going in one direction
05:59now if I wanted to on the other hand
06:01if I would keep that velocity the same
06:04where I was controlling my velocity
06:07from minus 400 to 800
06:09time well adding 800 to it
06:11then I would have a velocity that is really slow
06:14to really fast
06:15but that's not necessarily what I would want to have
06:17because I wouldn't want to have an item
06:19that's going really really slow
06:21or really really fast
06:22because I wouldn't want something getting stuck
06:24on the stage itself as you could see
06:27as we're having in these samples
06:28where items are continuing and continuing on
06:31so let's keep that velocity at that times 400
06:36but as we do that we're going to have
06:38to change out to our angle
06:39instead of having an angle that starts
06:41at minus 90 degrees
06:43I'm going to go ahead and change the angle
06:45to start from a 200 degree
06:48and basically making sure my explosion
06:49is going upwards
06:51I'm going to say 200 plus up to 140
06:55so I'm getting like this range
06:56that's going upwards
06:58so if I save that click on refresh
07:01and go right back in there
07:02you'll see that now my explosion
07:03is going upwards and in that angle
07:06in that zone of between that angle 200
07:09to basically 340
07:13all right so let's continue a little bit further
07:15and what I want to do next is
07:16I want to basically set also different sizes
07:19to my actual element
07:21so I'm going to go ahead to my tween max
07:23and I'm going to set the items
07:26that is being the item itself
07:29I want to change its scale
07:31to give it a little bit more dynamic nature
07:33so I'm going to go ahead
07:34I'm going to call math random
07:37and I'm going to basically make sure
07:40that we have your little range
07:42between 0.3 and 0.8
07:44giving it a play in its size
07:47making sure that each time that it explodes
07:49it's in a different range
07:52last but not least I'm going to go ahead here
07:54and change the gravity
07:55and I want to make the gravity much more intense
07:57so I'm going to go ahead here
07:59instead of making a negative gravity
08:00I'm going to make a very heavy gravity
08:03and I am happy with the result
08:07but I might also want to add a small delay
08:10just to make sure that each time
08:11that application loads
08:13it's going to give us a little second
08:15to get ready before our animation begins
08:18so if I go ahead and I just click on refresh
08:20we're going to see that now
08:21our animation is happening
08:23it's a lot more interesting
08:26it feels a lot more 3D
08:28and it's creating a really really awesome effect
08:31and that's physics 2D plugin
