Wrong angles given by atan2

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. 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;
}
{
float angle     = Transform_GetLocalAngle(transform);
Vector2 position    = Transform_GetLocalPosition(transform);
Vector2 scale       = Transform_GetLocalScale(transform);

GLfloat data = {(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)
{
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);

Application_Instantiate(application, zone);
}

• 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? – DMGregory Dec 27 '18 at 0:00
• I'll edit the question to be more precise – David Neves Dec 27 '18 at 0:02
• I'm not sure What you mean... but could you give na exemple of your suggestion? – David Neves Dec 27 '18 at 0:15
• it seems like the further away from the origin the angle error gets bigger. – David Neves Dec 27 '18 at 0:32

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.

• 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? – DMGregory Dec 27 '18 at 1:11