2
$\begingroup$

Is there a way to use a functional If command in a DynamicModule? I have a code which runs a car driving up a street using Clock to control its position on the y-axis and the PlotRange. I have MousePosition controlling the position of the car on the x-axis? I have tried multiple ways to input an If command so that if the car runs into an obstacle, a CreateDialog box will pop up saying you have crashed. I cannot figure out how to make this If command cooperate with the dynamic functionality. Any help?

Clarification: Here is a small example of the code that has the heart of the problem. The "car" variable is undefined, but can be easily replaced with just a point, using the x and y variables as its coordinates.

finalS = {Slider[Dynamic[v], {10, 500, 10}], Dynamic[v]}; final = 
 DynamicModule[{}, {Graphics[{{White, Thick, 
      Line[{{200, -500}, {200, 175000}}]}, {Yellow, Thick, 
      Dashing[{.1, .05}], Line[{{500, -500}, {500, 175000}}]}, {White,
       Thick, Line[{{800, -500}, {800, 175000}}]}, 
     Inset[Graphics[{Orange, Disk[{0, 0}, {3, 4}]}], {x, y}]}, 
    Axes -> False, 
    PlotRange -> {{0, 1000}, {-250 + (z Dynamic[v]), 
       750 + (z Dynamic[v])}}, Background -> Black, ImageSize -> 825],
    z = Dynamic[Clock[300]];, x = Dynamic[MousePosition[][[1]]];, 
   y = Dynamic[z*v];}]; 
   finalS final
$\endgroup$
  • $\begingroup$ Yes, it is possible and I used in If inside this answer. To get more specific help from anybody you should add a minimal example to your question. $\endgroup$ – Karsten 7. May 14 '16 at 14:01
  • $\begingroup$ Say I wanted to use an if command so that would execute if the car drove off of the road line, how would that be possible? $\endgroup$ – Ishnor May 14 '16 at 14:17
  • $\begingroup$ Ishnor -- I reformatted your code so that it can be copied. I changed "car" to an orange ellipse. But I do not know what kind of answer you are looking for: what kind of collision with what obstacle? $\endgroup$ – bill s May 14 '16 at 16:05
1
$\begingroup$

This will open a dialog notebook created by CreateDialog as soon as you "drive" over the left sideline.

final = DynamicModule[{dialogQ = False}, 
     {Graphics[{{White, Thick, Line[{{200, -500}, {200, 175000}}]}, 
      {Yellow, Thick, Dashing[{.1, .05}], Line[{{500, -500}, {500, 175000}}]}, 
      {White, Thick, Line[{{800, -500}, {800, 175000}}]}, 
      Inset[Graphics[{Orange, Disk[{0, 0}, {3, 4}]}], {x, y}]}, 
     Axes -> False, PlotRange -> {{0, 1000}, {-250 + (z Dynamic[v]), 
        750 + (z Dynamic[v])}}, Background -> Black, 
     ImageSize -> 825], z = Dynamic[Clock[300]];, 
    x = Dynamic[MousePosition[][[1]]], y = Dynamic[z*v]; 
    DynamicWrapper[Dynamic[dialogQ, TrackedSymbols :> {dialogQ}], 
     If[Setting@x < 200 && ! dialogQ, dialogQ = True; 
      CreateDialog[{TextCell["You went " Style["OFF ROAD!", Red]], 
        DefaultButton[DialogReturn[dialogQ = False]]}, Modal -> True]], 
     TrackedSymbols :> {x}]}];

I just added the part containing the If statement at the end of your code leaving the rest unchanged.

$\endgroup$
  • $\begingroup$ awesome thank you so much. I should be able to do the rest of the If commands based off of this. Much appreciated $\endgroup$ – Ishnor May 14 '16 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.