Skip to content

Instantly share code, notes, and snippets.

@ase34
Created April 30, 2014 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ase34/36ca784eb9945ce59f08 to your computer and use it in GitHub Desktop.
Save ase34/36ca784eb9945ce59f08 to your computer and use it in GitHub Desktop.
whitepaper latex code
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usepackage{fullpage}
\usepackage{tikz}
\usepackage{3dplot}
\usepackage{float}
\usepackage{fancyhdr}
\newcommand{\revision}{5}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\pagestyle{fancyplain}
\setlength{\headheight}{15.2pt}
\fancyhf{}
\cfoot{\thepage}
\lfoot{Revision \revision}
\rfoot{\today}
\renewcommand{\headrulewidth}{0pt}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true
tabsize=3
}
\begin{document}
\title{Intersection of a line and a plane for usage in Minecraft/Bukkit\\[20pt]
\large Revision \revision}
\author{ase34 - \url{http://ase34.github.io}}
\date{\today}
\maketitle
\section{Introduction}
This paper tries to provide a way for \textit{Bukkit}\footnote{\url{http://bukkit.org}} plugin developers to compute the intersection of the player's line of vision (or any other line) with a defined plane, for example a face of a block, or the surface of a map in an item frame (See Figure \ref{fig:frame}).
\begin{figure}[H]
\centering
\includegraphics[width=0.3\textwidth]{frame.jpg}
\caption{\label{fig:frame}An example usage by "drawing" on a map hanging on a frame. Animated version\protect\footnotemark}
\end{figure}
\footnotetext{\url{http://imgur.com/xQglGEF}}
\section{Defining the player's line of vision}
The player's line of vision is defined by \textit{two} vectors, the player's location vector $\mathbf{l_0}$, and and the player's direction vector $\mathbf{l}$. Every point $\mathbf{p}$ on the line are defined if it can solve the following equation:
%
$$\mathbf{p} = d\mathbf{l} + \mathbf{l_0} \quad d\in\mathbb{R}$$
\section{Defining the plane}
The block's plane is defined in a similar way as the line of vision, but with \textit{three} vectors, a location $\mathbf{p_0}$ defining the origin point, one vector $\mathbf{u}$ defining the x-axis and another one $\mathbf{v}$ defining the y-axis. They both are generally unit vectors pointing in one of the six main axes (X/Y/Z/-X/-Y/-Z). Every point $\mathbf{p}$ on the plane are defined if it can solve the following equation:
%
$$\mathbf{p} = \mathbf{p_0} + s\mathbf{u} + t\mathbf{v} \quad s, t\in\mathbb{R}$$
\section{Setting up the equation system}
First, we equate the two equation, resulting in:
%
$$d\mathbf{l} + \mathbf{l_0} = \mathbf{p_0} + s\mathbf{u} + t\mathbf{v} \quad d, s, t\in\mathbb{R}$$
%
%
or, in full form:
%
\begin{align*}
d\mathbf{l}_1 + \mathbf{l_0}_1 &= \mathbf{p_0}_1 + s\mathbf{u}_1 + t\mathbf{v}_1\\
d\mathbf{l}_2 + \mathbf{l_0}_2 &= \mathbf{p_0}_2 + s\mathbf{u}_2 + t\mathbf{v}_2\\
d\mathbf{l}_3 + \mathbf{l_0}_3 &= \mathbf{p_0}_3 + s\mathbf{u}_3 + t\mathbf{v}_3
\end{align*}
%
%
As there are \textit{three} unknowns and \textit{three} equations, we can solve this system in the next step.
\section{Solving the equation system}
To solve the equation system, we use Cramer's rule\footnote{\url{http://en.wikipedia.org/wiki/Cramer\%27s_rule}} and determinants. First, we transform the system in a more standard form:
%
$$d\mathbf{l} - s\mathbf{u} - t\mathbf{v} = \mathbf{p_0} - \mathbf{l_0}$$
%
%
and in full form:
%
\begin{align*}
d\mathbf{l}_1 - s\mathbf{u}_1 - t\mathbf{v}_1 = \mathbf{p_0}_1 - \mathbf{l_0}_1\\
d\mathbf{l}_2 - s\mathbf{u}_2 - t\mathbf{v}_2 = \mathbf{p_0}_2 - \mathbf{l_0}_2\\
d\mathbf{l}_3 - s\mathbf{u}_3 - t\mathbf{v}_3 = \mathbf{p_0}_3 - \mathbf{l_0}_3
\end{align*}
%
%
or in matrix form:
%
$$
\begin{bmatrix}
\mathbf{l}_1 & -\mathbf{u}_1 & -\mathbf{v}_1 \\
\mathbf{l}_2 & -\mathbf{u}_2 & -\mathbf{v}_2 \\
\mathbf{l}_3 & -\mathbf{u}_3 & -\mathbf{v}_3 \\
\end{bmatrix}
\begin{bmatrix}
d \\
s \\
t \\
\end{bmatrix}
= \begin{bmatrix}
\mathbf{p_0}_1 - \mathbf{l_0}_1 \\
\mathbf{p_0}_2 - \mathbf{l_0}_2 \\
\mathbf{p_0}_3 - \mathbf{l_0}_3 \\
\end{bmatrix}
$$
%
%
Now the unknowns can be calculated as follown:
%
\begin{align*}
d &= \frac{
\begin{vmatrix}
\mathbf{p_0}_1 - \mathbf{l_0}_1 & -\mathbf{u}_1 & -\mathbf{v}_1 \\
\mathbf{p_0}_2 - \mathbf{l_0}_2 & -\mathbf{u}_2 & -\mathbf{v}_2 \\
\mathbf{p_0}_3 - \mathbf{l_0}_3 & -\mathbf{u}_3 & -\mathbf{v}_3 \\
\end{vmatrix}
}
{
\begin{vmatrix}
\mathbf{l}_1 & -\mathbf{u}_1 & -\mathbf{v}_1 \\
\mathbf{l}_2 & -\mathbf{u}_2 & -\mathbf{v}_2 \\
\mathbf{l}_3 & -\mathbf{u}_3 & -\mathbf{v}_3 \\
\end{vmatrix}
}, \\
s &= \frac{
\begin{vmatrix}
\mathbf{l}_1 & \mathbf{p_0}_1 - \mathbf{l_0}_1 & -\mathbf{v}_1 \\
\mathbf{l}_2 & \mathbf{p_0}_2 - \mathbf{l_0}_2 & -\mathbf{v}_2 \\
\mathbf{l}_3 & \mathbf{p_0}_3 - \mathbf{l_0}_3 & -\mathbf{v}_3 \\
\end{vmatrix}
}
{
\begin{vmatrix}
\mathbf{l}_1 & -\mathbf{u}_1 & -\mathbf{v}_1 \\
\mathbf{l}_2 & -\mathbf{u}_2 & -\mathbf{v}_2 \\
\mathbf{l}_3 & -\mathbf{u}_3 & -\mathbf{v}_3 \\
\end{vmatrix}
}, \\
t &= \frac{
\begin{vmatrix}
\mathbf{l}_1 & -\mathbf{u}_1 & \mathbf{p_0}_1 - \mathbf{l_0}_1 \\
\mathbf{l}_2 & -\mathbf{u}_2 & \mathbf{p_0}_2 - \mathbf{l_0}_2 \\
\mathbf{l}_3 & -\mathbf{u}_3 & \mathbf{p_0}_3 - \mathbf{l_0}_3 \\
\end{vmatrix}
}
{
\begin{vmatrix}
\mathbf{l}_1 & -\mathbf{u}_1 & -\mathbf{v}_1 \\
\mathbf{l}_2 & -\mathbf{u}_2 & -\mathbf{v}_2 \\
\mathbf{l}_3 & -\mathbf{u}_3 & -\mathbf{v}_3 \\
\end{vmatrix}
}
\end{align*}\\
Then, the intersection point relative to the origin point $\mathbf{l_0}$ and with respect of the defined axes $\mathbf{u}$ and $\mathbf{v}$ is $(s, t)$. Also, see figure \ref{fig:block}.\\
To get to the absolute position of the intersection, either use:
$$d\mathbf{l} + \mathbf{l_0}$$
%
%
or:
%
$$\mathbf{p_0} + s\mathbf{u} + t\mathbf{v}$$
%
%
with the calculated values of $d$ or $s$ and $t$.
\section{Implementation in Java}
\label{sec:code}
\begin{lstlisting}
public static double determinant(double[][] matrix) {
return matrix[0][0] * matrix[1][1] * matrix[2][2] + matrix[0][1] * matrix[1][2] * matrix[2][0] + matrix[0][2] * matrix[1][0] * matrix[2][1] - matrix[0][2] * matrix[1][1] * matrix[2][0] - matrix[0][1] * matrix[1][0] * matrix[2][2] - matrix[0][0] * matrix[1][2] * matrix[2][1];
}
public static Vector getIntersection(Vector linePoint, Vector lineDirection, Vector planeOrigin, Vector planeXDirection, Vector planeYDirection) {
double[][] coefficients = {
{lineDirection.getX(), -planeXDirection.getX(), -planeYDirection.getX()},
{lineDirection.getY(), -planeXDirection.getY(), -planeYDirection.getY()},
{lineDirection.getZ(), -planeXDirection.getZ(), -planeYDirection.getZ()}
};
double[] solutions = {
planeOrigin.getX() - linePoint.getX(),
planeOrigin.getY() - linePoint.getY(),
planeOrigin.getZ() - linePoint.getZ()
};
double[][] dMatrix = {
{solutions[0], coefficients[0][1], coefficients[0][2]},
{solutions[1], coefficients[1][1], coefficients[1][2]},
{solutions[2], coefficients[2][1], coefficients[2][2]}
};
double[][] sMatrix = {
{coefficients[0][0], solutions[0], coefficients[0][2]},
{coefficients[1][0], solutions[1], coefficients[1][2]},
{coefficients[2][0], solutions[2], coefficients[2][2]}
};
double[][] tMatrix = {
{coefficients[0][0], coefficients[0][1], solutions[0]},
{coefficients[1][0], coefficients[1][1], solutions[1]},
{coefficients[2][0], coefficients[2][1], solutions[2]}
};
double det = determinant(coefficients);
double d = determinant(dMatrix) / det;
double s = determinant(uMatrix) / det;
double t = determinant(vMatrix) / det;
return new Vector(s, t, 0);
}
\end{lstlisting}
%
%
This piece of code is also available on GitHub Gists\footnote{\url{https://gist.github.com/ase34/11375298}}\\
The method arguments are (also compare with figure \ref{fig:block}):
\begin{description}
\item[\texttt{linePoint}]
$\mathbf{l_0}$ - The position of a point on the line, normally the player's location vector
\item[\texttt{lineDirection}]
$\mathbf{l}$ - The direction of the line, normally the player's direction vector
\item[\texttt{planeOrigin}]
$\mathbf{p_0}$ - The location of the plane's origin, normally located at the top left corner of a block face
\item[\texttt{planeXAxis}]
$\mathbf{u}$ - The X axis vector, normally pointing to the top right corner of a block face
\item[\texttt{planeYAxis}]
$\mathbf{v}$ - The Y axis vector, normally pointing to the bottom left corner of a block face
\end{description}
\section{Example usage}
\tdplotsetmaincoords{60}{110}
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=3,tdplot_main_coords]
\draw[thick,->] (0,0,0) -- (2,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,2,0) node[anchor=north west]{$z$};
\draw[thick,->] (0,0,0) -- (0,0,2) node[anchor=south]{$y$};
\draw[dashed] (0, 0, 0) -- (0, 0, 1);
\draw[dashed] (0, 0, 0) -- (1, 0, 0);
\draw[dashed] (0, 0, 0) -- (0, 1, 0);
\draw[dashed] (1, 0, 0) -- (1, 0, 1);
\draw[dashed] (1, 0, 0) -- (1, 1, 0);
\draw[dashed] (0, 1, 0) -- (0, 1, 1);
\draw[dashed] (0, 1, 0) -- (1, 1, 0);
\draw[dashed] (0, 0, 1) -- (0, 1, 1);
\draw[dashed] (0, 0, 1) -- (1, 0, 1);
\draw[dashed] (1, 1, 1) -- (0, 1, 1);
\draw[dashed] (1, 1, 1) -- (1, 0, 1);
\draw[dashed] (1, 1, 1) -- (1, 1, 0);
\filldraw[draw=red, fill=red!20!white, opacity=0.4]
(1, 0, 0) node[anchor=north, opacity=1]{$(125.0, 71.0, -19.0)$} -- (1, 1, 0) node[anchor=north, opacity=1]{$(125.0, 71.0, -18.0)$} -- (1, 1, 1) node[anchor=west, opacity=1]{$(125.0, 72.0, -18.0)$} -- (1, 0, 1) node[anchor=east, opacity=1]{$(125.0, 72.0, -19.0)$} node[anchor=south, opacity=1]{\color{blue}$\mathbf{p_0}$} -- (1, 0, 0);
\draw[->, draw=blue] (1, 0, 1) -- (1, 1, 1) node[anchor=south]{\color{blue}$\mathbf{u} = (0, 0, 1)$};
\draw[->, draw=blue] (1, 0, 1) -- (1, 0, 0) node[anchor=south east]{\color{blue}$\mathbf{v} = (0, -1, 0)$};
\coordinate (P) at (1, 0.7, 0.4);
\draw[dotted, thick, draw=red] (1, 0.7, 1) -- (P) node[midway] {\color{red}$t\mathbf{v}$};
\draw[dotted, thick, draw=red] (1, 0, 0.4) -- (P) node[anchor=north west] {\color{red}$P_i$} node[midway] {\color{red}$s\mathbf{u}$};
\end{tikzpicture}
\caption{\label{fig:block} A \textit{Minecraft} coordinate system with a block. $P_i$ specifies the intersection point.}
\end{figure}
The following piece of code demonstrates an example usage of the above method for retrieving the variables $s$ and $t$ for finding the intersection point of a block's face and the player's vision with given player's location and direction, and given $\mathbf{p_0}$, $\mathbf{u}$ and $\mathbf{v}$.
\begin{lstlisting}
Player player; // required variable
Vector planeOrigin = new Vector(125.0, 72.0, -19.0); // p_0 in the figure
Vector planeXDirection = new Vector(0, 0, 1); // u in the figure
Vector planeYDirection = new Vector(0, -1, 0); // v in the figure
Vector intersection = getIntersection(player.getEyeLocation().toVector(), player.getLocation().getDirection(), planeOrigin, planeXDirection, planeYDirection);
System.out.println("s = " + intersection.getX());
System.out.println("t = " + intersection.getY());
\end{lstlisting}
\section{License}
The snippet of code in section~\ref{sec:code} has been released under the terms of \textit{The Unlicense}\footnote{\url{http://unlicense.org/}}
\begin{quote}
This is free and unencumbered software released into the public domain.\\
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.\\
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.\\
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.\\
For more information, please refer to \url{http://unlicense.org/}
\end{quote}
%
%
Although the license does not require you to do so, I appreciate every credit I receive. Thank you!
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment