## restart the simulation when collision happens

Typically: "How do I... ", "How can I... " questions
Zhuang
Posts: 28
Joined: 02 Aug 2017, 14:43

### restart the simulation when collision happens

Hello, what I want to do is when collision happens, restart the simulation, so I judge if there is collision I first stop the simulation and then start the simulation, the relative code is:

Code: Select all

res1, _ = simReadCollision(collisionHandle)
if (res1 == 1) then
res1 = 0
print("Collision!")
simStopSimulation()
res = simStartSimulation()
print(res)
end
But when I execute it, the robot just stops after seconds and doesn't start again, the Linux terminal shows:
Collision!
0
Collision!
0
Collision!
0
It means the if statement loops three times and the operation simStartSimulation could not be performed. I guess there should be time delay to finish the operation, so I change the code like this

Code: Select all

require "socket"
function sleep(sec)
socket.select(nil, nil, sec)
end
res1, _ = simReadCollision(collisionHandle)
if (res1 == 1) then
print("Collision!")
simStopSimulation()
sleep(0.5)
res = simStartSimulation()
print(res)
sleep(0.5)
end

But the result is the same! I am so confused, what happened ?

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

### Re: restart the simulation when collision happens

Hello,

you don't give any information about what type of script you are using. Is it a threaded or non-threaded child script? Or is it a customization script?
Child scripts do not run when simulation is stopped. So you can't start a simulation from a child script. You will have to use a customization script.

Make sure you familiarize yourself with the different script types before going ahead.

Cheers