Godot Version
4.2.2 Linux via flatpak
Question
I’m trying to make the simplest thing possible, a texture affected by gravity.
public partial class Player : RigidBody2D
{
static float gravityMagnitude = (float)ProjectSettings.GetSetting("physics/2d/default_gravity");
static Vector2 gravityDirection = (Vector2)ProjectSettings.GetSetting("physics/2d/default_gravity_vector");
static Vector2 gravity = gravityMagnitude * gravityDirection;
Vector2 vel = Vector2.Zero;
Sprite2D sprite2D = new();
public override void _Ready()
{
AddChild(sprite2D);
sprite2D.Texture = (Texture2D)GD.Load("res://icon.svg");
this.Position = new Vector2(500, 300);
}
public override void _PhysicsProcess(double delta)
{
vel += (float)delta * gravity;
Vector2 movement = (float)delta * vel;
GD.Print(delta, " ", Position, " ", movement);
MoveAndCollide(movement);
}
}
However, the Player is falling waaaaaay too fast. This is the log:
0.016666666666666666 (500, 300) (0, 4.5370375E-06)
0.016666666666666666 (500, 300.02722) (0, 9.074075E-06)
0.016666666666666666 (500, 300.08163) (0, 1.3611113E-05)
0.016666666666666666 (500, 300.16318) (0, 1.814815E-05)
0.016666666666666666 (500, 300.27182) (0, 2.2685188E-05)
0.016666666666666666 (500, 300.4075) (0, 2.7222224E-05)
0.016666666666666666 (500, 300.5702) (0, 3.175926E-05)
0.016666666666666666 (500, 300.75983) (0, 3.62963E-05)
As you can see, the movement vector is around e-6, but the position is incrementing on each step 100000 times more. What in the world am I doing wrong???
Disclaimer: I know I can simply use the game physics engine to do this, but it won’t be compatible with some future features I have in mind for my game, so I have to do the position calculations manually.