Turn dynamics of objects off and on

Typically: "How do I... ", "How can I... " questions
Post Reply
PvtSchneewitchen
Posts: 12
Joined: 12 Jul 2019, 09:37

Turn dynamics of objects off and on

Post by PvtSchneewitchen »

Hi Coppelia Community,

i have two questions concerning dynamics during simulation:

1) In my scene i have a bin where objects get thrown in. When the movement of the objects in the bin calms down to a certain level i disable the dynamics of all objects. To do that i itareate through all objects and make them static with "sim.setObjectInt32Parameter(handle, sim.shapeintparam_static, 1)". So far so good.

At a later stage of my simulation i want to turn the dynamics of these object on again. For that i set the static parameter back to default "sim.setObjectInt32Parameter(handle, sim.shapeintparam_static, 0)". If i do that all objects randomly fly through my bin. It seems like there would be an accumulation of forces during the time i turn the dynamics off and when i switch them back on, these forces move the objects very strong.

Is there a proper way to turn dynamics of objects off and on again without this problem? Do i have to reset any forces when i make an object static?


2) What is the exact difference between these two ways to disable dynamics:
- sim.setObjectInt32Parameter(handle, sim.shapeintparam_static, 1)"

- p=sim.boolOr32(getModelProperty(handle), sim.modelproperty_not_dynamic)
sim.setModelProperty(handle, p)



Greetings,
Patrick

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

Re: Turn dynamics of objects off and on

Post by coppelia »

Hello Patrick,

my guess is your objects are flying around is because you are enabling them dynamically in a random order. E.g. if you enable objectA that is below objectB, then objectA might be squeezed between two static objects and pop out of that pinch. Maybe try to enable object in the reverse order they were disabled?

About your second question: both have the sdame effect. But the second one should be used when using a model (a model can be a hierarchy of several dynamically enabled object: disabling the model dynamically, will automatically disable all composing objects as well).

Cheers

PvtSchneewitchen
Posts: 12
Joined: 12 Jul 2019, 09:37

Re: Turn dynamics of objects off and on

Post by PvtSchneewitchen »

Your answer sounds understandable. But it doesn't seem to solve the problem.
I tried to enable/disable the objects due to their height like this:

Code: Select all

function simUtils.enableDynamicsInBin(nBinHandle, bEnabled)
	local tWorkpieceHandles = {}
    local nStatic = 0
    if(bEnabled)then
        nStatic = 0
		print("enable")
    else
        nStatic = 1
		print("disable")
    end

	--get workpiece handles of bin
	tWorkpieceHandles =	simUtils.getWorkpiecesOfBin(nBinHandle)
	
	--sort handles due to their height from low to high
	tHeightPlusHandles = {}
	for i=1, #tWorkpieceHandles, 1 do
		table.insert(tHeightPlusHandles, {sim.getObjectPosition(tWorkpieceHandles[i], -1)[3], tWorkpieceHandles[i]})
	end
	function compare(a,b)
		return a[1] < b[1]
	end
	table.sort(tHeightPlusHandles, compare)

	
	if(bEnabled)then
		--enable child handle dynamics from highest to lowest
		for i=#tHeightPlusHandles, 1, -1 do
			local nResult = sim.setObjectInt32Parameter(tHeightPlusHandles[i][2], sim.shapeintparam_static, nStatic)
			sim.resetDynamicObject(tHeightPlusHandles[i][2])
		end	
	else
		--disable child handle dynamics from lowest to highest
		for i=1, #tHeightPlusHandles, 1 do
			local nResult = sim.setObjectInt32Parameter(tHeightPlusHandles[i][2], sim.shapeintparam_static, nStatic)
			sim.resetDynamicObject(tHeightPlusHandles[i][2])
		end	
	end
end
As i said, this didn't fix the problem. When the object dynamics get enabled, the objects fly around randomly.
Interesting: The more i disable/reenable the dynamics, the less is the object movement after the reenabling. Looks like this Phenomenon slowy calms down.

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

Re: Turn dynamics of objects off and on

Post by coppelia »

I unfortunately cannot reproduce what you are experiencing. Here a very simple script I used to test this:

Code: Select all

function sysCall_init()
    h=sim.getObjectAssociatedWithScript(sim.handle_self)
    sim.setObjectInt32Parameter(h,sim.shapeintparam_static,0)
    lastP=sim.getObjectPosition(h,-1)
    enabledState=0
end

function sysCall_sensing()
    if enabledState==0 then
        local p=sim.getObjectPosition(h,-1)
        local dx={p[1]-lastP[1],p[2]-lastP[2],p[3]-lastP[3]}
        lastP=p
        local dist=math.sqrt(dx[1]*dx[1]+dx[2]*dx[2]+dx[3]*dx[3])
        if dist<0.001 then
            sim.setObjectInt32Parameter(h,sim.shapeintparam_static,1)
            sim.resetDynamicObject(h)
            enabledState=1
        end
    end
    if enabledState==1 then
        if sim.getSimulationTime()>60 then
            sim.setObjectInt32Parameter(h,sim.shapeintparam_static,0)
            enabledState=2
        end
    end
end
Attach above script to a sphere for instance. And duplicate the sphere many times during simulation, with all spheres landing in an open box (where the box sides are made of individual cuboids). As soon as the movement of a sphere is small, it is disabled dynamically. But after 60 seconds of simulation time, all created spheres are dynamically enabled again. I can't see a jump. Spheres simply start moving again.

Let me know what you made differently.

Cheers

PvtSchneewitchen
Posts: 12
Joined: 12 Jul 2019, 09:37

Re: Turn dynamics of objects off and on

Post by PvtSchneewitchen »

Thanks for your effort.

There are two differences. Each of my objects is a compund convex shape wich makes the simulation way more costly but i think this isn't the problem.
What i also do is disabling the dynamics and after that i move the bin a little bit by simly changing it's position with sim.setObjectPosition. The first reenabling of the dynamics is after the bin movement. It seems that the movement of the bin has an effect on the disabled objects. It's like the forces of the bin movement are saved on the disabled objects and have their impact when i reenable the dynamics of my objects I did a little workaround to disable the bin movement and that seems to fix the problem.

Is there a way that i can disable the dynamics, move the bin and then kind of delete all "saved" forces?

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

Re: Turn dynamics of objects off and on

Post by coppelia »

The forces are not saved for an object that is (also only temporarily) removed from dynamics simulation.

When moving the bin, do you also move the objects in the bin? Before or right after disabling the objects in the bin, I would attach them to the bin (parent-child relationship), so that a bin-movement will also move the objects attached to it (or the objects in it in that case).
If that wasn't the reason for your problem, then maybe you could try to temporarily disable the bin for exactly one simulation frame, at the same time you would enable the objects in the bin again.

Cheers

PvtSchneewitchen
Posts: 12
Joined: 12 Jul 2019, 09:37

Re: Turn dynamics of objects off and on

Post by PvtSchneewitchen »

Hm, i think i always did what you mentioned.

My Bin is permanently not dynamic but respondable.
Each object is set as a child of the bin right after it is created.
I first disable all object dynamics and then move the bin. The objects are moving with the bin, as you suggested.
Then i reenable all object dynamics and that causes the objects to fly around.
If i deactivate the dynamics after the bin movement the objects don't fly around when reenabling the dynamics.

Here are two videos of the process:

Turning off dynamics before bin movement
https://youtu.be/FmhU3jpGBf4

Turning off dynamics after bin movement
https://youtu.be/S0zLvIl3gy8

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

Re: Turn dynamics of objects off and on

Post by coppelia »

Do you notice the same behaviour with all physics engines?
Also, could that behaviour also be linked to the fact that your shapes are convex shapes (or compounds of convex shapes)? What happens if instead of those shapes you use primitive spheres?

Cheers

PvtSchneewitchen
Posts: 12
Joined: 12 Jul 2019, 09:37

Re: Turn dynamics of objects off and on

Post by PvtSchneewitchen »

Ok i tested it with some primitive shapes where i didn't notice the effect. So it seems that this effect really can be traced to the fact that i use convex shapes for the Simulation. The whole Simulation seems to be smoother with primitive shapes.
Here's the video for anybodies information:
https://youtu.be/St8QASQwq2k

Before i start the Simulation i import a CAD-Modell and do a convex decomposion on it to compute the convex shape of the model. I use this convex shape for the simulation where i duplicate it several times and let it fall in the bin.
Are there any tipps how i can improve the physic simulation with convex shapes to minimize such effects like trembling or flying objects?

Post Reply