0 votes

Trying to get used of how physics works in Godot, but there are a lot of things that I don't seem to get, specially about the 'add_force' function.

I am trying to use it to control the character(who is a RigidBody2D) as so:

if (Input.is_key_pressed(KEY_W)):
    add_force("vectors and stuff...")

And I have the following questions:

  • How to make the force applied fade away once the key is not pressed anymore?
  • How to cap how fast it can go?
  • How to apply the force in the direction that the body is rotated?

I know those are silly questions, but I came from an environment where force applying worked a little different.

in Engine by (60 points)

1 Answer

+1 vote

There are no silly questions when we learn something.
ad 1 You could apply a set_linear_damp to counter the force and eventually to stop the body from moving.

ad 2 For example with if statement. In pseudo code:

if the speed of the object is > 100, 
then set the speed of the object = 100

caps it at the speed value = 100

ad 3 You may use get_global_transform().get_rotation() to know the global rotation and then apply the force accordingly

It's always good to check the docs and see what's available there, for example for RigidBody2D:
http://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html

by (719 points)
edited by

About the ad 1, if I understood correctly how it works in Godot, friction is a value between 0 and 1, but it seems to do nothing even with maximum friction.

I checked just now. I think you should use set_linear_damp . The setting definitely slows down the object and make it stop..
Friction seams not to have any effect if object is not touching other objects I guess. Sorry for a misleading answer. I will edit that.

It makes it go slower, but still no signs of stopping.

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.