Character Controller: Why only use delta on gravity in PhysicsProcess?

Godot Version

4.2.1

Question

Hello, I hope that such a beginner’s question is in the right place.

I was looking at the “Squash the creeps” tutorial project from the Godot site and was wondering why the delta-time is not applied to the X and Y coordinates on the player controller.
The delta time is only taken into account when falling. I understand that the “_PhysicsProcess” runs constantly, but why is there an exception with gravity?

public override void _PhysicsProcess(double delta)
    {
        //...

        if (Input.IsActionPressed("move_right"))
        {
            direction.X += 1.0f;
        }
        if (Input.IsActionPressed("move_left"))
        
         //...

        _targetVelocity.X = direction.X * Speed;
        _targetVelocity.Z = direction.Z * Speed;

        if (!IsOnFloor())
        {
            _targetVelocity.Y -= FallAcceleration * (float)delta; // <-- HERE
        }

        // Moving the character
        Velocity = _targetVelocity;
        MoveAndSlide();
    }
}

I took the snippet from here: Moving the player with code — Godot Engine (stable) documentation in English

move_and_slide() uses delta “internally” so you don’t have to multiply the velocity with delta when you use move_and_slide(), you’ll get incorrect results if you do so.

Gravity is an exception because it’s an acceleration. The gravity value usually means how much you want falling objects to accelerate per second.
For example if your gravity is var gravity = 9.81, substracting it from velocity.y every frame would mean the acceleration is 9.81 (meters?) per frame. You have to multiply it with delta to make it 9.81 meters per second, as it supposed to be.

As you can see you’re setting _targetVelocity.Y with -=, so you’re not multiplying the vertical speed with delta either, you’re just using delta to calculate how much _targetVelocity should change this frame.

Hope it makes some sense, or someone who can explain it better comes along :smile:

3 Likes

Hey, I think I understand it now. It was actually a mistake on my part :slight_smile:

I have to think about it a bit more (somehow it’s hard for me to imagine) but if I understood you correctly, I would also have to calculate the delta to the X and Y coordinates if I were to implement a kind of thrust like in a spaceship or a car , correct?

In any case, thank you for the explanation :slight_smile:

1 Like

Yes, basically if you wanted to have an ‘acceleration’ property instead of a fix ‘speed’ property to determine the velocity, and you wanted the acceleration to be ‘per second’ instead of ‘per frame’, then you have to multiply it with delta.

1 Like

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