0 votes

I'm getting the acceleration of my RigidBody2D this way (it work):

func _process(_delta):

func calc_accelerations():
    var v0 = linear_velocity
    var r0 = angular_velocity
    var dt = 0.1
    yield(get_tree().create_timer(dt), "timeout") #wait for dt seconds
    var v1 = linear_velocity
    var r1 = angular_velocity

    acc = (v1 - v0)/dt    #dv/dt
    racc = (r1 - r0)/dt   #dTheta/dt

But the yield() induce lots of errors when my RigidBody2D is deleted from the scene. I use queue_free() to delete it.

Is there a clean way of accessing the acceleration ?
I tried putting a Timer child but it didn't worked with small delay

in Engine by (91 points)

1 Answer

+2 votes
Best answer

Any reason why you don't simply use the delta-argument provided to _process?

var v0
var r0

func _process(delta):
    if v0 and r0:
        var acc  = (linear_velocity  - v0) / delta
        var racc = (angular_velocity - r0) / delta
    v0 = linear_velocity
    r0 = angular_velocity
by (10,584 points)
selected by

thanks, I didn't thought about using delta

I just had to put it in physicsprocess() and it worked perfectly

Can someone explain why (linearvelocity - v0) is not 0 ? It should be zero right because v0 is also linearvelocity of the same frame.

v0 is actually the linear_velocity of the previous frame in this context if I understood correctly

Yes, it should be the linear velocity of the previous frame as per the acceleration formula goes, but how do we assign v0 the linear velocity of the previous frame, I am scratching my head over this long time

Let's say linear_velocity increases by 1.0 every frame,

Frame |  v0  | linear_velocity | v0 = linear_velocity
0     |  0.0 | 0.0             | 0.0
1     |  0.0 | 1.0             | 1.0
2     |  1.0 | 2.0             | 2.0
3     |  2.0 | 3.0             | 3.0

So you calculate acceleration with v0 and linear_velocity then prepare v0 for the next frame

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 Frequently asked questions and 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 [email protected] with your username.