manipulate velocities of rigidbodys

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

I want to change the velocity of a rigidbody object by a certain factor when a key is pressed. But using set_linear_velocity(get_linear_velocity()*factor) doesn’t work. Using that, the object won’t move at all, even when leaving out the factor. Every explanation and alternative would be appreciated.

It works fine for me. I imagine that is not the offending line of code.

You may want to check and see if the body is asleep.

avencherus | 2017-09-12 17:52

Everything else works fine as well. Setting speed with set_linear_velocity() just doesn’t work in that specific case for me.
No, it’s not asleep.

Löwenhügel | 2017-09-12 17:58

This will not change the velocity of the body if it is not already moving. A zero vector multiplied by a scalar is still a zero vector.

Smellyhobo101 | 2017-09-13 05:43

I’m well aware of this. But I don’t want to move something with this, just manipulate the velocity of the object if it’s already moving. (this comment is not meant to sound as aggressive as it might do)

Löwenhügel | 2017-09-13 11:55

#1 We’re talking about version 2.1.4 stable here, right?

You originally said “… Using that, the object won’t move at all …”
In the comment you say: “… just manipulate the velocity of the object if it’s already moving …”

#2 So the object is moving and is immediately stopping when you set the velocity?

I have no idea which other forces work on your rigidbody. Is there friction and/or gravity and/or damping?

You might want to apply an impulse to the rigidbody instead (regularly in _fixed_process or once).

wombatstampede | 2017-09-15 10:10

#1: Still on 2.1.3.
#2: I have a moving object, which works and moves fine as long as the given line is let out, but as soon as I put that discussed line in, it doesn’t any more.(Sorry for not clarifying this) The move-line and the velocity manipulating one are both in _process(delta)and are both called constantly (for testing purposes).
Moving works by apply_impulse, velocity manipulating by set_linear_velocity(get_linear_velocity()*factor). Gravity and damping are enabled.

Löwenhügel | 2017-09-15 15:59

If you are calling it in one of the processing loops (processing is more for less precise things, because the timestep delta can vary), then you are calling it too often.

See the note here in the documentation:

RigidBody2D — Godot Engine (stable) documentation in English

My assumption from your original description was that you were changing velocity once as part of a button press event.

A change to that should be fine, or you should consider implementing your own custom integration if you want to determine the velocity every physics step.

avencherus | 2017-09-15 17:38

:bust_in_silhouette: Reply From: Löwenhügel

Using _integrate_forces seems to work pretty well and solve the problem (although I don’t really understand why).
Thank you so much for all your help!