# Problem with a ball in 3D with Tikz

Si I want to represent the earth with a sphere/ball anything. I have found something that matches in my needs in 2D : \shade[ball color=blue!20!white,opacity=0.90] (120:3) circle (1cm); (although the opacity do not seem to change anything...). For instance I can do that :

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{decorations.pathmorphing,patterns,decorations,shapes,arrows,intersections,matrix,fit,calc,trees,positioning,arrows,chains,shapes.geometric,shapes,angles,quotes}
\begin{document}
\begin{tikzpicture}
\draw (0,0) node[below right]{$T$};
\draw[->] (0,0)--(2,0) node[right]{$x$};
\draw[->] (0,0)--(0,2) node[above]{$y$};
\draw[->] (0,0)--(-1.5,-1.2) node[below left]{$z$};
\draw (0,1.8) node[left]{$\mathcal{R}_{geo}$};
\draw[->, red] (0,0,0)--(1,1.8,0) node[right]{$\vv{\Omega}=\vv{\Omega}(\mathcal{R}_T/\mathcal{R}_{geo})$};
\end{tikzpicture}
\end{document}


And i get that :

But when I go in 3D, it implicitely goes in some plane

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{decorations.pathmorphing,patterns,decorations,shapes,arrows,intersections,matrix,fit,calc,trees,positioning,arrows,chains,shapes.geometric,shapes,angles,quotes}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1,cm={-1,-1,1,0,(0,0)},x=3.85mm,z=-1cm]
\draw[->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[->] (0,0,0) -- (0,4,0) node[right]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[above]{$z$};
\draw (0,0,2) node[left]{$\mathcal{R}_C$};
\draw (0,0,0) node[below right] {$S$};

\draw (140:3) arc (140:170:3);
\draw[dotted] (170:3) arc (170:360:3);

%\draw (120:3) circle (1.5cm);

\draw (70:3) arc (70:140:3);
\draw[dotted] (0:3) arc (0:70:3);

\end{tikzpicture}
\end{center}


And this is less satisfying :

So a couple of questions (the last one would be the main one, the other are more there is someone reading has an on the fly solution) :

• why does the opacity do not do anything in the first example
• Is there by any chance, an existing package that would enable me to use an actual earth as an earth ?
• If not, is there a better way to get some kind of sphere/ball to represent earth ?
• More directly, how can I get my desired ball in 3D ? I do not even understand how the ellipse I get can be so different from the one I get with the dotted line.
• Have you looked at tikz-3dplot? But I guess you know that TikZ is not really designed for 3D. It only knows 2D. 3D can be faked in simple cases, but if you need more than that, you need to look elsewhere. (tikz-3dplot helps with the faking, but it is still faking.) – cfr Nov 8 '16 at 0:34
• I feared that answer, asI have already invested in Tikz. What would be the package I would need to learn in order to build correct 3D figures ? – Anthony Martin Nov 8 '16 at 6:56
• Asymptote is one, but I believe there are other options as well. – cfr Nov 8 '16 at 15:57

This becomes somewhat easier to do if you go 3D by redefining the coordinate system rather than redefining the distances. Instead of setting cm= you can set x=, y=, and z= to get your desired 3D coordinates. Then if you want to draw a circle (or anything else) on the paper instead of in the xy-plane you can just specify its radius (or other dimensions) in cm rather than in tikz coordinates:

\documentclass[tikz, border=3pt]{standalone}
% \usetikzlibrary{decorations.pathmorphing,patterns,decorations,shapes,arrows,intersections,matrix,fit,calc,trees,positioning,arrows,chains,shapes.geometric,shapes,angles,quotes}
\begin{document}
\begin{tikzpicture}[scale=1, x={(225:.5)}, y={(1cm,0cm)}, z={(0cm,1cm)}]

\draw[->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[->] (0,0,0) -- (0,4,0) node[right]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[above]{$z$};
\draw (0,0,2) node[left]{$\mathcal{R}_C$};
\draw (0,0,0) node[below right] {$S$};

\draw (100:3) arc (100:170:3);
\draw[dotted] (170:3) arc (170:360:3);

\draw (70:3) arc (70:100:3);
\draw[dotted] (0:3) arc (0:70:3);
\end{tikzpicture}
\end{document}


In particular, compare the results of \draw (120:3) circle (1cm); vs. \draw (120:3) circle (1); in the diagram above.

(Also note that you can draw circles in planes other than the xy-plane by locally setting x and y, e.g., to draw your circle in the xz plane use \draw (120:3) [y={(0,0,1)}] circle (1);'. Here the center coordinate is still calculated in the outside coordinate system.)

• Like always it is exactly what I needed. Could you please just elaborate a little on you two last points ? I did not understand how the circle(1) produce the sphere I got. An how does the {[y={(0,0,1)}] works ? I would have understood {[y={(1,0,1)}] to indicate the xz plane, but why just z=1 ? – Anthony Martin Nov 9 '16 at 9:07
• @Aerandal The transformation changes the x, y, z coordinates, but when you specify units tikz reverts to page coordinates. So a circle with radius 1 is drawn as a circle with radius 1 in the xy plane (whatever that currently is), while a circle with radius 1cm is drawn with radius 1cm in the page coordinate system. That didn't work in your original version because you were redefining the cm units rather than the coordinate system. – Emma Nov 11 '16 at 3:08
• For the second question, circles (with radius given in coordinate system rather than page units) are drawn in the xy plane. So if you want to draw in the xz plane you can (temporarily) set y=z. More generally, to draw a in any plane you want you can set x and y to an orthonormal basis of that plane. (Caveat: if you do this you may want to set them to named coordinates instead of specifying their locations in the coordinate system, since the second one you set will be calculated using the new value for the first one you set.) – Emma Nov 11 '16 at 3:13

TikZ/PGF only do 2D. In simple cases, they can be used to fake 3D. tikz-3dplot can make the calculations required to do the faking easier. However, if you need more than fake, you need to look elsewhere to programmes which deal with 3D objects as 3D objects.

The opacity setting works fine. If I put the ball over the axes, the axes are visible through it.

I'm not quite sure what you are trying to do. The reason the ball looks different in the 3D case is because you are transforming all coordinates systematically. Do you simply want to take the ball outside the scope of that transformation?

That is, do you want one of the two possibilities shown here?

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[scale=1,cm={-1,-1,1,0,(0,0)},x=3.85mm,z=-1cm]
\draw[->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[->] (0,0,0) -- (0,4,0) node[right]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[above]{$z$};
\draw (0,0,2) node[left]{$\mathcal{R}_C$};
\draw (0,0,0) node[below right] {$S$};
\draw (140:3) arc (140:170:3);
\draw[dotted] (170:3) arc (170:360:3);
\draw (70:3) arc (70:140:3);
\draw[dotted] (0:3) arc (0:70:3);
\end{scope}
\end{tikzpicture}
\end{document}


# EDIT

To place the Earth on the trajectory, it is probably easiest to name coordinates within the scope of the transformation and draw the Earth outside. To put the Earth 'behind' the axes etc, the backgrounds library can be used.

Note that because TikZ does not do 3D, the drawing-order is critical if multiple Earths are required.

For example,

requires at least 2 loops to draw the Earths unless you avoid this by taking care to name the coordinates in an appropriate order.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\begin{scope}[scale=1,cm={-1,-1,1,0,(0,0)},x=3.85mm,z=-1cm]
\draw[->] (0,0,0) -- (4,0,0) node[anchor=north east]{$x$};
\draw[->] (0,0,0) -- (0,4,0) node[right]{$y$};
\draw[->] (0,0,0) -- (0,0,3) node[above]{$z$};
\draw (0,0,2) node[left]{$\mathcal{R}_C$};
\draw (0,0,0) node[below right] {$S$};
\draw (140:3) arc (140:170:3);
\draw[dotted] (170:3) arc (170:430:3) \foreach \i [count=\j] in {0,.1,.2,...,1} { coordinate [pos=\i] (e\j) } ;
\draw (70:3) arc (70:140:3) \foreach \i [count=\j from 12] in {.5,1} { coordinate [pos=\i] (e\j) } ;
\end{scope}
\begin{scope}[on background layer]
\foreach \i in {1,...,6,13,12}
`