GMS 2 Object rotation with accelleration

Lakko

Member
Hello,
I'm working on a new game on android.
I have an object (obj_player) that always rotate around a point (the middle of the room). At the start of the room obj_player have to rotate clockwise.
Code:
CREATE EVENT
image_angle = 0;
r_speed = 2;
r_speed_old = 2;
image_xscale = 1;
image_yscale = 1;

STEP EVENT
image_angle += r_speed;
in any moment when i tap the screen and i hold finger on screen the obj_player have to start rotate in the opposite direction at an higher speed (r_speed = 20) with a speed accelleration; when i release the finger it as to rotate in the same direction but slower (r_speed = 2) with a decelleraion.
When obj_player change direction i also whant to flip the sprite according with the direction.

I've made a code that works pretty good but without the speed accelleration and decelleration:
Code:
GLOBAL MOUSE LEFT PRESSED EVENT
switch (r_speed)
{
     case 2:
          r_speed = -20;
          image_yscale = image_yscale*-1;
          break;
     case -2:
          r_speed = 20;
          image_yscale = image_yscale*-1;
          break;    
}

GLOBAL MOUSE LEFT RELEASED EVENT
switch (r_speed)
{
     case -20:
          r_speed = -2;
          break;
     case 20:
          r_speed = 2;
          break;    
}
Have you any idea on how to add the accelleration and decelleration?
 

Paskaler

Member
Add another variable as a middle man. Add to that variable the rotation speed and then that to the r_speed.

r_speed += acceleration

For slowing down use the lerp function

r_speed = lerp(r_speed, 0, deacceleration)

You'd have to put bounds as well on the r_speed else it will accelerate to infinity.
 

Lakko

Member
I've solved using this code, any chanche to improve the code?

Code:
CREATE EVENT
clicked = 0;
rotation_direction = 1; //1 clockwise  -1 anticlockwise
acceleration = 2;
image_angle = 0;
r_speed = 2;
image_xscale = 1;
image_yscale = 1;
Code:
STEP EVENT
image_angle += r_speed;
if clicked == 1
{
    switch (rotation_direction)
    {
        case 1:
            if r_speed <= 12
            {
                r_speed += acceleration;
            }
            else if r_speed >=14
            {
                r_speed = 14;
            }
            break;

            case -1:
                if r_speed >= -12
                {
                    r_speed -= acceleration;
                }
                else if r_speed <=-14
                {
                    r_speed = -14;
                }
            break;
    }
    break;
}
else
{
    exit;
}

Code:
GLOBAL PRESS
if clicked == 0
{
    image_yscale = image_yscale*-1;
  
    switch (rotation_direction)
    {
        case 1:
            rotation_direction = -1;
            break;
        case -1:
            rotation_direction = 1;
            break;
    }
}
else
{
    exit;
}
clicked = 1;

Code:
GLOBAL RELEASE
if clicked == 1
{
    clicked  = 0;
    if rotation_direction == -1
    {
        r_speed = -2;
    }
    if rotation_direction == 1
    {
        r_speed = 2;
    }
}
else
{
    exit;
}
 
Top