# Clamp Top Speed Giving A Jittery Look

4.2.1

### Question

I am attempting to make an RigidBody that speeds up to a point and is then clamped at that point, after reading I can see that clamped has been depreciated, but is now used with limit length, but when running this code:

``````func _physics_process(delta):
self.set_constant_force(Vector2(xSpeed, ySpeed))
self.linear_velocity = self.linear_velocity.limit_length(sqrt(
maxXSpeed * maxXSpeed + maxYSpeed * maxYSpeed
)) #clamp at the Vector2(maxXSpeed, maxYSpeed)
print(self.linear_velocity)
``````

The object become incredibly jittery with this, but does work, and I have no idea why it is not moving smoothly

I have tried changing the `set_constant_force` to `set_linear_velocity` inside a `_integrate_forces` function but that did not seem to work:

``````func _integrate_forces(delta):
self.set_linear_velocity(Vector2(xSpeed, ySpeed))
self.linear_velocity...
``````

How would I make this function so once it reaches the max speed it stays there, and had a smooth look.

Note: The function does not need to stay as a constant force, if the solution turns out to be using a different force.

may i see what you meant by :

also is this with the rigidbody having a gravity 1 or 0?

i tested with

GIF:

does your rigidbody come in contact with something? whatâ€™s the setup?

The rigidbody does not come into contact with anything, and yes it does have a gravity of 0, your setup is exactly what I have just without the staticbody.

My only thought is that setting a constant force and limiting the velocity are entirely different things within the physics engine. That force will be constantly accelerating your body while you limit the velocity directly. (I.e. you are working against yourself)

My suggestion would be to try a physics approach rather than touching the velocity directly. Or move away from the constant force function. Or set constant force to zero of max speed is reached.

Only apply a central force if below speed, and limit velocity when above speed. Avoiding the use of constant force.

I would suggest limiting the speed before applying forces. Instead of applying forces and then limiting velocity.

Use apply_cental_impulse, this modifies the velocity directly (the function internally multiplies the force with the the inverse mass.)

Modify damping while moving, to oppose directional force, limiting speed.

Use a PID feedback loop monitoring velocity and modulating acceleration force.

I bet @zdrmlpzdrmlp example is going to slow to notice the issue.