# Manipulate too slow for rigid transformation of 3D object

I am trying to write a code that moves a cylinder inside another (a prismatic joint). Manipulate is very slow and I suspect that this is because the position of the movable part is being computed and rendered everytime. I know you can do this with Dynamic but I haven't really learn how to use this properly. Could someone give me some help? Here is the simple code I use:

cylJoints[R_, r_, {z1_, z2_}] :=
Module[{b, cyl, d},
b = BoundaryDiscretizeRegion[
RegionDifference[Cylinder[{{0, 0, z1}, {0, 0, z2}}, R],
Cylinder[{{0, 0, z1}, {0, 0, z2}}, r]],
BaseStyle -> {Opacity[0.3]}];
d = (z2 - z1)/3;
cyl = Cylinder[{{0, 0, z1 - d}, {0, 0, z2 + d}}, r];
Manipulate[
Show[b, Graphics3D@Translate[cyl, {0, 0, t*d}]], {t, -1, 1}]];

cylJoints[6, 3, {-5, 5}]


Dynamic[t]*d is a quick fix but there is another problem.

By using outer Module variables in Manipulate you may end up with broken Manipulate if you intend to save it as a cdf etc. Module variables definitions will not survive kernel reset. You can rewrite your example a little:

cylJoints[R_, r_, {z1_, z2_}] := Manipulate[
Module[{b, cyl, d},
b = BoundaryDiscretizeRegion[
RegionDifference[Cylinder[{{0, 0, z1}, {0, 0, z2}}, R],
Cylinder[{{0, 0, z1}, {0, 0, z2}}, r]],
BaseStyle -> {Opacity[0.3]}];

d = (z2 - z1)/3;

cyl = Cylinder[{{0, 0, z1 - d}, {0, 0, z2 + d}}, r];

Show[b, Graphics3D@Translate[cyl, {0, 0, Dynamic[t]*d}]]
]
, {t, -1, 1}];

cylJoints[6, 3, {-5, 5}]

• Thanks this works. Is there a way to disallow the render resize/repan when manipulating? It seems even with PlotRange values in Show I am unable to fix the camera. Commented Jul 12, 2019 at 19:41
• Wow.. I just noticed something. I don't know if it's a bug or if it's something because of BoundaryDiscretizeRegion. But if Exchange the two objects in Show (so if I write Show[Graphics3D...,b,...]), then I do not have this camera problem. Commented Jul 13, 2019 at 5:16