My RigidBody2D is not being affected by linear damping

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Paar

Hi! I’m new to physics based gameplay and am currently trying to figure out how to properly move RigidBody2D object. My code:

extends RigidBody2D

export var thrust_power := 100.0
export var movement_power := 100.0

var thrust_status := 0.0
var horizontal_direction := 0.0

func _process(delta: float) -> void:
	horizontal_direction = Input.get_action_strength("right") - Input.get_action_strength("left")

func _integrate_forces(state: Physics2DDirectBodyState) -> void:
	# Vertical force
	if Input.is_action_pressed("thrust"):
		add_central_force(thrust_power * Vector2.UP)
	# Horizontal force
	add_central_force(movement_power * horizontal_direction * Vector2.RIGHT)

At first, my object is properly being affected by gravity. When I hold “thrust” key and accumulate enough force, my object starts to move upward but does that infinitely and won’t be affected by gravity again even though I don’t hold the “thrust” key anymore (the code should be adding 0 force).
Currently my linear damping in the project settings is of default value 0.1 and is not overriden on the rigidbody itself.
It’s very confusing and I couldn’t find anything in the docs or anywhere else. Thanks!

:bust_in_silhouette: Reply From: DaddyMonster

Ok, everybody else ignored this one so I’ll have a crack at it. (you drew the short straw and got me, sorry!)

First obvious blunder: you’re calling the Input class in _process and not _physics_process - big red flag there. The former is just every frame, the latter is linked to the physics engine (with a set max framerate) which is obviously what you want given that you’re working with the engine.

Don’t call the physics engine twice in one frame. It’s like a new girlfriend in that regard, it doesn’t go down well to text twice before she’s repled. Like this:

extends RigidBody2D

var power = 1000.0
var force = Vector2.ZERO

func _physics_process(delta):
  force.x += Input.get_action_strength("right")\ 
            -Input.get_action_strength("left") * power * delta
  force.y = if Input.is_action_pressed("thrust"):
      force.y += power * delta

func _integrate_forces(state):

Obviously, I’ve tested nothing aside from this fine glass of red.

ps. you probably want an is_on_floor for jumping but I’ll leave that to you.
pps. don’t you want gravity?

Hey, thanks for your answer. I think it is perfectly save to get Input in the _process function as it is not related to physics. But I will try to call the add_central_force only once, just to be sure.

Paar | 2021-11-13 09:50