0 votes

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.

asked Sep 12 in Engine by Löwenhügel (29 points)

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.

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.

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.

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)

#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 fixedprocess or once).

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.

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:

http://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html?highlight=set_linear_velocity#class-rigidbody2d-set-linear-velocity

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.

1 Answer

0 votes

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!

answered Sep 16 by Löwenhügel (29 points)
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.