0
$\begingroup$

I want to reproduce a simulation involving a movable cannon and a movable target of varying size. The cannon is fixed such that the vertical height is variable but the x and y position is fixed.

This is the code:

displacement[orign_List, velocity_, alpha_, gamma_, tEnd_] := 
With[{g = 9.8}, 
Select[If[tEnd <= 0, {}, 
Chop[Table[{origin[[1]] + velocity*t*Cos[alpha Pi/2], 
   origin[[2]] + velocity*t*Cos[gamma Pi], 
   origin[[3]] + velocity*t*Sin[alpha Pi/2] - 0.5 g*t^2, t}, {t, 
   0, tEnd, 0.25}]]], #[[3]] >= 0 &]]

velocity[velocity_, alpha_, gamma_, t_] := 
With[{g = 9.8}, 
Chop[If[t > 0, {velocity*Cos[alpha Pi/2], velocity*Cos[gamma Pi], 
 velocity*Sin[alpha Pi/2] - g t}, {0., 0., 0.}]]]

intersect[point_List, corner1_List, corner2_List] := 
point[[1]] >= corner1[[1]] && point[[1]] <= corner2[[1]] && 
point[[2]] >= corner1[[2]] && point[[2]] <= corner2[[2]] && 
point[[3]] >= corner1[[3]] && point[[3]] <= corner2[[3]]
intersection[points_List, corner1_List, corner2_List] := 
Or @@ (intersect[#, corner1, corner2] & /@ points)

With[{width = 200, height = 200, length = 200, limit = 10}, Manipulate[
DynamicModule[{b, Lx, Ly, Lz, cannonAlpha, cannonGamma, targetX, 
targetY, path, text, color, Vx, Vy, 
 Vz}, {cannonGamma, cannonAlpha} = cannonOrient;
{targetX, targetY} = targetPos;
b = cannonL Cos[(1 - cannonAlpha) Pi/2];
Lx = cannonL Cos[cannonAlpha Pi/2];
Ly = length/2 + cannonL Cos[cannonGamma Pi];
Lz = cannonL Sin[cannonAlpha Pi/2];
path = 
displacement[{Lx, Ly, cannonZb + Lz}, cannonVM, cannonAlpha, 
 cannonGamma, time];
tLast = If[Length[path] > 1, Last[path][[4]], 0];
{Vx, Vy, Vz} = velocity[cannonVM, cannonAlpha, cannonGamma, tLast];
color = 
If[intersection[
  path, {targetX, targetY, targetZ}, {targetX + targetL, 
   targetY + targetW, targetZ + targetH}], Red, Green];
Column[{
 Grid[{{"Vx", "Vy", "Vz"}, {Vx, Vy, Vz}}], 
 Graphics3D[{{Thickness[0.02], 
    Line[{{0, length/ 2, cannonZb}, {Lx, Ly, 
       Lz + cannonZb}}]}, {color, 
    Cuboid[{targetX, targetY, targetZ}, {targetX + targetL, 
      targetY + targetW, targetZ + targetH}]}, 
   Point[Most[#]] & /@ path}, 
  PlotRange -> {{0, width}, {0, length}, {0, height}}, 
  ImageSize -> 300]}]],
 {{cannonVM, 50}, 10, 
 100}, {{cannonOrient, {0.5, 0.5}}, {0, 0}, {1, 1}}, {cannonL, 20, 
 length/ 2}, {cannonZb, 0, 
 height/ 2}, {{targetPos, {100, 100}}, {5 limit, 0}, {width - limit,
 length - limit}}, {targetZ, 0, height - limit}, {targetL, limit, 
 length/2}, {targetW, limit, width/2}, {targetH, limit, 
 height/ 2}, {time, 0, 25}, {time, 0, 25, ControlType -> Trigger}, 
 SaveDefinitions -> True]]

But I don't understand why the path of the bullet isn't shown in the animation. Can anyone help me, please?

$\endgroup$
1
$\begingroup$

Typo in the definition of displacement "orign"->"origin"

$\endgroup$

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.