Page 1 of 1

model's distance recording

Posted: 26 Jul 2021, 17:44
by lwang87
Hello,
My model is a car. I will control it to run through a maze (not always straight). I defined an initial variable — distance=0. And I want to know how to record the distance it move from the initial position to the end. Thank you for spending your time on answering my question.

Re: model's distance recording

Posted: 27 Jul 2021, 10:00
by fferri
You can accumulate traveled distance (distance d between current position and previous position) in a child script like so:

Code: Select all

function sysCall_init()
    self=sim.getObjectHandle('.')
    distance=0
    lastPosition=sim.getObjectPosition(self,-1)
end

function sysCall_sensing()
    local p=sim.getObjectPosition(self,-1)
    local d=0; for i=1,3 do d=d+math.pow(p[i]-lastPosition[i],2) end; d=math.sqrt(d)
    lastPosition=p
    distance=distance+d
end

Re: model's distance recording

Posted: 27 Jul 2021, 13:33
by lwang87
Thank you so much, fferi!