Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - TooFux

Pages: [1]
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:

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
}

again please comment if you can :)

2
General / Re: joystick axis
« on: June 08, 2012, 12:46:22 am »
Thanks!

I've written this code.
Maybe someone could comment if he/she see something that could be improved.
Otherwise it will perhaps be usefull for someone else :-)

#include <math.h>

sfVector2f sfJoystick_normaliseDirectionProportional(float x, float y)
{
#define VECTOR_LENGTH(x, y) \
  sqrtf(((x) * (x)) + ((y) * (y)))

    float angle, in_len, max_len, max_x, max_y, ratio, norm_x, norm_y;
    sfVector2f res;
    if (x == 0.f && y == 0.f) {
        res.x = 0.f;
        res.y = 0.f;
        return res;
    }
    angle = atan2f(y, x);
    in_len = VECTOR_LENGTH(x, y);
    if (fabsf(x) > fabsf(y)) {
        max_x = 100.f;
        max_y = max_x * y / x;
    } else {
        max_y = 100.f;
        max_x = max_y * x / y;
    }
    max_len = VECTOR_LENGTH(max_x, max_y);
    ratio = in_len / max_len;
    norm_x = cosf(angle);
    norm_y = sinf(angle);
    res.x = norm_x * ratio;
    res.y = norm_y * ratio;
    return res;
#undef VECTOR_LENGTH
}

3
General / Re: joystick axis
« on: June 06, 2012, 02:44:29 pm »
No, this is not what I want to get.

Maybe I was not precise enough in my question.

When I said "constant speed", I mean that speed in horizontal/vertical should be the same than in diagonal, but in a lot of games if we push the axis just a little the avatar moves more slowly. I want this behavior.

for exemple:
- if the input is (100, 0) I want to get (1.0, 0.0)
- if the input is (20, 0) I want to get (0.2, 0.0)
- if the input is (100, 100) I want to get (cos (pi/4), sin (pi/4))  => (0.7071, 0.7071)
- if the input is (20, 20) I want to get (cos (pi/4), sin (pi/4)) / 5  =>  (0.14142, 0.14142)

4
General / joystick axis
« on: June 05, 2012, 09:37:55 pm »
Hi,

When the player moves the joystick axis, we get values between [-100, 100].

Right is (100, 0) and upper right is (100, 100).

My problem is that I would like to move at constant speed in all directions.
When I use this input the length in horizontal and vertical is 100 and the length in diagonal is ((sqrt 2) * 100).

How can I convert this square area into a circle area ?

Pages: [1]