model's distance recording

Typically: "How do I... ", "How can I... " questions
Post Reply
lwang87
Posts: 41
Joined: 24 May 2021, 16:47

model's distance recording

Post 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.

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: model's distance recording

Post 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

lwang87
Posts: 41
Joined: 24 May 2021, 16:47

Re: model's distance recording

Post by lwang87 »

Thank you so much, fferi!

Post Reply