<p>The parametric equation for a spiral is:</p>

<p>$$
\begin{eqnarray}
\begin{aligned}
    x &amp;= &amp;(a + b \theta) \times \cos(\theta)\\
    z &amp;= &amp;(a + b \theta) \times \sin(\theta)
\end{aligned}
\end{eqnarray}
$$</p>

<p>The direction on the spiral point is simply the derivative of the system with respect to $\theta$. Thus the direction is: </p>

<p>$$
\begin{eqnarray}
\begin{aligned}
 x_{dir} &amp;=&amp;-(a + b \theta) \times \sin(\theta) + b \times \cos(\theta)\\
 z_{dir} &amp;=&amp;(a + b \theta) \times \cos(\theta) + b \times \sin(\theta)
\end{aligned}
\end{eqnarray}
$$</p>

<p>Finally angle is is simply atan2 of the direction equation. </p>

<p><a href="https://i.sstatic.net/bcKzY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bcKzY.png" alt="Spiral"></a></p>

<p><strong>Image 1</strong>: This is what I get when i plot squares at $(x, z)$ with direction $(x_{dir}, z_{dir})$</p>

<p>Mathematica <a href="http://pastebin.com/ukNFUiPc" rel="nofollow noreferrer">source</a> for image</p>

CC BY-SA 3.0
2015-12-27T14:38:35.250Z
I tried `float dirX = -(a + b * angle) * sin(angle) + b * cos(angle);` and `float dirZ = (a + b * angle) * cos(angle) + b * sin(angle);
` then `float newAngle = atan2(dirZ, dirX);` but this does not set the angle correctly.
David
CC BY-SA 3.0
2015-12-27T14:52:42.093Z
I'm multiplying the result by 180 * Pi to convert to radians
David
CC BY-SA 3.0
2015-12-27T14:57:16.397Z
Programming language is c++
David
CC BY-SA 3.0
2015-12-27T15:02:54.920Z
Atan2 c++ http://www.cplusplus.com/reference/cmath/atan2/
David
CC BY-SA 3.0
2015-12-27T15:13:48.623Z
ill update the question with what I have so far can you have a look?
David
CC BY-SA 3.0
2015-12-27T16:33:02.170Z
Ive just noticed that your circle is spiraling in the opposite direction to mine
David
CC BY-SA 3.0
2015-12-27T17:51:48.493Z
@damorton It depends on how your csys is defined and from which side of the spiral you look from. These are arbitrary definitions. I just have a right handed coordinate system in a 2d space.
joojaa
CC BY-SA 3.0
2015-12-28T17:52:47.107Z
Turned out that the above was perfect, the problem was with the dimensions of each domino. Swapping the width and height values fix it :/ thanks @joojaa
David
CC BY-SA 3.0
2015-12-27T14:20:42.503Z
2015-12-27T15:16:17.587Z
2015-12-27T15:16:17.587Z
joojaa
<p>I'm trying to rotate a rectangle so that it faces another rectangle in a spiral.</p>

<p>Im using the Bullet pyhics library and freeglut for the 3d application, for anyone interested <a href="https://github.com/damorton/bullet-dominos" rel="nofollow noreferrer">https://github.com/damorton/bullet-dominos</a></p>

<p><a href="https://i.sstatic.net/I300x.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/I300x.png" alt="enter image description here"></a></p>

<pre><code>float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

for (int i = 4; i &lt; maxPoints; i++)
{
    angle = 0.1 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float newAngle = atan2(previousZ - z, previousX - x) * 180 / PI;
    temp-&gt;setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}
</code></pre>

<p>You can see from the top view that the rectangles are not pointing towards the previous rectangles position. </p>

<p><a href="https://i.sstatic.net/sk2W2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/sk2W2.png" alt="enter image description here"></a></p>

<p>EDIT: After trying the below code</p>

<pre><code>float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

for (int i = 0; i &lt; maxPoints; i++)
{
    angle = 0.1 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float dirX = -(a + b * angle) * sin(angle) + b * cos(angle);
    float dirZ = (a + b * angle) * cos(angle) + b * sin(angle);
    float newAngle = atan2(dirZ, dirX) * 180 / PI;
    temp-&gt;setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}
</code></pre>

<p>The result is:</p>

<p><a href="https://i.sstatic.net/ypk6t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ypk6t.png" alt="enter image description here"></a></p>

<p>Console output:</p>

<p><a href="https://i.sstatic.net/LFkYQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LFkYQ.png" alt="enter image description here"></a></p>

<p>EDIT: Solution. Some things to note are that the result of <code>atan2()</code> was not converted to degrees using <code>atan2() * 180 / PI</code>. Also that I passed in <code>atan2(X, Z)</code> compared to <code>atan2(Z, X)</code> that is sometimes done for hysterical raisins <a href="http://www2.tcl.tk/10814" rel="nofollow noreferrer">http://www2.tcl.tk/10814</a>.</p>

<pre><code>// create spiral dominos
float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

float dominoHeight = 2.0f;

for (int i = 2; i &lt; maxPoints; i++)
{
    if (i % 30 == 0) dominoHeight++;

    angle = 0.15 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float dirX = -(a + b * angle) * sin(angle) + (b * cos(angle));
    float dirZ = (a + b * angle) * cos(angle) + (b * sin(angle));
    float newAngle = atan2(dirX, dirZ);
    printf("dirX: %f - dirZ: %f - angle: %f\n", dirX, dirZ, newAngle);
    temp-&gt;setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}
</code></pre>

<p><a href="https://i.sstatic.net/KmM1S.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KmM1S.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-12-27T15:35:39.203Z
It would be nice if you could include in your post what libraries you use. Your using Unity right?
joojaa
CC BY-SA 3.0
2015-12-27T15:39:25.653Z
Using Bullet physics and freeglut
David
CC BY-SA 3.0
2015-12-27T22:06:55.920Z
Not Unity https://github.com/damorton/bullet-dominos
David
<transformations><c++>
Angle between two points in Cartesian coordinate system C++
David
# Angle between two points in Cartesian coordinate system C++

(Asked by: username_0 on 2015-12-27)

I'm trying to rotate a rectangle so that it faces another rectangle in a spiral.

Im using the Bullet pyhics library and freeglut for the 3d application, for anyone interested <https://github.com/damorton/bullet-dominos>

[![enter image description here](https://i.sstatic.net/I300x.png)](https://i.sstatic.net/I300x.png)

```
float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

for (int i = 4; i < maxPoints; i++)
{
    angle = 0.1 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float newAngle = atan2(previousZ - z, previousX - x) * 180 / PI;
    temp->setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}

```

You can see from the top view that the rectangles are not pointing towards the previous rectangles position.

[![enter image description here](https://i.sstatic.net/sk2W2.png)](https://i.sstatic.net/sk2W2.png)

EDIT: After trying the below code

```
float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

for (int i = 0; i < maxPoints; i++)
{
    angle = 0.1 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float dirX = -(a + b * angle) * sin(angle) + b * cos(angle);
    float dirZ = (a + b * angle) * cos(angle) + b * sin(angle);
    float newAngle = atan2(dirZ, dirX) * 180 / PI;
    temp->setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}

```

The result is:

[![enter image description here](https://i.sstatic.net/ypk6t.png)](https://i.sstatic.net/ypk6t.png)

Console output:

[![enter image description here](https://i.sstatic.net/LFkYQ.png)](https://i.sstatic.net/LFkYQ.png)

EDIT: Solution. Some things to note are that the result of `atan2()` was not converted to degrees using `atan2() * 180 / PI`. Also that I passed in `atan2(X, Z)` compared to `atan2(Z, X)` that is sometimes done for hysterical raisins <http://www2.tcl.tk/10814>.

```
// create spiral dominos
float x = 0;
float z = 0;
float angle = 0.0f;
int a = 2, b = 2;
float previousX = 0.0f;
float previousZ = 0.0f;

float dominoHeight = 2.0f;

for (int i = 2; i < maxPoints; i++)
{
    if (i % 30 == 0) dominoHeight++;

    angle = 0.15 * i;
    x = (a + b * angle) * cos(angle);
    z = (a + b * angle) * sin(angle);

    GameObject* temp = CreateGameObject(x, 0, z);

    float dirX = -(a + b * angle) * sin(angle) + (b * cos(angle));
    float dirZ = (a + b * angle) * cos(angle) + (b * sin(angle));
    float newAngle = atan2(dirX, dirZ);
    printf("dirX: %f - dirZ: %f - angle: %f\n", dirX, dirZ, newAngle);
    temp->setRotationYaw(newAngle);

    previousX = x;
    previousZ = z;
}

```

[![enter image description here](https://i.sstatic.net/KmM1S.png)](https://i.sstatic.net/KmM1S.png)

**Tags:** transformations, c++

> **Comments:**
> **username_1**: It would be nice if you could include in your post what libraries you use. Your using Unity right?  
> **username_0**: Using Bullet physics and freeglut  
> **username_0**: Not Unity https://github.com/damorton/bullet-dominos  

---

## 1 answers

### Answer by username_1 on 2015-12-27. Score: 5 | Accepted answer

The parametric equation for a spiral is:

$$
\begin{eqnarray}
\begin{aligned}
x &= &(a + b \theta) \times \cos(\theta)\\
z &= &(a + b \theta) \times \sin(\theta)
\end{aligned}
\end{eqnarray}
$$

The direction on the spiral point is simply the derivative of the system with respect to $\theta$. Thus the direction is:

$$
\begin{eqnarray}
\begin{aligned}
x\_{dir} &=&-(a + b \theta) \times \sin(\theta) + b \times \cos(\theta)\\
z\_{dir} &=&(a + b \theta) \times \cos(\theta) + b \times \sin(\theta)
\end{aligned}
\end{eqnarray}
$$

Finally angle is is simply atan2 of the direction equation.

[![Spiral](https://i.sstatic.net/bcKzY.png)](https://i.sstatic.net/bcKzY.png)

**Image 1**: This is what I get when i plot squares at $(x, z)$ with direction $(x\_{dir}, z\_{dir})$

Mathematica [source](http://pastebin.com/ukNFUiPc) for image

> **Comments:**
> **username_0**: I tried `float dirX = -(a + b * angle) * sin(angle) + b * cos(angle);` and `float dirZ = (a + b * angle) * cos(angle) + b * sin(angle); ` then `float newAngle = atan2(dirZ, dirX);` but this does not set the angle correctly.  
> **username_0**: I'm multiplying the result by 180 * Pi to convert to radians  
> **username_0**: Programming language is c++  
> **username_0**: Atan2 c++ http://www.cplusplus.com/reference/cmath/atan2/  
> **username_0**: ill update the question with what I have so far can you have a look?  
> **username_0**: Ive just noticed that your circle is spiraling in the opposite direction to mine  
> **username_1**: @damorton It depends on how your csys is defined and from which side of the spiral you look from. These are arbitrary definitions. I just have a right handed coordinate system in a 2d space.  
> **username_0**: Turned out that the above was perfect, the problem was with the dimensions of each domino. Swapping the width and height values fix it :/ thanks @username_1  

---

