Discuss Scratch

EExoduss
New Scratcher
5 posts

Sprite

Hi,
i'm learning scratch in a deep way and now, i am trying to learn how to move a sprite.

In particular, i found this code and it seems that a lot of people use it.

In particolare mi sono imbattuto in questo codice che, a quanto vedo, va per la maggiore:
when green flag clicked
forever
if <key [right arrow v] pressed?> then
change [x velocity v] by (1)
end
set [x velocity v] to ((x velocity) * (0.9))
change x by (x velocity)
end

My question is: is this a uniform accelerated motion?

My doubt exist because of this line of code:

change x by (x velocity)
end

We are changing x coordinate by adding x velocity. In other words, we are adding velocity to a distance (which should be a mistake because we can't sum a distance with a velocity)

Furthermore, why using this code, the variable “x velocity” cannot overcome the value of 4 (keeping pressed the right arrow)?

Sorry for the question but i'm actually a bit confused.

Last edited by EExoduss (May 3, 2022 21:35:39)

AnimatorsParadise
Scratcher
1000+ posts

Sprite

What the script does is increase the x velocity variable when you press the right arrow, which means the sprite will increase in speed and move when you press it. However, the set x velocity block gradually decreases the x velocity variable, making the sprite decrease in speed when you're not pressing the arrow key. The two changes to the x velocity variable balance out when you're pressing the right arrow.

Last edited by AnimatorsParadise (May 3, 2022 21:49:15)

MathPuppy314
Scratcher
500+ posts

Sprite

You are adding the velocity to the position. The velocity determines how fast the sprite is moving and you move the sprite by that speed.

This method of acceleration will apply the same “force” to an object. At a faster speed, friction is more present meaning the force needed to accelerate it will be greater. This is also why the velocity has a limit of 4. When the velocity reaches 4, you still will only be applying 1 unit of force, but the friction (velocity*0.9) will be applying 1 unit of force the other way, which balances it out.

EExoduss
New Scratcher
5 posts

Sprite

MathPuppy314 wrote:

You are adding the velocity to the position. The velocity determines how fast the sprite is moving and you move the sprite by that speed.

This method of acceleration will apply the same “force” to an object. At a faster speed, friction is more present meaning the force needed to accelerate it will be greater. This is also why the velocity has a limit of 4. When the velocity reaches 4, you still will only be applying 1 unit of force, but the friction (velocity*0.9) will be applying 1 unit of force the other way, which balances it out.


Ah, now i undestood why the value of velocity converges.

But the problem of adding the velocity to a position is not so clear.

The motion is uniform accelerated. This means that the position should change according to the following:

X = x0 + V0*t + 1/2 a*t^2

and considering that the block is “change x position by…” and V0=0, we can say that:

x = 1/2 a*t^2 where t should be the time of right arrow pressed

So instead of this:
change x by (x velocity)

which leads to this formula:

x = x0 + V (x= coordinate, v= velocity)

we should have:

change x by [1/2*(a)*(t)^2]

I actually dont know how to put time of button pressed.

Anyway the script works so i'm making some mistake in this reasoning.

Last edited by EExoduss (May 4, 2022 18:34:31)

MathPuppy314
Scratcher
500+ posts

Sprite

I'm not quite sure what you mean with all the variables, but I'll try to give an explanation anyway.

Velocity can be re-written as:

[velocity v] = ([change in position v] / [change in time v])::stack variables

We can rearrange this equation to isolate the change in position:

[change in position v] = ([velocity v] * [change in time v])::stack variables
or
change x by ((velocity) * ([change in time v]::variables))

Because this updates every frame (30 times per second), the change in time will be constant, and we don't need to include it in the equation. This means simply that we can write:

change x by (velocity)

The x position moves at the speed of the velocity variable.

The velocity is just “pushed” by 1 every frame if the key is pressed, meaning you can change the speed at which the sprite moves.

EExoduss
New Scratcher
5 posts

Sprite

That's the answer i was searching for.
Thank you really much

Powered by DjangoBB