Apply_impulse() don't work

Godot Version 4.2

Question

My “roll”, and “get hit” are all been done with apply_impulse(), and lerp back to 0 in physic update loop, but I do not get knock back at all - Please help😢

By my design, the force should be burst in single frame, and the physical loop will slowly make it back to 0.

The funny thing is, if I disable the physical loop update for the part lerping velocity, both the knock back and roll works.

However, as soon as I added the lerp velocity back, the knock back imediately stop functioning. Even if the knock back actually commit way much force into the ridgedbody2d.

func hit(value:int,temp_direction:Vector2):
visual.play(‘Hit’)
apply_impulse(temp_direction*Vector2(1000,1000))

func roll():

apply_impulse(temp_direction*Vector2i(1000,1000))
await get_tree().create_timer(0.2).timeout

func _physics_process(delta):
SpeedReduceDefault(delta)

func SpeedReduceDefault(delta):
linear_velocity = lerp(linear_velocity, Vector2(0,0), 5* delta)

You should set up a damping force in the physics body.

Your lerp has 5*delta for its percentage. So if you run at 60fps then delta is .016. that ends up being 0.08 or 8% so it will take 12 frames to get to 0 linear_velocity. Or 0.192 seconds. Which is very short to imo.

But I suspect that there is already a linear damp setup that is added to this, slowing it down faster.

1 Like

Thank you to bring this idea, but the main question I am having with this example, is why that roll function with the same mechanism works(I was testing on a really big number on both them), but the hit back function just not working?

I suspect that is have something to do with Vector2 and Vectori2, though I have tried to keep all as Vectori2, didn’t work for me.

And this is a 2D scene script, and disabled gravity by set 2D gravity.y to 0.

Becuase my project is relevent simple, right now, I directly chenge the velocity to achieve what I want. However, the question remains.

It could be related to when you activate the forces that is not shown here.

I assume hit is within a collision callback. And roll is in an input event?

Roll is input event, and hit is a function that will call by another object with Area2D to detect. As the animation and other logic in the hit() function been triggered, only the physical knock back is not properly set.

As if I remove the reduce velocity function, both of them are working exactly what it suppose to, therefore I believe none other force are making adjustment to the velocity.

One thing I do figure out is at the function invoked on the hit, the next function will be directly jump to _physical_process, as I am questioning if it have anything to do with trigger single and the normal update process function.

Does the roll animation by chance adjust the node position? Or just the sprite.

This may not be necessarily true. On a default project the main loop triggers a process loop and physics loop. Which will run synchronously. (I can’t remember the order)

The physics engine can run multiple times in one frame called substeps. The input is checked many times, I think within each substep to make input as lag free as possible.(As well as before and after the physics loop) Collision signals may happen during the physics loop, but may be resolved after the physics loop. I’d have to look at the source again.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.