0 votes

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

func _process(_delta):
    calc_acceleration()

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

asked May 2 in Engine by leo-pnt (42 points)

1 Answer

+1 vote
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
answered May 2 by njamster (8,874 points)
selected May 2 by leo-pnt

thanks, I didn't thought about using delta

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

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.