Advertisement
matthewrmata

In-Plane Movement Overhead View

Jan 25th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 8.34 KB | None | 0 0
  1. \documentclass[12pt]{article}
  2.  
  3. \usepackage[paperwidth=215pt, paperheight=215pt,margin=12pt]{geometry}
  4. \usepackage{color,tikz,ifthen,animate,pgf}
  5.  
  6. \begin{document}
  7.  
  8. % This code will simulate a pitch based on the 9-parameter PITCHf/x model, including the pitch plane and projections for PITCHf/x movement without (red) and with (blue dashed) accounted for.
  9.  
  10. % Written by Matthew Mata, January 2017.
  11.  
  12. \thispagestyle{empty}
  13.  
  14. % Set the counter
  15. \newcounter{m}
  16. \setcounter{m}{0}
  17.  
  18. % Set the PITCHf/x parameters
  19. \pgfmathsetmacro{\xo}{1.958}
  20. \pgfmathsetmacro{\yo}{50.0}
  21. \pgfmathsetmacro{\zo}{6.041}
  22. \pgfmathsetmacro{\vxo}{-0.469}
  23. \pgfmathsetmacro{\vyo}{-115.739}
  24. \pgfmathsetmacro{\vzo}{-0.622}
  25. \pgfmathsetmacro{\ax}{-10.947}
  26. \pgfmathsetmacro{\ay}{23.455}
  27. \pgfmathsetmacro{\az}{-32.665}
  28.  
  29. % Set gravity
  30. \pgfmathsetmacro{\g}{-32.174}
  31.  
  32. % Set the binormal vector
  33. \pgfmathsetmacro{\Bx}{\vyo*\az - \vzo*\ay}
  34. \pgfmathsetmacro{\By}{\vzo*\ax - \vxo*\az}
  35. \pgfmathsetmacro{\Bz}{\vxo*\ay - \vyo*\ax}
  36. \pgfmathsetmacro{\Bnorm}{100*sqrt((0.01*\Bx)*(0.01*\Bx) + (0.01*\By)*(0.01*\By) + (0.01*\Bz)*(0.01*\Bz))}
  37. \pgfmathsetmacro{\Bx}{\Bx/\Bnorm}
  38. \pgfmathsetmacro{\By}{\By/\Bnorm}
  39. \pgfmathsetmacro{\Bz}{\Bz/\Bnorm}
  40.  
  41. % Find the fixed B-coordinate
  42. \pgfmathsetmacro{\Bfixed}{\Bx*\xo + \By*\yo + \Bz*\zo}
  43.  
  44. % Set the vector in the direction of home plate, u
  45. \pgfmathsetmacro{\Ux}{(\Bx/abs(\Bx))*(-\By)/sqrt(\Bx*\Bx + \By*\By)}
  46. \pgfmathsetmacro{\Uy}{abs(\Bx)/sqrt(\Bx*\Bx + \By*\By)}
  47. \pgfmathsetmacro{\Uz}{0}
  48.  
  49. % Set the vector in the direction of movement, w
  50. \pgfmathsetmacro{\signW}{(\Bx*\Uy-\By*\Ux)/abs(\Bx*\Uy-\By*\Ux)}
  51. \pgfmathsetmacro{\Wx}{\signW*(-\Bz*\Uy)}
  52. \pgfmathsetmacro{\Wy}{\signW*(\Bz*\Ux)}
  53. \pgfmathsetmacro{\Wz}{\signW*(\Bx*\Uy-\By*\Ux)}
  54. \pgfmathsetmacro{\Wnorm}{sqrt(\Wx*\Wx + \Wy*\Wy + \Wz*\Wz)}
  55. \pgfmathsetmacro{\Wx}{\Wx/\Wnorm}
  56. \pgfmathsetmacro{\Wy}{\Wy/\Wnorm}
  57. \pgfmathsetmacro{\Wz}{\Wz/\Wnorm}
  58.  
  59. % Find the position, velocity, and acceleration in uw-space
  60. \pgfmathsetmacro{\uo}{\Ux*\xo + \Uy*\yo + \Uz*\zo}
  61. \pgfmathsetmacro{\vuo}{\Ux*\vxo + \Uy*\vyo + \Uz*\vzo}
  62. \pgfmathsetmacro{\au}{\Ux*\ax + \Uy*\ay + \Uz*\az}
  63. \pgfmathsetmacro{\wo}{\Wx*\xo + \Wy*\yo + \Wz*\zo}
  64. \pgfmathsetmacro{\vwo}{\Wx*\vxo + \Wy*\vyo + \Wz*\vzo}
  65. \pgfmathsetmacro{\aw}{\Wx*\ax + \Wy*\ay + \Wz*\az}
  66. \pgfmathsetmacro{\gw}{\Wz*\g}
  67.  
  68. % Find the time to home plate from 50 feet
  69. \pgfmathsetmacro{\r}{\vyo/\ay}
  70. \pgfmathsetmacro{\s}{2*(\yo-(17/12))/\ay}
  71. \pgfmathsetmacro{\tplate}{-\r - sqrt(\r*\r - \s)}
  72.  
  73. % Find the time of release at 55 feet
  74. \pgfmathsetmacro{\u}{2*(\yo-55)/\ay}
  75. \pgfmathsetmacro{\trel}{-\r - sqrt(\r*\r - \u)}
  76.  
  77. % Find the flight time of the pitch
  78. \pgfmathsetmacro{\t}{\tplate - \trel}
  79.  
  80. % Set the end velocities
  81. \pgfmathsetmacro{\vxend}{\vxo + \tplate*\ax}
  82. \pgfmathsetmacro{\vyend}{\vyo + \tplate*\ay}
  83. \pgfmathsetmacro{\vzend}{\vzo + \tplate*\az}
  84.  
  85. % Set the number of frames
  86. \pgfmathtruncatemacro{\frames}{int(round(\t*60)+1)}
  87.  
  88. % Set the time increment
  89. \pgfmathsetmacro{\dt}{\t/(\frames-1)}
  90.  
  91. \begin{center}
  92.     \begin{animateinline}[poster=first,loop,controls]{60}
  93.         \whiledo{\them < \frames}{
  94.             \begin{tikzpicture}[scale=0.725]
  95.                 \clip(-4,-1) rectangle (4,7);
  96.                 % Plot the field
  97.                 \draw[green!75!black,fill=green!75!black] (-4,7) -- (4,7) -- (4,-1) -- (-4,-1) -- cycle;
  98.                 \draw[brown!75!black,fill=brown!75!black] (0,0) circle [radius=1.3];
  99.                 \draw[brown!75!black,fill=brown!75!black] (0,6.05) circle [radius=0.9];
  100.                 \draw[white!75!brown,fill=white!75!brown] (-0.1,6.05) -- (-0.1,6.1) -- (0.1,6.1) -- (0.1,6.05) -- cycle;
  101.                 \draw[thick, white!75!brown] (0.3708,0.3708) -- (6.364,6.364);
  102.                 \draw[thick, white!75!brown] (-0.3708,0.3708) -- (-6.364,6.364);
  103.                 \draw[white, thick, fill=white] (0,0) -- (0.0708,0.0708) -- (0.0708,0.1416) -- (-0.0708,0.1416) -- (-0.0708,0.0708) -- cycle;
  104.                 \draw[white!75!brown, thick] (0.1208,0.3708) -- (0.5208,0.3708) -- (0.5208,-0.2291) -- (0.1208,-0.2291) -- cycle;
  105.                 \draw[white!75!brown, thick] (-0.1208,0.3708) -- (-0.5208,0.3708) -- (-0.5208,-0.2291) -- (-0.1208,-0.2291) -- cycle;
  106.                 \draw[white!75!brown, thick] (0.179,-0.2291) -- (0.179,-0.8) -- (-0.179,-0.8) -- (-0.179,-0.2291);
  107.                 % Calculate the time increment
  108.                 \pgfmathsetmacro{\tinc}{\dt*\them + \trel};
  109.                 % Current flight time of the pitch
  110.                 \pgfmathsetmacro{\time}{\dt*\them};
  111.                 % Find the x and y positions of the pitch
  112.                 \pgfmathsetmacro{\xraw}{0.5*\ax*\tinc*\tinc + \vxo*\tinc + \xo};
  113.                 \pgfmathsetmacro{\yraw}{0.5*\ay*\tinc*\tinc + \vyo*\tinc + \yo};
  114.                 % Velocity of the pitch at the given time
  115.                 \pgfmathsetmacro{\vx}{\ax*\tinc + \vxo};
  116.                 \pgfmathsetmacro{\vy}{\ay*\tinc + \vyo};
  117.                 \pgfmathsetmacro{\vz}{\az*\tinc + \vzo};
  118.                 \pgfmathsetmacro{\vel}{sqrt((15*\vx/22)*(15*\vx/22) + (15*\vy/22)*(15*\vy/22) + (15*\vz/22)*(15*\vz/22))};
  119.                 % Time remaining
  120.                 \pgfmathsetmacro{\trem}{\tplate-\tinc};
  121.                 % Projection of the in-plane pitch excluding drag
  122.                 \pgfmathsetmacro{\uraw}{0.5*\au*\tinc*\tinc + \vuo*\tinc + \uo};
  123.                 \pgfmathsetmacro{\wraw}{0.5*\aw*\tinc*\tinc + \vwo*\tinc + \wo};
  124.                 \pgfmathsetmacro{\vu}{\au*\tinc + \vuo};
  125.                 \pgfmathsetmacro{\vw}{\aw*\tinc + \vwo};
  126.                 \pgfmathsetmacro{\uproj}{0.5*\au*(\tplate - \tinc)*(\tplate - \tinc) + \vu*(\tplate - \tinc) + \uraw};
  127.                 \pgfmathsetmacro{\wproj}{0.5*\gw*(\tplate - \tinc)*(\tplate - \tinc) + \vw*(\tplate - \tinc) + \wraw};
  128.                 \pgfmathsetmacro{\xplane}{\Ux*\uproj + \Wx*\wproj + \Bx*\Bfixed};
  129.                 % Calculate the drag
  130.                 \pgfmathsetmacro{\vxbar}{(\vx+\vxend)/2};
  131.                 \pgfmathsetmacro{\vybar}{(\vy+\vyend)/2};
  132.                 \pgfmathsetmacro{\vzbar}{(\vz+\vzend)/2};
  133.                 \pgfmathsetmacro{\vbar}{1.25*sqrt((\vxbar/1.25)*(\vxbar/1.25) + (\vybar/1.25)*(\vybar/1.25) + (\vzbar/1.25)*(\vzbar/1.25))};
  134.                 \pgfmathsetmacro{\vcoeff}{-abs(\ax*(\vxbar/\vbar) + \ay*(\vybar/\vbar) + (\az-\g)*(\vzbar/\vbar))};
  135.                 \pgfmathsetmacro{\axdrag}{\vcoeff*(\vxbar/\vbar)};
  136.                 \pgfmathsetmacro{\aydrag}{\vcoeff*(\vybar/\vbar)};
  137.                 \pgfmathsetmacro{\azdrag}{\g + \vcoeff*(\vzbar/\vbar)};
  138.                 % Contribution of drag in the w-direction
  139.                 \pgfmathsetmacro{\awdrag}{\Wx*\axdrag + \Wy*\aydrag + \Wz*\azdrag};
  140.                 % Projection of the in-plane pitch into the strike zone including drag
  141.                 \pgfmathsetmacro{\wprojdrag}{0.5*\awdrag*(\tplate - \tinc)*(\tplate - \tinc) + \vw*(\tplate - \tinc) + \wraw};
  142.                 \pgfmathsetmacro{\xplanedrag}{\Ux*\uproj + \Wx*\wprojdrag + \Bx*\Bfixed};
  143.                 % Plot a straight line from the ball to the projection without drag
  144.                 \draw[red] ({0.1*\xraw},{0.1*\yraw}) -- ({0.1*\xplane},{0.1*(17/12)});             
  145.                 % Plot a curve from the ball to the projection with drag
  146.                 \pgfmathsetmacro{\framesadj}{\frames-1}
  147.                 \ifthenelse{ \them < \framesadj }{
  148.                 \draw[blue, domain=0:\trem, smooth, dashed] plot ({0.1*\Ux*(0.5*\au*\x*\x + \vu*\x + \uraw) + 0.1*\Wx*(0.5*\awdrag*\x*\x + \vw*\x + \wraw) +  0.1*\Bx*\Bfixed},{0.1*(0.5*\ay*\x*\x + \vy*\x + \yraw)});
  149.                 }
  150.                 {
  151.                 }
  152.                 % Plot the projection without drag
  153.                 \draw[red, ball color=red, shading=ball] ({0.1*\xplane},{0.1*(17/12)}) circle [radius=0.0125];
  154.                 % Plot the projection with drag
  155.                 \draw[blue, ball color=blue, shading=ball] ({0.1*\xplanedrag},{0.1*(17/12)}) circle [radius=0.0125];
  156.                 % Plot the ball
  157.                 \draw[white, ball color=white, shading=ball] ({0.1*\xraw},{0.1*\yraw}) circle [radius=0.0125];
  158.                 % Display the metrics
  159.                 \node[above right] at (-4,6) {Dist.: };
  160.                 \node[above left] at (-1.25,6) {\pgfmathprintnumber[precision=1,fixed]{\yraw}};
  161.                 \node[above right] at (-1.5,6) {ft};
  162.                 \node[above right] at (0,6) {Vel.: };
  163.                 \node[above left] at (2.75,6) {\pgfmathprintnumber[precision=1,fixed]{\vel}};
  164.                 \node[above left] at (4,5.9) {mph};
  165.                 \node[above right] at (-4,-1) {Time: };
  166.                 \node[above left] at (-1,-1) {\pgfmathprintnumber[precision=2,fixed]{\time}};
  167.                 \node[above right] at (-1.25,-1) {sec};
  168.                 \foreach \n in {0,...,\frames}{
  169.                     \ifthenelse{\n < \them}{
  170.                         \pgfmathsetmacro{\tshadow}{\dt*\n + \trel};
  171.                         \pgfmathsetmacro{\xshadow}{0.5*\ax*\tshadow*\tshadow + \vxo*\tshadow + \xo};
  172.                         \pgfmathsetmacro{\yshadow}{0.5*\ay*\tshadow*\tshadow + \vyo*\tshadow + \yo};
  173.                         \draw[white, ball color=white, shading=ball, opacity=0.5] ({0.1*\xshadow},{0.1*\yshadow}) circle [radius=0.025];
  174.                     }
  175.                 }
  176.             \end{tikzpicture}
  177.             \stepcounter{m}
  178.             \ifthenelse{\them < \frames}{
  179.                 \newframe
  180.             }
  181.             {
  182.                 \end{animateinline} \relax
  183.             }
  184.         }
  185. \end{center}
  186.  
  187. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement