Android Help - Smooth Directions

Good evening.

I've been tweaking some smooth movements with a virtual joystick for Android.

As is, the joystick allows you to control the direction of the player and works perfectly, but the character changes directions immediately.

I've managed to make the player change directions smoothly according to the same direction which the joystick.

The problem with the code I have made is that when you change from 0 degrees to 359 or from 359 to 0 the player makes a full circle the otherway around to the selected direction.
Hours spent around this and cannot make a solution, any help is greatly appreciated!

Code:
//Speed
    if (var_joystick_active = true && point_distance(x,y,device_mouse_x(temp_joystick),device_mouse_y(temp_joystick)) > radius_circ*var_screen_size)
    {
        if (instance_exists(obj_player))
        {
            image_angle = var_direction;
            if obj_player.image_angle < image_angle obj_player.image_angle += 10;
            if obj_player.image_angle > image_angle obj_player.image_angle -= 10;
            obj_player.turning = 1; //Player stop slithering
        }
    }
Thank you in advance!
 

samspade

Member
You might want to look up angle difference in the manual. That's probably the easiest solution. See this example from the manual:

Code:
var pd = point_direction(x, y, mouse_x, mouse_y);
var dd = angle_difference(image_angle, pd);
image_angle -= min(abs(dd), 10) * sign(dd);
Just substitute the variables you actually want in the above code and I think you would get what you want (a little hard to tell without knowing how your player actually moves).
 
Hello Sam.

Thank you for the prompt reply.

My player moves forwards wherever he is facing at a constant rate.

Code:
speed = 10;
I will look at this right now.
 
Sam, you are the best.

Code:
if image_angle != var_direction
                    {
                        var dd = angle_difference(image_angle, obj_joystick.var_direction);
                        image_angle -= min(abs(dd), 10) * sign(dd);
                    }
This solved it and it works perfectly and super smooth!

If I could I would like to buy you a cup of coffe, really! XD

Thank you so much.

:banana:
 
Last edited:
Top