RigidBody accelerates more than the maximum allowed

Ive been a few hours researching since i pretty much suck at maths and after some work i got it to work my issue is that i can’t get to cap the maximum velocity correctly so it just ends up speeding more each time


using Godot;
using System;

public partial class Character : RigidBody3D
{
	
	public float acceleration = 5f;
	public float friction = 0.85f;

	public float maxVelocity = 40f;

	public StateMachine stateMachine = new StateMachine();
	public virtual void Initialize()
	{

		stateMachine.Initialize(this);

	}

	public void Move(Vector3 direction,double delta)
	{
		
		var wishDir = direction.Normalized() * maxVelocity;
		wishDir = wishDir.Clamp(-maxVelocity,maxVelocity);
		
		if (wishDir != Vector3.Zero)
		{
			
			var force = wishDir * acceleration;
			
			ApplyCentralForce(force);
		} else
		{
			var force = -LinearVelocity * friction;
			ApplyCentralForce(force);
		}




	}
	






}

You’re not capping the velocity anywhere in your code. You’re capping the force that you then apply to the body. These are different things.

1 Like

Allright could you explain further still because im blowing my mind trynna fix this out i tried a lot of things capping the force in many different ways wich it seems to obviusly not work and about the velocity i’ll try doing what you say

RigidBody3D has a linear_velocity property that you should cap after you apply the force.

Make sure to adhere to this note though:

If you need to directly affect the body, prefer _integrate_forces() as it allows you to directly access the physics state.

Ight i’ll give it a try

I clamped the velocity in the said function ans still nothing same issue idk if i did some calculation incorrectly

using Godot;
using System;

public partial class Character : RigidBody3D
{
	
	public float acceleration = 5f;
	public float friction = 0.85f;

	public float maxVelocity = 40f;

	public StateMachine stateMachine = new StateMachine();
	public virtual void Initialize()
	{

		stateMachine.Initialize(this);

	}

	public void Move(Vector3 direction,double delta)
	{
		
		var wishDir = direction.Normalized() * maxVelocity;
	
		
		if (wishDir != Vector3.Zero)
		{
			
			var force = wishDir * acceleration;
			
			
			GD.Print(force.Length());
			ApplyCentralForce(force);

		} else
		{
			var force = -LinearVelocity * friction;
			ApplyCentralForce(force);
		}




	}

    public override void _IntegrateForces(PhysicsDirectBodyState3D state)
    {
		state.LinearVelocity.Clamp(-maxVelocity,maxVelocity);
    }







}

Try Clampf instead of Clamp. And you need to assign the result back to the state.LinearVelocity.

Yeah i noticed still i used another approach and works my only issue is that maxvel number is very small but i don’t mind it using

using Godot;
using System;

public partial class Character : RigidBody3D
{
public float acceleration = 5f;

public float friction = 0.85f;



public float maxVelocity = 5f;



public StateMachine stateMachine = new StateMachine();

public virtual void Initialize()

{



    stateMachine.Initialize(this);



}



public void Move(Vector3 direction,double delta)

{

    

    var wishDir = direction.Normalized() \* maxVelocity;



    

    if (wishDir != Vector3.Zero)

    {

        Vector3 horizontalVel = new Vector3(LinearVelocity.X, 0, LinearVelocity.Z);

        Vector3 velocityChange = wishDir - horizontalVel;

        var force = velocityChange \* acceleration \* Mass;

        

        



        ApplyCentralForce(force);

            GD.Print(LinearVelocity.Length());

        

    } else

    {

        var force = -LinearVelocity \* friction \* Mass;

        ApplyCentralForce(force);

    }






}



public override void \_IntegrateForces(PhysicsDirectBodyState3D state)

{







}

}

I don’t understand how or why that works, but if you’re happy with it, then be my guest.

1 Like