Here's my version:
DynamicModule[{l0 = 500, h = 30, d = 30, x0 = 100, y0 = 400, v = 1,
u = -2},
(* INIT *)
t = 0;
x = x0 + v*t;
y = y0 + u*t;
(* DISPLAY *)
Panel[
Column[{
Graphics[Dynamic@{
Rectangle[{0, -10}, {l0, 0}],
Green, Rectangle[{x - d, -0}, {x, h}],
Blue, Rectangle[{y, 0}, {y + d, h}]}
],
Row@{
Slider[ (* update x, y whenever t changes *)
Dynamic[t, (t = #; x = x0 + v*t; y = y0 + u*t) &],
{0, 200, 1}], " ", Dynamic@t},
Dynamic@{y <= x, " ", If[y <= x, "Collision", "Separated"]}}]
]
]
Code like x = Dynamic[x0 + v*t]
is a bit odd to see. The "value" of x
is literally Dynamic[x0 + v*t]
. Whatever the numerical values of x0
, v
, and t
happen to be, x
is never equal to the numerical value of x0 + v*t
. It is always equal to the symbolic expression Dynamic[x0 + v*t]
. When a Dynamic[expr]
is displayed by the Front End, the Front End shows the current value of expr
. So when x
is displayed, you see the numerical value of x0 + v*t
. But x
does not equal that value; it never does, as I have said.
The variables x
,y
, t
were global, and I left them that way. If this was not intentional, then I suggest they be added to the list of variables for the DynamicModule[]
. Otherwise, they might create bugs in any open notebook.