11

Consider the following MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{intersections,snakes}
\tikzstyle{water} = [decorate,decoration={zigzag,segment length=3mm,amplitude=.5mm}]
\definecolor{police}{RGB}{27,128,196}
\definecolor{sirene}{RGB}{240,79,35}
\definecolor{windows}{RGB}{189,230,250}
\definecolor{tire}{RGB}{121,131,140}
\definecolor{mark}{RGB}{246,235,21}
\definecolor{star}{RGB}{242,103,34}
\definecolor{street}{RGB}{46,46,46}
\definecolor{forest}{RGB}{34,139,34}
\definecolor{deep}{RGB}{31,61,12}
\definecolor{aqua}{RGB}{107,202,226}
\tikzset{
    pentagon/.style={
        append after command={
            \pgfextra{
                \begin{scope}[shift={(\tikzlastnode)},rotate=17.5,scale=.1]
                    \path[name path=linie1] (72:1) -- (3*72:1);
                    \path[name path=linie2] (0:1) -- (2*72:1);
                    \path[name intersections={of=linie1 and linie2, by=s}];
                    \path[name path=linie3] (72:1) -- (4*72:1);
                    \path[name intersections={of=linie3 and linie2, by=s1}];
                    \path[name path=linie4] (2*72:1) -- (4*72:1);
                    \path[name path=linie5] (72:1) -- (3*72:1);
                    \path[name intersections={of=linie4 and linie5, by=s2}];
                    \path[name path=linie6] (0:1) -- (3*72:1);
                    \path[name path=linie7] (2*72:1) -- (4*72:1);
                    \path[name intersections={of=linie6 and linie7, by=s3}];
                    \path[name path=linie7] (72:1) -- (4*72:1);
                    \path[name path=linie8] (3*72:1) -- (0:1);
                    \path[name intersections={of=linie7 and linie8, by=s4}];
                    \draw[fill=#1] (0:1) -- (s1) -- (72:1) -- (s) -- (2*72:1) -- (s2) -- (3*72:1) -- (s3) -- (4*72:1) -- (s4) -- cycle;
                \end{scope}
            }
        }
    },
    police car/.style={
        append after command={
            \pgfextra{
                \node[minimum width=1cm,minimum height=.5cm] (a) at (\tikzlastnode) {};
                \fill[windows] ([xshift=.2cm,yshift=.25cm]a.south west) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([xshift=-.1cm,yshift=.25cm]a.south east);
                \path[fill=police] (a.south west) -- ([yshift=.25cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east);
                \path[fill=white] ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                \draw (a.south west) -- ([yshift=.25cm]a.south west) --+ (.2,0) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([yshift=.25cm,xshift=.9cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east) -- cycle;
                \draw ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                \fill[black,radius=.1] ([xshift=.2cm]a.south west) circle;
                \fill[black,radius=.1] ([xshift=-.1cm]a.south east) circle;
                \fill[sirene] ([xshift=0cm]a.north) -- ([xshift=.15cm]a.north) arc(0:180:.075);
                \node[xshift=.05cm,yshift=-.13cm,pentagon=yellow] at (a.center) {};
            }
        }
    },
    racer/.style={
        append after command={
            \pgfextra{
                \begin{scope}[rotate=#1]
                        \node[rotate=#1,minimum width=1cm,minimum height=.5cm] (a) at (\tikzlastnode) {};
                        \fill[windows] ([xshift=.2cm,yshift=.25cm]a.south west) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([xshift=-.1cm,yshift=.25cm]a.south east);
                        \path[fill=black] (a.south west) -- ([yshift=.25cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east);
                        \path[fill=black] ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                        \draw (a.south west) -- ([yshift=.25cm]a.south west) --+ (.2,0) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([yshift=.25cm,xshift=.9cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east) -- cycle;
                        \draw ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                        \draw (a.south west) -- (a.south east);
                        \fill[tire,radius=.1] ([xshift=.2cm]a.south west) circle;
                        \fill[tire,radius=.1] ([xshift=-.1cm]a.south east) circle;
                \end{scope}
            }
        }
    }
}
\begin{document}
    \begin{tikzpicture}
        \draw[scale=1.1,xshift=-.5cm,yshift=.5cm,fill=street,yshift=-2cm,rounded corners] (0,0) rectangle (10,-8);
        \draw[xshift=.25cm,yshift=.25cm,fill=aqua,rounded corners,yshift=-3.5cm,xshift=1.5cm] (0,0) rectangle (2,-2);
        \draw[xshift=0cm,yshift=.25cm,fill=forest,rounded corners,yshift=-3.5cm,xshift=6.5cm] (0,0) rectangle (2,-2);
        \begin{scope}[yshift=-.5cm]
            \draw[fill=forest,rounded corners,yshift=-6.5cm,xshift=1.5cm] (0,0) rectangle (7,-2);
            \foreach \x in {1,2,...,5}
            {
                \fill[xshift=9.3cm,yshift=-7.7cm,deep,rotate=180] (1.3*\x,0) arc(180:360:.5) -- (1.3*\x+1,0) parabola[parabola height=1mm] (1.3*\x,0);
            }
        \end{scope}
        \draw[ultra thick,yellow!90!black,xshift=.55cm,yshift=-2.5cm,rounded corners,dashed,scale=.9] (0,0) rectangle (10,-8);
        \draw[ultra thick,yellow!90!black,yshift=-2cm,rounded corners,dashed] (5,-.5) --+ (0,-3.5) --+ (-4.5,-3.5) --+ (4.55,-3.5);

        \draw[xshift=.25cm,yshift=.25cm,yshift=-4cm,xshift=1.5cm,water,blue] (.3,0) -- (1.7,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4.3cm,xshift=1.5cm,water,blue] (.3,0) -- (1,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4.6cm,xshift=1.5cm,water,blue] (1,0) -- (1.7,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4cm,yshift=-.9cm,xshift=1.5cm,water,blue] (.3,0) -- (1.7,0);

        \node[police car] at (5,-2) {};
        \node[racer=90] at (0,-5) {};

        \draw[xshift=.7cm,ultra thick,blue,opacity=.7,->] (5,-2) -- (9,-2) arc(-270:-360:.5) --+ (0,-4);
        \draw[xshift=.7cm,ultra thick,red,opacity=.7,->] (-.5,-5.6) --+ (0,-.5) arc(180:270:.5) --+ (9.5,0);
    \end{tikzpicture}
\end{document}

Here is the output:

Screenshot

My question is: How can I achieve that the cars move along the arrows, so that the officer gets the racer?

  • 1
    I would put the cars in \saveboxes and then move them along the paths with decorations.markings.... – user121799 Aug 25 '18 at 2:34
  • 1
    You can use reflective paint for the arrows, and then mount steering light sensors on the cars. – hpekristiansen Aug 25 '18 at 2:42
  • @marmot, hpekristiansen: Could you post please a MWE (if it's no problem)? – current_user Aug 25 '18 at 2:51
  • 1
    why tie your hands drawing complex images in code that at the end generates a vector type image, you can import svg sprites in pdf format using the graphicx package, and take advantage of tikz's parametric abilities to change them and move them, as @marmot suggests, or using nodes in the path. – J Leon V. Aug 25 '18 at 3:04
  • 1
    I really like the indentation on your source code :) – cmhughes Aug 26 '18 at 8:48
12

Here is a first version. I was not aware that the rounded corners had such an impact on the tangent slope. My bad, fixed it

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{intersections,snakes,decorations.markings,calc,shapes.geometric}
\tikzset{water/.style={decorate,decoration={zigzag,segment
length=3mm,amplitude=.5mm}}}
\definecolor{police}{RGB}{27,128,196}
\definecolor{sirene}{RGB}{240,79,35}
\definecolor{windows}{RGB}{189,230,250}
\definecolor{tire}{RGB}{121,131,140}
\definecolor{mark}{RGB}{246,235,21}
\definecolor{star}{RGB}{242,103,34}
\definecolor{street}{RGB}{46,46,46}
\definecolor{forest}{RGB}{34,139,34}
\definecolor{deep}{RGB}{31,61,12}
\definecolor{aqua}{RGB}{107,202,226}
\tikzset{
    pentagon/.style={
    shape=star, star points=5, star point ratio=1.9, draw,minimum
width=3mm,fill=#1}}
\newsavebox\PoliceCar
\sbox\PoliceCar{\tikz{
                \node[minimum width=1cm,minimum height=.5cm] (a) at (0,0) {};
                \fill[windows] ([xshift=.2cm,yshift=.25cm]a.south west) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([xshift=-.1cm,yshift=.25cm]a.south east);
                \path[fill=police] (a.south west) -- ([yshift=.25cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east);
                \path[fill=white] ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                \draw (a.south west) -- ([yshift=.25cm]a.south west) --+ (.2,0) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([yshift=.25cm,xshift=.9cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east) -- cycle;
                \draw ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                \fill[black,radius=.1] ([xshift=.2cm]a.south west) circle;
                \fill[black,radius=.1] ([xshift=-.1cm]a.south east) circle;
                \fill[sirene] ([xshift=0cm]a.north) -- ([xshift=.15cm]a.north) arc(0:180:.075);
                \node[xshift=.05cm,yshift=-.13cm,pentagon=yellow,scale=0.3] at (a.center) {};
            }
        }
\newsavebox\Racer
\sbox\Racer{\tikz{\node[minimum width=1cm,minimum height=.5cm] (a) at
(0,0) {};
                        \fill[windows] ([xshift=.2cm,yshift=.25cm]a.south west) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([xshift=-.1cm,yshift=.25cm]a.south east);
                        \path[fill=black] (a.south west) -- ([yshift=.25cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east);
                        \path[fill=black] ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                        \draw (a.south west) -- ([yshift=.25cm]a.south west) --+ (.2,0) -- ([yshift=-.01cm,xshift=.4cm]a.north west) -- ([yshift=-.01cm,xshift=-.24cm]a.north east) -- ([yshift=.25cm,xshift=.9cm]a.south west) -- ([yshift=.25cm,xshift=.1cm]a.south east) -- ([xshift=.1cm]a.south east) -- cycle;
                        \draw ([xshift=.2cm]a.south west) -- ([xshift=.2cm,yshift=.25cm]a.south west) -- ([xshift=.9cm,yshift=.25cm]a.south west) -- ([xshift=.9cm]a.south west);
                        \draw (a.south west) -- (a.south east);
                        \fill[tire,radius=.1] ([xshift=.2cm]a.south west) circle;
                        \fill[tire,radius=.1] ([xshift=-.1cm]a.south east) circle;
            }
}
\begin{document}
\foreach \X in {0,0.03,...,0.9}
    {\begin{tikzpicture}
        \draw[scale=1.1,xshift=-.5cm,yshift=.5cm,fill=street,yshift=-2cm,rounded corners] (0,0) rectangle (10,-8);
        \draw[xshift=.25cm,yshift=.25cm,fill=aqua,rounded corners,yshift=-3.5cm,xshift=1.5cm] (0,0) rectangle (2,-2);
        \draw[xshift=0cm,yshift=.25cm,fill=forest,rounded corners,yshift=-3.5cm,xshift=6.5cm] (0,0) rectangle (2,-2);
        \begin{scope}[yshift=-.5cm]
            \draw[fill=forest,rounded corners,yshift=-6.5cm,xshift=1.5cm] (0,0) rectangle (7,-2);
            \foreach \x in {1,2,...,5}
            {
                \fill[xshift=9.3cm,yshift=-7.7cm,deep,rotate=180] (1.3*\x,0) arc(180:360:.5) -- (1.3*\x+1,0) parabola[parabola height=1mm] (1.3*\x,0);
            }
        \end{scope}
        \draw[ultra thick,yellow!90!black,xshift=.55cm,yshift=-2.5cm,rounded corners,dashed,scale=.9] (0,0) rectangle (10,-8);
        \draw[ultra thick,yellow!90!black,yshift=-2cm,rounded corners,dashed] (5,-.5) --+ (0,-3.5) --+ (-4.5,-3.5) --+ (4.55,-3.5);

        \draw[xshift=.25cm,yshift=.25cm,yshift=-4cm,xshift=1.5cm,water,blue] (.3,0) -- (1.7,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4.3cm,xshift=1.5cm,water,blue] (.3,0) -- (1,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4.6cm,xshift=1.5cm,water,blue] (1,0) -- (1.7,0);
        \draw[xshift=.25cm,yshift=.25cm,yshift=-4cm,yshift=-.9cm,xshift=1.5cm,water,blue] (.3,0) -- (1.7,0);
        \path[use as bounding box] (current bounding box.south west) rectangle
         (current bounding box.north east);
        \draw[xshift=.7cm,ultra thick,blue,opacity=.7,->,rounded corners=5mm,
        postaction={decorate,decoration={markings,mark=at position 1.1*\X
        with {\path (-10pt,0) coordinate(tmp1) (10pt,0) coordinate(tmp2);
        \pgftransformreset
        \draw let
        \p1=($(tmp2)-(tmp1)$),\n1={atan2(\y1,\x1)} in 
         (tmp1)   node[rotate=\n1] {\usebox\PoliceCar};}}}] 
         (5,-2) -|  (9,-6);
        \draw[xshift=.7cm,ultra thick,red,opacity=.7,->,rounded corners=5mm,
        postaction={decorate,decoration={markings,mark=at position \X
        with {\path (-1pt,0) coordinate(tmp1) (1pt,0) coordinate(tmp2);
        \pgftransformreset
        \path let \p1=($(tmp2)-(tmp1)$),\n1={atan2(\y1,\x1)} in 
         (tmp1) node[rotate=\n1] {\usebox\Racer};}}}] 
         (-.5,-5.2) |- (9,-6.1);
    \end{tikzpicture}}
\end{document}

enter image description here

  • Nice work but the police car turn is less successful than the other: the car makes a right angle that is not realistic! – AndréC Aug 25 '18 at 5:17
  • @AndréC I know. This is by far the most unrealistic thing in the whole animation. ;-) (More seriously, cars do not follow straight line - arc - straight line. This is because the curvature jumps along such paths. However, one moves the steering wheel in a continuous way, so in the real world the curvature does not jump. So even if I might move the car to the center of the decoration, it will not become "realistic" unless I change the path.) – user121799 Aug 25 '18 at 10:33
8

just for fun and to expose my point, I use the recycled code to draw the road from Drawing street curve, then doing the positioning node in a path including an image, in this case images transformed to svg edited in inkscape and exported to pdf format, you can access these files on my drive, available until the big G disappears, consists of 4 sprites 2 for each car, which I alternate in a certain frame.

RESULT: enter image description here

MWE: To run you need to download the pdf files Sprite-police-side.pdf,Sprite-police-front.pdf,Sprite-car-front.pdf,Sprite-car-side.pdf, or any tikz pdf output, inkscape pdf output,etc., to the forder where the main .tex file is saved.

% arara: pdflatex: {synctex: yes, action: nonstopmode}
% arara: animate: {density: 100, delay: 13, other: -background white -alpha remove}
% arara: showanimate
\documentclass[tikz,border=20pt]{standalone}
\usetikzlibrary{arrows,positioning,backgrounds,patterns}
\definecolor{grass}{HTML}{296402}
\definecolor{way}{HTML}{373737}
\tikzset{
    %STYLES
    runway node/.style={% Runway 
        rounded corners,
        way,
        line width=54pt,
        preaction={
            draw,
            way,
            line width=60pt,
        },
        preaction={
            draw,
            yellow,
            line width=55pt, % Yellow border lines.
        },
        postaction={
            draw,
            white,
            line width=19pt,
            dash pattern=on 35pt off 15pt, % Here control the road division lines
        },
        postaction={
            draw,
            way,
            line width=18pt,
        }
    }
}
\begin{document}
    \foreach \x in {1,...,30}{
    \pagecolor{grass}
    \begin{tikzpicture}
    %Runways
    \draw[runway node]
    (0,0) rectangle ++(12,-12)
    (0,-6) -- ++(12,0)
    (6,-6) -- ++(0,6);
    \draw[pattern=horizontal lines light blue,rounded corners=10pt] (1.5,-4.5) rectangle (4.5,-1.5) ;
    \draw[ultra thick,->,blue,rounded corners=1cm]
    (6,0)
            \ifnum\x<14
                -| ++(6,-6)node[pos=\x/30]{\includegraphics[scale=0.12]{Sprite-police-side}};
            \else
                -| ++(6,-6)node[pos=\x/30]{\includegraphics[scale=0.15]{Sprite-police-front}};
            \fi
    \draw[ultra thick,->,red,rounded corners=1cm]
    (0,-3)
            \ifnum\x<16
                |- ++(12,-3)node[pos=\x/30]{\includegraphics[scale=0.15]{Sprite-car-front}};
            \else
                |- ++(12,-3)node[pos=\x/30]{\includegraphics[scale=0.12]{Sprite-car-side}};
            \fi

    \end{tikzpicture}
    }
\end{document}

PSD: animation done with imagemagick converter.

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.