Dummy's problems of following an open path

Typically: "How do I... ", "How can I... " questions
Post Reply
anteto
Posts: 10
Joined: 24 May 2021, 08:00

Dummy's problems of following an open path

Post by anteto »

HELLO!

I created an open path and added a dummy to make the dummy move along the path. I found that the dummy does not stop after moving from the start of the path to the end of the path. Instead, it returns to the starting point and repeats the previous movement. May I ask How can we make the dummy move along the open path and stop at the end? Here is the code for dummy to follow the path, the code comes from official documents.

Code: Select all

function sysCall_init()
    objectToFollowPath=sim.getObjectHandle(sim.handle_self)
    path=sim.getObjectHandle('Path')
    pathData=sim.unpackDoubleTable(sim.readCustomDataBlock(path,'PATH'))
    local m=Matrix(#pathData//7,7,pathData)
    pathPositions=m:slice(1,1,m:rows(),3):data()
    pathQuaternions=m:slice(1,4,m:rows(),7):data()
    pathLengths,totalLength=sim.getPathLengths(pathPositions,3)
    velocity=0.02 -- m/s
    posAlongPath=0
    previousSimulationTime=0
    corout=coroutine.create(coroutineMain)
end

function sysCall_actuation()
    if coroutine.status(corout)~='dead' then
        local ok,errorMsg=coroutine.resume(corout)
        if errorMsg then
            error(debug.traceback(corout,errorMsg),2)
        end
    end
end

function coroutineMain()
    sim.setThreadAutomaticSwitch(false)
    while true do
        local t=sim.getSimulationTime()
        posAlongPath=posAlongPath+velocity*(t-previousSimulationTime)
        posAlongPath=posAlongPath % totalLength
        local pos=sim.getPathInterpolatedConfig(pathPositions,pathLengths,posAlongPath)
        local quat=sim.getPathInterpolatedConfig(pathQuaternions,pathLengths,
                                                 posAlongPath,nil,{2,2,2,2})
        sim.setObjectPosition(objectToFollowPath,path,pos)
        sim.setObjectQuaternion(objectToFollowPath,path,quat)
        previousSimulationTime=t
        sim.switchThread()
    end
end
CHEERS!

coppelia
Site Admin
Posts: 10360
Joined: 14 Dec 2012, 00:25

Re: Dummy's problems of following an open path

Post by coppelia »

Hello,

the position of the dummy along the path is held in variable posAlongPath. That variable is however made cyclic via the modulo operator:

Code: Select all

posAlongPath=posAlongPath % totalLength
So remove above line, and it won't operate in a cyclic manner anymore.

Cheers

anteto
Posts: 10
Joined: 24 May 2021, 08:00

Re: Dummy's problems of following an open path

Post by anteto »

hello!

thanks!you guys are really helpful!

cheers!

Post Reply