02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Discovering the Physics2DPlugin

In this chapter, we will jump right into the Physics2DPlugin that enables us to create 2d physics related animations. In this lecture, we will explore the basics of working with the plugin, as we explore the velocity and angle of animations.

Video Transcript

00:00Welcome, welcome to a new chapter and a new topic.
00:03Let's jump right in there.
00:05And really our goal is to create
00:07a really cool velocity animation
00:10or a physics 2D animation.
00:13So let's jump right into it.
00:15And I already pre-configured,
00:17if you go inside of your HTML,
00:19I already added the physics 2D plugin,
00:22which you could find inside of your JavaScript code
00:24if you're a club member.
00:27And just make sure that you load it up
00:28from your plugins folder
00:30and add it into the HTML.
00:31And we're starting off with the physics 2D plugin.
00:35So let's get at it.
00:36The physics 2D, and I'm gonna go ahead
00:38and just create quickly a little function
00:41to cut down the amount of, you know, disorder.
00:45So I'm just gonna call this, let's call this,
00:47let's call this physics 2D.
00:49I'm just gonna grab this name here.
00:54And we're gonna call this do physics 2D plugin.
00:57That will be the name of our function.
01:00I'm gonna go ahead to the bottom of my function list,
01:03create that function really quickly.
01:06And let me just break a few lines
01:09so it's in our stage, beautiful.
01:11So what I wanna do is I wanna add a circle onto the stage
01:16and then start animating it.
01:18So I'm gonna go ahead by creating a new div layer.
01:21And I'm just gonna go ahead and create that layer
01:24even before I add it into the stage
01:27by just sending into jQuery a new div.
01:29I'm gonna set its class to be circle.
01:33And I already pre-configured inside of my CSS
01:36that a circle will look like a circle.
01:38Oh, and I forgot to put here an equal.
01:41And all that's left for me to do
01:43is to tell this div to append itself
01:47to the slide item that we've been working in
01:50throughout the chapters.
01:52So I'm gonna go ahead here, and if I just save this
01:55and go back into the browser and click on refresh,
01:57we should see now that we have a circle sitting here
02:00waiting for us to do something with it.
02:04The next step is that, well, the next step that I wanna do
02:07is start creating animations for our circle.
02:09And to do that, I'm gonna approach my tween max
02:12and I'm gonna animate two animation, sending in our div.
02:17Next step is I'm gonna define how many seconds,
02:20which I'll decide that it's gonna be three seconds.
02:22And it's time for me to start animating.
02:25Now, the most simple way of creating the velocity
02:29is using the, well, a physics 2D animation
02:31is by basically going ahead and calling the physics 2D,
02:38creating that object inside of our properties,
02:41and then sending the different parameters
02:42that we're gonna go through each and every one of them,
02:45the important ones, to see how and what they do.
02:48So we have to have at least one parameter.
02:51And in this case, I'm gonna put inside
02:53velocity, which is a very important one,
02:55and I'm gonna define a velocity of 400.
02:59What does that even mean?
03:00What it means is that the direction
03:02that we're gonna be taking
03:03will take about 400 pixels a second.
03:06In other words, we're expecting to move 1200 pixels
03:10within those three seconds currently.
03:13So if I save everything and I go back into my application,
03:15we're gonna see that we're gonna have
03:16this animation going on, and it's gonna take
03:19three seconds to move 1200 pixels.
03:24Obviously, this animation isn't incredibly interesting,
03:28but notice how different it is
03:30than every other animation you do
03:31between Max until now is that we didn't define an endpoint.
03:35It's very open-ended.
03:37We're basically giving that engine
03:38to control those features
03:41by creating its own animation sequence,
03:44by just giving it rules to create
03:45some sort of a physicality, a fake physicality,
03:48but a very useful one.
03:50It's not a complete physics engine,
03:52but it's a mimic of physics,
03:54which could be very, very useful.
03:56But let's continue and make this animation
03:59a tad more interesting.
04:01And I'm gonna go ahead and add to it Oto an angle.
04:06And the angle itself will enable me Oto to control
04:09also the direction of the throw,
04:11not only from what direction it is,
04:13but also to what direction it's going towards.
04:16And I'm gonna go ahead here, save it,
04:17and click on refresh.
04:18And we can see that now that animation
04:20is gonna also have a direction where it's going towards.
04:23Now I wanna change this around
04:25just to show you that the controls that we create here
04:28have a big impact.
04:29So if I set the velocity to be, for example, 100,
04:33and I wouldn't change anything else,
04:35we'll see that the distance that we're gonna go
04:37is gonna be a distance of only 300 pixels in total
04:40because we have only three seconds.
04:43Beyond that, if I go ahead and I change the angle
04:45to be a negative angle,
04:48then I'm going to be going upwards.
04:50Now the angles could be anything between
04:52minus 360 to 360.
04:54So we could play whatever directions we want, really,
04:58but it's really just a circle.
04:59And question is, to what direction are we going?
05:02Where the default zero is gonna take us
05:04from left to right in a straight line,
05:06if we put an angle at 10%,
05:08then we're basically a degree of 10 degrees more accurately,
05:12then I'm pushing myself 10 degrees lower.
05:15Then other way around, if I'm setting it to minus,
05:17then I'm taking it 10 degrees upwards, all right?
05:21Now I wanna take a pause in this animation
05:24and we're gonna continue with the physics 2D
05:26and explore more of its features in the next lesson,
05:29but I want you to stop and play around
05:31with these features seeing what happens
05:33if you go to a negative value,
05:35which you guessed it,
05:36instead of going in from left to right,
05:38we're gonna go from right to left.
05:41Now in the following video, what we're gonna do,
05:44and notice also that we're going downwards
05:46instead of upwards because we really have a path.
05:48So when we changed our path to go upwards,
05:51really what we did is we created this path
05:53that is going in this direction.
05:55So if we're animating in the other direction,
05:56then we're going downwards instead of going upwards.
05:59If we wanted to go upwards in this situation
06:01where we're going backwards,
06:03then we would want to change the angle to be positive
06:07because what we really wanna happen
06:09is that our angle will go downwards if we went forward
06:11or upwards if we were going in the other direction.
06:15All right, so we'll see you in the next lesson.
06:17And in the next lesson,
06:18we're gonna continue and explore many of,
06:20well, we'll finish up exploring all the different features
06:23and capabilities of the physics 2D engine.
06:26So we'll see you in the next lesson.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!