s
slhelpferer

# Why does velocity not become 0?

Somehow I cannot wrap my head around this 2d vector.

var normal_speed := 600.0

var max_speed := normal_speed

var direction := Vector2.ZERO

var drag_factor := 0.01

var desired_velocity := Vector2.ZERO

var steering_vector := Vector2.ZERO

var velocity := Vector2.ZERO

desired_velocity = max_speed * direction

steering_vector = desired_velocity - velocity

velocity += steering_vector * drag_factor

If I say  steering_vector * drag factor is the velocity. To change position I use velocity and the delta for the constant performance. If I use an input for example W to go up I start at Vector2.Zero and move direction.y - with a speed of  600.0 pixels per frame. Multiplying this by 0.1 every frame will reduce my steering vector (which seems to be directly affecting my velocity) from 600 - 60 = 540 then my new velocity is 540 (I should move slower), if I continue each frame will my velocity not just hit 0 and therefore I should not move?  I am surely missing something because this is not how it behaves but maybe someone can explain this to me so a simpleton like me can understand it (maths is not yet my strong-suit, got lots of catching up to do!)

• godoterfr replied
`var normal_speed := 600.0var max_speed := normal_speedvar direction := Vector2.ZEROvar drag_factor := 0.01var desired_velocity := Vector2.ZEROvar steering_vector := Vector2.ZEROvar velocity := Vector2.ZEROdesired_velocity = max_speed * directionsteering_vector = desired_velocity - velocityvelocity += steering_vector * drag_factor`

var drag_factor := 0.01

Your drag_factor is 1% not 10%.

if direction is W, Vector2.UP = Vector2(0.0, -1.0)
if your ship don't move in start, velocity = Vector(0.0, 0.0)

desired_velocity = 600.0 * (0.0, -1.0) = (0.0, -600.0)
steering_vector = (0.0, -600.0) - (0.0, 0.0) = (0.0, -600.0)
velocity += (0.0, 0.0) + (0.0, -600.0) * 0.01 = (0.0, -6.0)

`position += velocity * delta`

if position start (0.0, 0.0)

position += (0.0, 0.0) + (0.0, -6.0) * delta(very small number, imagine 0.016667) = (0.0, -0,100002) at frame 1

Now, imagine that you are at maximum speed towards UP and that you now press RIGHT.
And your position is (0.0, -1000.0)

desired_velocity = 600.0 * (1.0, 0.0) = (600.0, 0.0)
steering_vector = (600.0, 0.0) - (0.0, -600.0) = (600.0, 600.0)
velocity += (0.0, -600.0) + (600.0, 600.0) * 0.01 = (6, -594.0)
position += (0.0, -1000.0) + (6, -594.0) * 0.016667 = (0.100002, -1009.9) next frame

Now if you raise all the keys, since you have a drag_fator = 0.01, it will take several seconds, rather several tens of seconds, for the ship to come to rest.
And even if visually you see it as stationary, the velocity Vector2 can be a (0.0, 0.00001256) then (0.0, 0.00001249) etc.

2 loves
• Nathan Lovato replied

if I continue each frame will my velocity not just hit 0 and therefore I should not move?

Yes, after a while, the ship should stop moving with the code you provided. But as godoterfr pointed out, it will take several seconds for the ship to stop. Especially with a drag factor as low as 0.01, the change in velocity is very slow. So your ship is accelerating and slowing down, but the change takes a very long time and is hard to see.

Try increasing the value to 0.15, you'll see a big difference.

1 love
• s
slhelpferer replied

Thanks for the info, I don't think I fully understand but I believe I know why my initial thinking was wrong. I believed that the velocity value that we subtract from the desired_speed in the steering_vector variable would be additive so after some frames I thought I should not be moving even when I add input.

Visualizing the values as vectors (0,0)(0,0) is also great, thanks again for the answer.

• godoterfr replied

You can try putting print() in your code. Since it will be in physics_process, it will spam some values but you will see the change directly.

`print(velocity)`
1 love
• s
slhelpferer replied

Hi, I believe I understood it now...

I start off with a desired velocity but rather than moving instantly to the desired velocity I use velocity in the position = velocity * delta. That means my velocity at frame 1 is steering factor * drag factor which is essentially whatever the desired velocity is * drag factor. In my case (yes correct 1%, I forgot to change the code when pasting) 600 * 0.01 = 6. At frame1 I move at a speed of 6 rather than 600 and gradually add more to velocity with the same equation (less every frame due to the steering vector calculation) every frame until I hit desired speed at which desired speed - velocity == 0 meaning the steering factor * drag force == 0 which means I am not changing my velocity until I change my input again which adds a potential new vector point that I gradually move towards...

I hope I understood this now and if so I have no clue why it took me this long and thanks to the answers they helped.

• Nathan Lovato replied

You got it right! Good job.

1 love
• o
okpeery replied

Maybe because this is the 1st practice and we're only making small edits to tie the game all together, but why have normal_speed and max_speed since we assign max_speed to the value of normal_speed? Boost speed is different from max_speed too so it must be building blocks for later exercises? In this specific activity, could we have just used a general speed variable?

• Nathan Lovato replied

ookpeery in this lesson, we change the value of max_speed.

The variables normal_speed and boost_speed could be named respectively normal_max_speed and boost_max_speed for clarity: they're two values between which max_speed alternates in the code.

We use variables to represent them just to put a label on the respective maximum speed values: 600 and 1500 pixels per second, respectively.

As for just having one variable representing the maximum speed, I think this is what we have already, as the normal and boost speeds are basically constants. However, as we haven't introduced the const keyword yet, we use variables instead.