Group Abstract Group Abstract

Message Boards Message Boards

Problem with DynamicModule in a program with colliding blocks

I made interactive program with colliding blocks. The condition of collision is y<=x, where y & x are coordinates of two blocks along straight line of their motion. I could use the condition y<=x in Manipulate. But I couldn't use the condition y<=x in version with DynamicModule. I have read Help chapters about dynamics and know that dynamic variables are objects in front end. I tried different methods to convert they to numbers to use in operator If[y<=x,"collision","separate"] but in vain.

I give simplified part of the program relative to collision

  • Manipulate

    Manipulate[
     Module[{l0 = 500, h = 30, d = 30, x0 = 100, y0 = 400, v = 1, 
       u = -2},
      x = x0 + v*t; y = y0 + u*t;
      Column[{Graphics[{Rectangle[{0, -10}, {l0, 0}], Green,
          Rectangle[{x - d, -0}, {x, h}], Blue, 
          Rectangle[{y, 0}, {y + d, h}]}],
        {y < x, "  ", If[y <= x, "Collision", "Separated"]}}]],
     {t, 0, 200, 1, Appearance -> "Labeled"}
     ]
    
  • DynamicModule

    DynamicModule[{l0 = 500, h = 30, d = 30, x0 = 100, y0 = 400, v = 1, 
      u = -2},
     x = Dynamic[x0 + v*t]; y = Dynamic[y0 + u*t];
     Panel[Column[{Graphics[{Rectangle[{0, -10}, {l0, 0}], Green,
          Rectangle[{x - d, -0}, {x, h}], Blue, 
          Rectangle[{y, 0}, {y + d, h}]}],
        {Slider[Dynamic@t, {0, 200, 1}], Dynamic@t},
        {y <= x, "  ", Dynamic@If[y <= x, "Collision", "Separated"]}}]]
     ]
    

Best regards, Ivan Siahlo

POSTED BY: Ivan Siahlo
4 Replies

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.

POSTED BY: Michael Rogers

Thanks. This variant gives me the possibility better understand Dynamic[] command.

POSTED BY: Ivan Siahlo

Thanks. It works. I wasted much time but didn't find this straightforward solution.

POSTED BY: Ivan Siahlo

I myself find Dynamic mysterious. After playing around a bit I found that this variant works as intended:

Dynamic[If[y0 + u*t <= x0 + v*t, "Collision", "Separated"]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard