6

I made a simple TikZ picture like this:

enter image description here

and I would make an animation that rotates the whole image about its z_world axis.

I made this attempt:

\documentclass[10pt]{article}
\usepackage[italian]{babel}
\usepackage{geometry}
\geometry{a4paper}

\usepackage{media9}
\usepackage{animate}

\newcounter{angle}
\setcounter{angle}{0}

\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}

\begin{figure}[ht]
\centering

\begin{animateinline}[loop, poster = first, controls]{30}  
\whiledo{\theangle<359}{

\tdplotsetmaincoords{120}{\theangle}
\begin{tikzpicture}[scale=2,tdplot_main_coords]

\def\laserPointX{1.5}
\def\laserPointY{0}
\def\laserPointZ{2.5}
\def\moduloQuadro{\laserPointX*\laserPointX+\laserPointY*\laserPointY+\laserPointZ*\laserPointZ}
\def\moduloLaser{sqrt{(\moduloQuadro)}}

% laser plane
\filldraw[red!50, opacity = 0.2] (\laserPointX,\laserPointY,\laserPointZ) -- (0,-0.8,0) -- (0,0.8,0) -- cycle;
\shade [ball color=red] (\laserPointX,\laserPointY,\laserPointZ) circle [radius=0.05cm];

% axis laser
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (1.5*\laserPointZ/\moduloLaser,0,-1.5*\laserPointX/\moduloLaser) node[anchor=west]{$y_{laser}$};
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=east]{$x_{laser}$};
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (\laserPointX/\moduloLaser*1.5,\laserPointY/\moduloLaser*1.5,\laserPointZ/\moduloLaser*1.5) node[anchor=south]{$z_{laser}$};

% axis world
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (1.5,0,0) node[anchor=north west]{$y_{world}$};
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=north]{$x_{world}$};
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,0,1.5) node[anchor=south]{$z_{world}$};

\end{tikzpicture}

\stepcounter{angle}
\ifthenelse{\theangle<359}{
        \newframe
}{
        \end{animateinline}
}
}
\end{figure}
\end{document}

But I obtained an ugly effect (due to, I think, the TikZ picture that resizes itself every frame) composed by a rotation and a translation.

Does someone know how to achieve a correct rotation?

  • @AlexG Sorry, You're right. I edited the question adding the code. – dbovo89 Jun 21 '16 at 10:26
  • 1
    Your Code is incomplete. Its missing at least a document class and the usepackage for tikz-3dplot – MaPePeR Jun 21 '16 at 10:56
  • @MaPePeR Ok, I edited the code again. This time I added the complete code. – dbovo89 Jun 21 '16 at 11:26
8

All frames must share the same, manually inserted bounding box (in 2D screen coordinates) in order to override the automatic, TikZ-computed ones (which may differ from frame to frame).

For parameterized pictures within the animateinline environment, the \multiframe command is to be preferred over \whiledo:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\usepackage{animate}

\begin{document}

\begin{animateinline}[loop, poster = first, controls]{24}
  \multiframe{360}{iAngle=0+1}{
    \tdplotsetmaincoords{120}{\iAngle}
    \begin{tikzpicture}[tdplot_main_coords]

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \useasboundingbox[tdplot_screen_coords] (-3,-1.5) rectangle (3,3);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \def\laserPointX{1.5}
    \def\laserPointY{0}
    \def\laserPointZ{2.5}
    \def\moduloQuadro{\laserPointX*\laserPointX+\laserPointY*\laserPointY+\laserPointZ*\laserPointZ}
    \def\moduloLaser{sqrt{(\moduloQuadro)}}

    % laser plane
    \filldraw[red!50, opacity = 0.2] (\laserPointX,\laserPointY,\laserPointZ) -- (0,-0.8,0) -- (0,0.8,0) -- cycle;
    \shade [ball color=red] (\laserPointX,\laserPointY,\laserPointZ) circle [radius=0.05cm];

    % axis laser
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (1.5*\laserPointZ/\moduloLaser,0,-1.5*\laserPointX/\moduloLaser) node[anchor=west]{$y_{laser}$};
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=east]{$x_{laser}$};
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (\laserPointX/\moduloLaser*1.5,\laserPointY/\moduloLaser*1.5,\laserPointZ/\moduloLaser*1.5) node[anchor=south]{$z_{laser}$};

    % axis world
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (1.5,0,0) node[anchor=north west]{$y_{world}$};
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=north]{$x_{world}$};
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,0,1.5) node[anchor=south]{$z_{world}$};

    \end{tikzpicture}
  }
\end{animateinline}

\end{document}

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.