4
$\begingroup$

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

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

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

Console output:

enter image description here

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

$\endgroup$
  • $\begingroup$ It would be nice if you could include in your post what libraries you use. Your using Unity right? $\endgroup$ – joojaa Dec 27 '15 at 15:35
  • $\begingroup$ Using Bullet physics and freeglut $\endgroup$ – David Dec 27 '15 at 15:39
  • $\begingroup$ Not Unity github.com/damorton/bullet-dominos $\endgroup$ – David Dec 27 '15 at 22:06
5
$\begingroup$

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

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

Mathematica source for image

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – David Dec 27 '15 at 14:38
  • $\begingroup$ I'm multiplying the result by 180 * Pi to convert to radians $\endgroup$ – David Dec 27 '15 at 14:52
  • $\begingroup$ Programming language is c++ $\endgroup$ – David Dec 27 '15 at 14:57
  • $\begingroup$ Atan2 c++ cplusplus.com/reference/cmath/atan2 $\endgroup$ – David Dec 27 '15 at 15:02
  • $\begingroup$ ill update the question with what I have so far can you have a look? $\endgroup$ – David Dec 27 '15 at 15:13

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.