• 2D
  • How do i set limits of linear velocity X and Y separately?

I want to set the limits of linear velocities of X and Y separately, heres what i have so far;

     		if linear_velocity.length() > max_speed:
     			linear_velocity = linear_velocity.normalized() * max_speed
     		else:
     			apply_central_impulse(Vector2(cos(angle), sin(angle)) * speed_acceleration)
     		gravity_scale = lerp(gravity_scale, 0, 0.1)
     	else:
     		gravity_scale = min(gravity_scale + gravity_acceleration, max_gravity)

this code sets both X and Y linear velocity limits to the max speed, being 300, but i want to set the limit of how fast X can go to 450, and how fast Y can go to 600 ONLY when it falls down, how can i achieve this? I thought of getting the linear velocities X speed by linear_velocity.x but that hasnt worked, and im unsure even if it did i could change the X limit to something else

Is that a RigidBody2D?

You could try limiting the x-component of the parameter of apply_central_impulse().

The documentation has this note:

Note: You should not change a RigidBody2D's position or linear_velocity every frame or even very often. If you need to directly affect the body's state, use _integrate_forces, which allows you to directly access the physics state.

The length function returns the magnitude of the entire vector, so it will have the X and Y taken into account. If you want to check just the length of a single axis, you probably want to do something like this:

if abs(linear_velocity.x) > max_horizontal_speed:
	linear_velocity.x = sign(linear_velocity.x) * max_horizontal_speed

Then you can limit each axis of velocity independently.

That said though, as @DaveTheCoder mentioned, you don’t really want to set the linear velocity manually too often unless it’s in _integrate_forces, as otherwise you may be fighting the physics engine. You may want to move the logic for limiting the velocity to _integrate_forces if you can for more predictable results.

6 days later

@TwistedTwigleg said: The length function returns the magnitude of the entire vector, so it will have the X and Y taken into account. If you want to check just the length of a single axis, you probably want to do something like this:

if abs(linear_velocity.x) > max_horizontal_speed:
	linear_velocity.x = sign(linear_velocity.x) * max_horizontal_speed

Then you can limit each axis of velocity independently.

That said though, as @DaveTheCoder mentioned, you don’t really want to set the linear velocity manually too often unless it’s in _integrate_forces, as otherwise you may be fighting the physics engine. You may want to move the logic for limiting the velocity to _integrate_forces if you can for more predictable results.

After playing around with this, i have very certainly got it to work! ill try to add in the integrate_forces style movement, but as of now add_central_impulse works fine so ill stay with it, for now, if i have any issues however ill refer to what you said, thanks bunches!