Questions about 'add_force'

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mudley
:warning: Old Version Published before Godot 3 was released.

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.

:bust_in_silhouette: Reply From: Freeman

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:

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.

Mudley | 2017-05-22 23:28

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.

Freeman | 2017-05-23 00:40

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

Mudley | 2017-05-23 02:05