<h2>The problem</h2>

<p>I'm using a 3D extension of the well-known "<a href="https://en.wikipedia.org/wiki/Turtle_graphics" rel="nofollow noreferrer">Turtle graphics</a>" to draw plants. My system works by maintaining a position <span class="math-container">$\vec{p}$</span> and three vectors:</p>

<ul>
<li>The heading <span class="math-container">$\vec{H}$</span></li>
<li>The "up" direction <span class="math-container">$\vec{U}$</span></li>
<li>The "left" direction <span class="math-container">$\vec{L}$</span></li>
</ul>

<p>My problem is now that I want to be able to create 3D objects at the position of the turtle, but with the added difficulty of being "transformed" into the turtle's coordinate system defined by the vectors <span class="math-container">$\vec{H}, \vec{U}$</span> and <span class="math-container">$\vec{L}$</span>, so that the model is "aligned" with the direction and orientation of the turtle. I basically want to achieve that the model is transformed in such a way, that:</p>

<ul>
<li>The local origin becomes the turtle position</li>
<li>The local x-Axis is rotated onto the heading vector <span class="math-container">$\vec{H}$</span></li>
<li>The local y-Axis is rotated onto the right vector <span class="math-container">$- \vec{L}$</span></li>
<li>The local z-Axis is rotated onto the up vector <span class="math-container">$\vec{U}$</span></li>
</ul>

<p>The reason I want this is to be able to spawn 3D models that describe the leaves of a plant on stalks generated by the turtle.</p>

<h2>My own idea so far</h2>

<p>I'm not very good at linear algebra, but I thought of using a base change matrix of the form:
<span class="math-container">$$A := \begin{pmatrix}\vec{H} &amp; -\vec{L} &amp; \vec{U} \end{pmatrix}$$</span>
And combining that with the translation by <span class="math-container">$\vec{p}$</span> to obtain the following model matrix:</p>

<p><span class="math-container">$$T := \begin{pmatrix} A &amp; \begin{matrix} p_1 \\ p_2 \\ p_3\end{matrix}\\ 
\begin{matrix}0 &amp; 0&amp; 0\end{matrix} &amp; 1
\end{pmatrix}$$</span></p>

<p>Would this achieve the effect I want? I am currently only in the planning phase of the project this will be used in, so I can't check this myself.</p>

CC BY-SA 4.0
2019-12-16T22:28:24.343Z
Sure that should work
joojaa
CC BY-SA 4.0
2019-12-16T22:58:34.237Z
@joojaa Thanks! In addition, i also wanna scale the model by a factor $f$. Do i have to apply the scaling matrix before $T$ (as in $T  \cdot S$), or after it (as in $S\cdot T$)? I never worked with base change matrices before.
Katharina
CC BY-SA 4.0
2019-12-17T07:19:26.910Z
Depends on about what you want to scale. OTOH if you do a $T^{-1} S T$ it does not matter much.
joojaa
CC BY-SA 4.0
2019-12-17T14:37:33.287Z
@joojaa I basically want to scale the model I am placing, so I would like to basically scale it, and then move it into the new coordinate system. That would be $T * S$ then, right?
nshct
CC BY-SA 4.0
2019-12-17T14:53:19.807Z
But scale about what point
joojaa
CC BY-SA 4.0
2019-12-17T16:49:41.897Z
@joojaa the local model origin
nshct
<transformations><model><linear-algebra>
Transformation for aligning 3D object with reference frame of 3D turtle graphics
Katharina
# Transformation for aligning 3D object with reference frame of 3D turtle graphics

(Asked by: username_0 on 2019-12-16)

## The problem

I'm using a 3D extension of the well-known "[Turtle graphics](https://en.wikipedia.org/wiki/Turtle_graphics)" to draw plants. My system works by maintaining a position $\vec{p}$ and three vectors:

* The heading $\vec{H}$
* The "up" direction $\vec{U}$
* The "left" direction $\vec{L}$

My problem is now that I want to be able to create 3D objects at the position of the turtle, but with the added difficulty of being "transformed" into the turtle's coordinate system defined by the vectors $\vec{H}, \vec{U}$ and $\vec{L}$, so that the model is "aligned" with the direction and orientation of the turtle. I basically want to achieve that the model is transformed in such a way, that:

* The local origin becomes the turtle position
* The local x-Axis is rotated onto the heading vector $\vec{H}$
* The local y-Axis is rotated onto the right vector $- \vec{L}$
* The local z-Axis is rotated onto the up vector $\vec{U}$

The reason I want this is to be able to spawn 3D models that describe the leaves of a plant on stalks generated by the turtle.

## My own idea so far

I'm not very good at linear algebra, but I thought of using a base change matrix of the form:
$$A := \begin{pmatrix}\vec{H} & -\vec{L} & \vec{U} \end{pmatrix}$$
And combining that with the translation by $\vec{p}$ to obtain the following model matrix:

$$T := \begin{pmatrix} A & \begin{matrix} p\_1 \\ p\_2 \\ p\_3\end{matrix}\\
\begin{matrix}0 & 0& 0\end{matrix} & 1
\end{pmatrix}$$

Would this achieve the effect I want? I am currently only in the planning phase of the project this will be used in, so I can't check this myself.

**Tags:** transformations, model, linear-algebra

> **Comments:**
> **username_1**: Sure that should work  
> **username_0**: @username_1 Thanks! In addition, i also wanna scale the model by a factor $f$. Do i have to apply the scaling matrix before $T$ (as in $T  \cdot S$), or after it (as in $S\cdot T$)? I never worked with base change matrices before.  
> **username_1**: Depends on about what you want to scale. OTOH if you do a $T^{-1} S T$ it does not matter much.  
> **username_2**: @username_1 I basically want to scale the model I am placing, so I would like to basically scale it, and then move it into the new coordinate system. That would be $T * S$ then, right?  
> **username_1**: But scale about what point  
> **username_2**: @username_1 the local model origin  

---

## 0 answers

