[Intro] Hello Fellow Scratchers! – Particle
simulations are crazy cool! – And they are like the icing on the cake for many projects,
elevating a game from good – to awesome! Over the course of the next two videos,
we are going to be creating some absolutely beautiful particle effects! From particle streams,
fizzing trails, to explosions with collisions & awesome sound effects - Wow, I can’t wait to get
started 😊 – The colours and sounds really make this so satisfying to watch! And it gets me so
excited as I can imagine this looking awesome in any Scratch game right? Just too cool. So guys,
you ready? Let’s get Scratching! [Setting The Scene]
We’ll begin by creating a shiny new Scratch project “Particles!”.
Our first steps is to create that initial particle stream… Firing our new particle clones
that follow a curve back down to the ground. We’ll set the scene. In the background sprite,
Filling the entire canvas black. Against this, our particles will
show up really bright and colourful! Then, back in Sprite1, Sorry Scratchy,
I’m replacing you with a small red circle. Zoom right in and draw it around the size
of one grid square, that’s 4x4 pixels. Then, make absolutely sure to drag our
circle to snap it the centre of the costume canvas, like so. Don’t miss that step otherwise
your particles won’t be positioned right! [Fire Shoot Launch Spawn Clone Particles]
And now it’s coding time – Let’s fire off some new particles shall we!?!
When flag clicked, we hide. Yep this original particle sprite is a clone factory – It will
create many visible particle clones, but doesn’t itself want to be visible.
Then we loop forever, firstly waiting for the mouse to be down,
and then… we want to create particles… But thinking ahead, if we want to create
more than one particle at a time, then we’ll need this next bit to
be in a custom block. So create a new block, naming it “create particles at”.
With an input “x”, and another “y”. And, of course, we tick the “run without screen refresh”.
That will do fine – Drop the block in after the mouse is down, here.
For this demo project, we’ll spawn the new particles at the mouse cursor, mouse x & y,
but you can enter any position you like into these inputs.
So next up, in our custom block, we “go to x
y” of the passed in x & y inputs. [Particle Momentum / Speed x & y]
What we need next is to define how fast our particle is moving.
Make two new variables, “speed x” for this sprite only, and “speed y”. They need to be
for this sprite only because each particle want’s to be able to move in a different
direction with different speeds., otherwise with for all sprites, all the particles would end up
moving the same direction in a kind of grand dance! Not really what we are after! Gosh no.
Ok, so just for now, let’s set the initial speed x to 6, and speed y to 12.
And finally, we get to create that clone of myself. The cloned particle sprite will take on
the same position, and speeds that we just set. Almost there, “When I start as a clone”.
First, we’ll show the sprite, remember it was originally hidden.
Then loop forever so we can animate this sucker! And I’m going to use a “go to
x y” – This might seem like an unusual choice, but it’s because I need to move
both the x & y positions at the same time. In which case, we need to reposition the sprite
at the original x position, & y position, plus our speed x & speed y adjustments.
Yay, since this loops forever, it should keep moving the particle each screen refresh, and will
produce the movement animation for the particle. Let’s smash the green flag and run the project!
Pew! Pew! Pew!!! Yeah! Now this is fun :D – My little red dots go flying across the screen, and
make some rather funky patterns in the process! You might have noticed that they appear to be
congregating at the top right of the stage however… And indeed very
soon… I’ve used up all 300 of my available sprite clones, and Scratch is not willing
to yield me any more! Shame on you Scratch! Anyhow, we can combat this by removing the ones
that should have gone off screen. IF, touching edge,
then delete this clone – easy as PI. And now, you can stay here firing off little
red particle friends for as long as you want, and Scratch willing, you’ll be hear until someone
calls you for Tea! – If you’re that lucky :D [Gravity]
But in the meantime, particles just don’t look good without… Gravity, to pull them down.
Make a new variable, named “GRAVITY” for ALL sprites since gravity is the same for everyone.
And just after the green flag is clicked, set gravity to negative 1 (-1).
And to have it take effect, drop in a change speed y block by GRAVITY,
just before we actually move the sprite. COME ON THEN! Green flag time – And woohoo yeah,
It’s like playing with a hosepipe in your garden! Really very entertaining,
and a little mesmerising to boot – I just love the naturally forming patterns… So cool.
[Firing a circle of Particles] But now I want to look at how we
can use these particles to simulate a sparkler trail. For this we want to fire out a batch of
particles in a circle, all at once. For this each particle will need a power, and a direction.
Find the “create particles” script, and drop a “repeat 10” loop around the set speed & create
clone blocks. This will create 10 clones, all at once because we checked the “run without screen
refresh” box for this custom block. Then create two new variables,
“power” for this sprite only, And “dir”, the direction, also for this sprite only.
Set these two variables before we begin looping around, dir to 0, and power to, 12.
Now to calculate the speed x & y for each particle! Set speed x to, multiply.
The power variable, by, and this is the fun bit. the “sin of”,
our “dir”, the direction variable. Easy. Do the same for speed y, only remembering
to switch the “sin” for a “cos” operator. Lastly, change “dir” by, 360 (a full circle),
divided by, the number of particles we are firing, that’s 10. A full circle in 10 equal steps.
Give that a test – Woo – It’s like having 10 hose pipes, all at once
now lol, wacky. Ooh look at that! Ha. Now it’s not perhaps the circle of
particles we were imagining – So, up the repeat to 30 particles… and
consequently, divide by 30 as well… And then: Oh man yeah, now we can see the circle! So cool.
But just watch out, once we try to create more than 300 clones, we do start to get weird things
happening to our beautiful circles! [Sparkler Trail]
So, I’ll drop the repeat back down to 10, and now – rather than creating perfect circles of
particles, let’s randomize it a bit, and make a sparkler trail instead.
For this we remove the “change dir” block entirely, and move the set initial set dir and
power ‘inside’ the repeat loop like so. Now each particle can have it’s own direction and power.
The direction will be a random number between -180.0 and 180.0.
The power is also a random number between 0.0, and 10.0.
(Including the .0 in a random number block allows it to generate fractional
numbers and not just whole numbers which will improve the look of our sparkles!).
Let’s see this in action! Here we go…
Oh wow… Look at this, How good is this!?! It is just like holding a sparkler – Again,
we can have a lot of fun playing with this! I can’t wait to play with the colours… But,
before we do – I want to talk about air resistance and terminal velocity.
[Air Resistance & Terminal Velocity] At present these sparks float through space,
and keep accelerating downwards until they are off screen – But in reality they should be slowed by
the air. That’s why fireworks explode out and then slow down as they begin to fall. Air resistance!
We can simulate this quite easily too. Make a new variable, named “RESIST”, for all sprites.
And set it, under GRAVITY, to 0.9. Great, so back to the start as clone script.
Where we change speed y, now we bring in a “set speed x” block.
Setting speed x to itself, “speed x”, multiplied by, the “RESIST” variable.
Being a number just less than 1, this will cause speed x to reduce in speed each time we set it.
We will do the same for “speed y”, Setting it to “speed y” multiplied by RESIST.
But look, then we also add GRAVITY to “speed y”, so why don’t we drop in an addition block into
our set speed y, and combine the multiply on the left, with the GRAVITY on the right. Then we can
remove the change speed y altogether. One less block will run that little but faster right!
And run the project. What we find is that the particles no longer
spread out anywhere near as widely, and they all quickly begin falling together at the same speed.
This is the effect of air resistance, and the particles have reached their terminal velocity
– The fastest they can fall before gravity and air resistance equal each other out. So interesting!
To see how this applies to fireworks, Let’s change RESIST to 0.8, and then…
in the create particles script, change the power to go up to 30.0.
Do you see how the sparks now shoot out and then slowly fall together? Just like fireworks!
Anyhow, this is a little over the top for our use, so put the power back to 10.0,
and then scroll right up, and set RESIST to a more sensible “0.97”.
Just remember you won’t see the resistance change until you click the green flag… Right! 😊
[Playing with Colours] So did I mention playing with colours
might be fun? How about we just cycle through different colours as we are creating the clones,
in the create particles script. Drop in a change color by, and this needs to be
real small… As little as 0.2 perhaps, don’t forget we are creating 100s of
these clones so this will change real fast. Give that a run – Woo-ho-hoo! How beautiful
is this? It’s a very magical effect and really brings this project to life 😊.
I wonder how this looks if we up the number of generated particles to 100…
Wow – Just tapping the mouse now generates these awesome firework like blasts of
colour – Now that is cool! [Trails with Pen]
One thing that is perhaps missing here are particle trails… You know,
how light leaves a motion trail behind it as it moves… these particles if paused are
perfect dots… What I would prefer is for them to look like this!
So how can we do that? The answer is with the Pen Extension.
I’ll just put my repeat back to 10 items, and then, click into the Extension addons,
enable the Pen extension. Ooh nice – New blocks to play with!
Find with me the when I start as clone script. So as a new particle is created, we can start
by setting the pen size, to 2 pixels. That makes sure our lines are a little more chunky.
Then, before we move this sprite, stuff in a pen down to begin drawing a line.
And then right after we’ve moved, we use a pen up to finish drawing again. This way
each particle will trace it’s last movement as a single short line on the stage canvas.
Well that sounds cool – But what does it look like?
Wow – Trails indeed! Only, these trails are all blue, and not matching our particles colours.
Easy fix, find the change color block before we clone these particles,
and pop in the equivalent “change pen color” block, changing it again, by 0.2.
Right, let’s give that a spin – Colour change confirmed! Nice. If you are wondering why the
trail colour is not the same as the sprite colour, it’s because the sprite colour effect
ranges from 0 to 200, but the pen colour effect ranges from 0 to 101… Go figure!
Anyhow, these trails are just too long, they persist forever! Let’s stop that.
We’ll make a new sprite, but before that, change the name of sprite1, to “Particles”
Now make that new sprite, naming it “Curtain”. We call it this because
it is used to cover over things. You’ll see. For starters, when Flag clicked. Loop forever, and
simply drop in a pen, erase all block. And run! So, the previous particle trail is erased,
and then the next lines are re-drawn each screen refresh.
Now listen – You can see here that the screen has to have been erased, before the particles draw
their lines. So how did I know that this erase would run first? If you wondered that, then well
done, that thought doesn’t occur to everyone. And the reason this erase runs first is because
the forever loop it is within is started BEFORE the forever loop in any of the particle clones.
Those forever loops don’t start running until after the clones are created, much later on
that this green flag script. And this gives the erase all block priority to run first.
Hey shall we get rid of those sprite costumes now we have the pen trails?
Click into the particles sprite, and under the when I start as clone, you’ll see the
show block. Now, we could just hide this sprite, but… we’re not going to do that,
because I presently want to do some collision detection. So instead, drop in a set ghost effect,
to 100 – Full transparent… but… still collidable. Useful.
There – Beautiful indeed 😊 [Long Fading Trails]
Ok, still talking about trails – You may be thinking it would be even nicer to
have longer trails than this? If you want to experiment with that then try this.
Back in the curtain sprite, create a fully black costume.
Then in place of the erase all, drop in a pen, stamp block. This will draw that fully black
rectangle right over our pen drawings are thus result in the erasing of the scene as before.
We just need to ensure we position the curtain sprite in the middle of the screen, at 0 by 0.
But, since the curtain is a sprite, it currently shows in front of all our beautiful pen, so first,
we need to hide the curtain sprite. This doesn’t stop it from stamping, so that’s perfect.
See, it now works just like the erase block did.
Except… that we now have the option to only partially stamp this curtain. Using
a set ghost effect block, and setting it to 95%, that’s almost entirely faded away to nothing.
The effect of this is that everything we paint on the canvas takes a really long time to be erased
– So cool, but this is way over the top for my purposes. I’m going to set this ghost effect 30%.
Interestingly, you might not think you can see any difference here from our original erase all
solution… But if I pause it – there can you see the effect? It’s subtle, but it does actually
make things feel even more authentic – Which is what we are after! Just play with this, and
the other values in this project and just have a whole lot of fun experimenting, that’s what I do!
[Outro] But I’m afraid that that is the end of
this episode, but not of this tutorial – Yes, part 2 will be coming very soon and we’ll be adding
collisions, particle bounce, and some fantastic sound effects to really round off the package!
😊 If you’ve enjoyed what you’ve seen so far, then please smash the like button under the video, and
don’t forget to subscribe to the channel, checking that bell icon, to get notified as soon as my
next video is released! If you manage to create something cool with this tutorial, then do post
it in the studio linked in the description under the video – I’d love to see what you’ve made!
So, thanks for watching, have a great week ahead, and Scratch on Guys!