This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-06-12 07:18:30

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

What is velocity and why and how does it work?

Welcome to the ultimate velocity tutorial. This tutorial will tell you what velocity is, how it works (how to do it), and why it works.

1. What is velocity?
2. How do you do it?
3. Why does it work?
4. Good points made by other people


You can see an example of what this script does here.

Last edited by PullJosh (2012-06-13 16:29:18)


http://www.blocks.scratchr.org/API.php?action=text&string=I'm_on_vacation!&xpos=155&ypos=90&font_size=30&bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#2 2012-06-12 07:21:19

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: What is velocity and why and how does it work?

What is velocity?
Velocity is a variable that allows smoothness, most commonly in movement. In the movement form, it is used to cause friction. It slows down as it goes.
Home     Next ->

Last edited by PullJosh (2012-06-12 07:49:25)


http://www.blocks.scratchr.org/API.php?action=text&string=I'm_on_vacation!&xpos=155&ypos=90&font_size=30&bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#3 2012-06-12 07:22:44

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: What is velocity and why and how does it work?

How do you do it?
I'm not going to explain the script right now. Explaining goes in the next part. Here's the script:

when gf clicked
set [velocity v] to (0)
goto x:(0) y:(0)
forever
if <touching color [#000000]?> // replace this with the ground color.
change y by (2) // we could do velocity for this part too, but I'm not going to.
else
change y by (-2)
end
if <key [right arrow v] pressed?>
change [velocity v] by (0.2)
end
if <key [left arrow v] pressed?>
change [velocity v] by (-0.2)
end
if <not<(velocity) = [0]>>
if <(velcocity) > [0]>
change [velocity v] by (-0.1)
else
change [velocity v] by (0.1)
end
end
if <touching [edge v]?>
set [velocity v] to ((velocity) * (-1))
end
change x by (velocity)
Don't understand the orange blocks? Go here for a tutorial on them.

Home   <- Back | Next ->

Last edited by PullJosh (2012-06-13 16:11:38)


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#4 2012-06-12 07:25:03

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: What is velocity and why and how does it work?

Why does it work?
First of all, let's remember what velocity is: a number that changes to make things smoother. That means that this variable (named "velocity" in this script) changes, therefore changing the amount that the sprite moves. We start out with this:

when gf clicked
set [velocity v] to (0)
goto x:(0) y:(0)
This part of the script just resets the velocity. That way, if you were going left when you clicked the green flag, you wouldn't keep going left at the start.
The next part is gravity. You could, in fact, use velocity here, too. For this example, though, we will be keeping this part simple. After all, one velocity example should be enough.

After that is arrow key movement. This is where we really start working with velocitys. What this does, is changes the amount that the sprite changes.
The next part is a bit harder. What this does is make the friction. If the sprite is not moving, meaning the velocity is at zero, it does not need to slow down the amount it is moving. If it is, though, we get into the next part. This is what it will do if it is not still:
if <(velocity) > (0)>
change [velocity v] by (-0.1)
else
change [velocity v] by (0.1)
end
If the velocity is grater than (bigger than) zero, it will go right. This is why: Remember what velocity is: an amount that the sprite moves. If you change the sprite's x position by 5, it will move five steps the the right. Try it out with a simple script, if you want. The same goes for left. If it is a negative number, it will go left. That means that we need to separate scripts. If it is going right we need to make it closer to going left, and vise-versa. That is what this part of the script does.

The next part is for bouncing off edges. You could change this script up to work will walls, but that would be harder. This script is just about velocity. First it checks to see if it is touching a wall. If it is then it does this:
set [velocity v] to ((velocity) * (-1))
Whenever you multiply a number by negative one it reverses. This is what I mean: 1 * -1 = -1, -3 * -1 = 3, 5 * -1 = -5, etc. This then changes if it is going left or right (see above paragraph).

Lastly, we have the part that actually moves the sprite. It is what gives all this velocity changing meaning. I hope you understand what I mean.

Home         <- Back | Next ->

Last edited by PullJosh (2012-06-12 17:24:50)


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

#5 2012-06-12 07:26:03

PullJosh
Scratcher
Registered: 2011-08-01
Posts: 500+

Re: What is velocity and why and how does it work?

These are some good points made by other people:
Nobody has had many comments on this topic. Be the first!

Home      <- Back

Last edited by PullJosh (2012-06-12 17:25:50)


http://www.blocks.scratchr.org/API.php?action=text&amp;string=I'm_on_vacation!&amp;xpos=155&amp;ypos=90&amp;font_size=30&amp;bgimage=http://imageshack.us/a/img339/7215/sspeechsigapiforwords.png

Offline

 

Board footer