GM:S 1.4 How to change direction for a physics object?

Discussion in 'Programming' started by Bhreno, Oct 12, 2019 at 9:38 PM.

  1. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    17
    Hello. I would like to know how do I change the direction of a physical object in GMS1.4 without using the phy_speed_x & phy_speed_y variables (which are already being used to a previous random value). It's possible?
    For example, the object to be created may follow any direction, however its speed has already been set by phy_speed_x & phy_speed_y, and I wanted to change just his direction, without changing the speed.
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,166
    Do you mean that you want to instantaneously change to a different direction, but keep the same speed?

    Apparently, there's no built-in variable that measures physics speed in pixels per step, so you'll need to measure it yourself. sqrt(phy_speed_x*phy_speed_x + phy_speed_y*phy_speed_y)

    And if you have an angle you want to move in, then you can use lengthdir_x and lengthdir_y to set new phy_speed_x and phy_speed_y values using that angle, and the speed measured above.

    What's with the weird picture uploads?
     
    Bhreno likes this.
  3. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    17
    I will explain further:

    My obj_player should have random speed and direction every time it is created.
    speed_player = irandom(7); 8 different speeds;
    direction_player = irandom(360); all possible different directions;

    The speed I could set with phy_speed_x & phy_speed_y, and it worked perfectly.

    The direction, whenever I try to set the random value, it ends up interfering with the speed and the object goes crazy through the room.
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,547
    How exactly did you set phy_speed_x and phy_speed_y? If you did it properly, both the speed and the direction should work straight out of the box.
    Code:
    speed_player = irandom(7);
    direction_player = irandom(360);
    phy_speed_x = lengthdir_x(speed_player, direction_player);
    phy_speed_y = lengthdir_y(speed_player, direction_player);
    
     
    Bhreno likes this.
  5. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    17
    I set the speed variables as follows:

    In Create Event:

    Code:
    speed_ball = irandom(4);
    
    switch (speed_ball)
    {
    case 0: speed_ball = 3; break;
    case 1: speed_ball = 4; break;
    case 2: speed_ball = 5; break;
    case 3: speed_ball = 6; break;
    case 4: speed_ball = 7; break;
    }
    
    
    phy_speed_x = speed_ball;
    phy_speed_y = speed_ball;
    
    In Step Event:

    Code:
    //speed min x & y
    if (phy_speed_x) > (-2) && (phy_speed_x) < 2 && (phy_speed_y) > (-2) && (phy_speed_y) < 2
    {
    if (phy_speed_x) < 0
    {
    phy_speed_x = -2;
    }
    else
    if (phy_speed_x) > 0 or (phy_speed_x) == 0
    {
    phy_speed_x = 2;
    }
    else
    if (phy_speed_y) < 0
    {
    phy_speed_y = -2;
    }
    else
    if (phy_speed_y) > 0 or (phy_speed_y) == 0
    {
    phy_speed_y = 2;
    }
    }
    
    else
    //speed max x & y
    if (phy_speed_x) < (-10) && (phy_speed_x) > 10 && (phy_speed_y) < (-10) && (phy_speed_y) > 10
    {
    if (phy_speed_x) < (-10)
    {
    phy_speed_x = -10;
    }
    else
    if (phy_speed_x) > 10
    {
    phy_speed_x = 10;
    }
    else
    if (phy_speed_y) < (-10)
    {
    phy_speed_y = -10;
    }
    else
    if (phy_speed_y) > 10
    {
    phy_speed_y = 10;
    }
    }
     
  6. Bhreno

    Bhreno Member

    Joined:
    Oct 5, 2019
    Posts:
    17
    If I use this code, the speed goes out of control.
     
    Last edited: Oct 13, 2019 at 2:57 PM

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice