0
\$\begingroup\$

I have a method to calculate angle between two vectors, but it is giving wrong values.

here is the code :

float Vector2_Dot(Vector2 a, Vector2 b)
{
    return a.x * b.x + a.y * b.y;
}

float Vector2_Cross(Vector2 a, Vector2 b)
{
    return a.x * b.y - a.y * b.x;
}

float Vector2_Angle(Vector2 a, Vector2 b)
{
    return atan2f(Vector2_Cross(a, b), Vector2_Dot(a, b));
}

As you can see in the image bellow the angle is slightly wrong. output

I'm giving the two positions marked by a black cross. is the code correct or Do I have a problema elsewhere?

Here's is where I do my transformation matrix calculation:

float Transform_GetLocalAngle(DeadTransform *transform)
{
    if (transform->parent != NULL)
        return transform->parent->angle - transform->angle;
    else
        return transform->angle;
}
void Transform_Update(DeadTransform *transform, GLfloat depth)
{
    float angle     = Transform_GetLocalAngle(transform);
    Vector2 position    = Transform_GetLocalPosition(transform);
    Vector2 scale       = Transform_GetLocalScale(transform);

    GLfloat data[16] = {(GLfloat)cosf(angle) * scale.x,     (GLfloat)(-sinf(angle)) * scale.y,  0.0f,   position.x / 1000,
                        (GLfloat)(sinf(angle)) * scale.x,   (GLfloat)cosf(angle) * scale.y,     0.0f,   position.y / 1000,
                        0.0f,                               0.0f,                               1.0f,   depth,
                        0.0f,                               0.0f,                               0,      1.0f};
    Matrix_SetData(transform->transformationMatrix, data);
}

This is where I create the road:

direction = Vector2_Subtract(*tile->top->transform->position, *tile->topLeft->transform->position);
            pos.x = tile->top->transform->position->x + direction.x / 2;
            pos.y = tile->top->transform->position->y - direction.y / 2;

            float angle = Vector2_Angle(*tile->top->transform->position, *tile->topLeft->transform->position);

            CreateConnection(application, pos.x, pos.y, angle);

void CreateConnection(struct Application *application, float x, float y, float angle)
{
    DeadGameObject *zone = GameObject_Create("Connection");
    zone->transform->position->x = x;
    zone->transform->position->y = y;
    zone->transform->angle = angle;
    zone->transform->scale->x = 64;
    zone->transform->scale->y = 6;
    DeadRenderer *renderer = Renderer_Create(Texture2D_Create("Images/Connection.png", GL_CLAMP, GL_LINEAR));
    Renderer_SetDepth(renderer, -2);
    GameObject_AddComponent(zone, renderer, Type_Renderer);

    Application_Instantiate(application, zone);
}
\$\endgroup\$
  • \$\begingroup\$ You're calculating the angle between the two vectors joining those two black crosses to the origin. Did you mean to calculate the angle between the vector joining the two crosses and a vector along the x axis? \$\endgroup\$ – DMGregory Dec 27 '18 at 0:00
  • \$\begingroup\$ I'll edit the question to be more precise \$\endgroup\$ – David Neves Dec 27 '18 at 0:02
  • \$\begingroup\$ I'm not sure What you mean... but could you give na exemple of your suggestion? \$\endgroup\$ – David Neves Dec 27 '18 at 0:15
  • \$\begingroup\$ it seems like the further away from the origin the angle error gets bigger. \$\endgroup\$ – David Neves Dec 27 '18 at 0:32
1
\$\begingroup\$

After some research I found the answer:

float Vector2_Angle(Vector2 a, Vector2 b)
{
    Vector2 c = Vector2_Subtract(a, b);

    return atan2f(c.y, -c.x);
}

this is the right formula.

\$\endgroup\$
  • 2
    \$\begingroup\$ Do you understand the reason this gives different results than what you were doing earlier, or are you still looking for answers that explain the error you were getting? \$\endgroup\$ – DMGregory Dec 27 '18 at 1:11

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.