0 votes

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!

Godot version 3.4 stable
in Engine by (72 points)

1 Answer

0 votes

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):
    add_central_force(force)

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?

by (1,697 points)
edited by

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 addcentral_force only once, just to be sure.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.