1
General / Re: joystick axis
« on: June 08, 2012, 12:05:56 pm »
Hi,
It seems that the previous code can be simpilfied, this version seems to produce the same results without needing to calculate the angle. Here we save calling atan2, cos and sin:
again please comment if you can
It seems that the previous code can be simpilfied, this version seems to produce the same results without needing to calculate the angle. Here we save calling atan2, cos and sin:
sfVector2f
sfJoystick_normaliseDirectionProportional(float x, float y)
{
#define VECTOR_LENGTH(x, y) \
sqrtf(((x) * (x)) + ((y) * (y)))
float in_len, ratio;
sfVector2f res;
if (x == 0.f && y == 0.f) {
res.x = 0.f;
res.y = 0.f;
return res;
}
in_len = VECTOR_LENGTH(x, y);
if (fabsf(x) > fabsf(y))
ratio = fabsf(x / 100.f) / in_len;
else
ratio = fabsf(y / 100.f) / in_len;
res.x = x * ratio;
res.y = y * ratio;
return res;
#undef VECTOR_LENGTH
}
sfJoystick_normaliseDirectionProportional(float x, float y)
{
#define VECTOR_LENGTH(x, y) \
sqrtf(((x) * (x)) + ((y) * (y)))
float in_len, ratio;
sfVector2f res;
if (x == 0.f && y == 0.f) {
res.x = 0.f;
res.y = 0.f;
return res;
}
in_len = VECTOR_LENGTH(x, y);
if (fabsf(x) > fabsf(y))
ratio = fabsf(x / 100.f) / in_len;
else
ratio = fabsf(y / 100.f) / in_len;
res.x = x * ratio;
res.y = y * ratio;
return res;
#undef VECTOR_LENGTH
}
again please comment if you can