Hello,
before moving a dynamic model around, you should first dynamically reset all objects composing it: when you move a dynamic object instantly, the attached dynamic objects will have difficulties to follow (e.g. like in real life: you cannot instantaneously move an object, and if you do (or something close to instantaneous), it will break because of the infinite acceleration).
So you have two options:
- move the objects slowly, or by applying forces (e.g. simAddForce)
- remove the object from the dynamics engine, move it, then add it again. You would need the function simResetDynamicObject. If you have several objects built on a tree, you will have to call simResetDynamicObject for all the objects in the tree, for example to iterate over all objects in a model:
Code: Select all
local robotObjects=simGetObjectsInTree(robotBaseHandle,sim_handle_all,0)
for i=1,#robotObjects,1 do
simResetDynamicObject(robotObjects[i])
end
-- Now you can move the robot to a new position
simSetObjectPosition(robotBaseHandle,-1,newRobotPosition)
If you have to iterate over several objects to reset, make sure you are running in a non-threaded child script, or if you are running in a threaded child script, make sure you won't get interrupted for the full procedure of reset and reposition: use
simSetThreadAutomaticSwitch(false) before and
simSetThreadAutomaticSwitch(true) after, in that case.
Additionally, if you have to reset your robot many times, you will notice that small errors accumulate: indeed, dynamically enabled objects (e.g. frame --> joint --> wheel) will always have small positional and orientational errors (that's how most physics engine work). This means that the physics engine will not keep the wheel perfectly centered on the joint during dynamic simulation. If you reset your frame-joint-wheel, then that small error will be kept. In order to avoid that, you should additionally also memorize the relative positions/orientations of all your objects in the robot, and just after reset, apply them. You can use
simGetConfigurationTree for that:
Code: Select all
--init phase:
robotObjects=simGetObjectsInTree(robotBaseHandle,sim_handle_all,0)
robotInitialConfig=simGetConfigurationTree(robotBaseHandle)
-- When you want to reset your robot to a new position:
simSetThreadAutomaticSwitch(false) -- only if you run in a threaded script
for i=1,#robotObjects,1 do
simResetDynamicObject(robotObjects[i])
end
sim.setConfigurationTree(robotInitialConfig)
simSetObjectPosition(robotBaseHandle,-1,newRobotPosition)
simSetThreadAutomaticSwitch(true) -- only if you run in a threaded script
Cheers