1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GML Replacement for built in speed functions?

Discussion in 'Programming' started by Trace, Dec 22, 2019.

  1. Trace

    Trace Member

    Joined:
    Jan 14, 2019
    Posts:
    2
    So I'm trying to make some sort of F-Zero like racing game, but I'm using the built in speed variables and functions because I don't know how to achieve what i want with custom variables, which is already bad because the built in speed variables are way too jank.
    The other problem with them is that i have to main states. A normal one for normal driving and steering, and another for drifting. The main difference between the two is that the normal driving one manually sets speed and direction, while the drifting state uses motion_add to be more slippery and more like drifting.
    This is where the problem comes in: Since the normal driving state manually changes direction, momentum is lost in a very unnatural way when switching states, and i also cant apply motion to the sides of the player when in the normal driving state.
    So what i need is some way to replicate what i have now, except with custom variables and in a way that allows me to do more natural movement while still keeping the consistent steering of the normal driving state.
    Additional Information:
    -Version is GM:S 1.4.9999
    Code for the regular driving state:
    Code:
    scrInputs(); //Nothing more than button inputs
    scrMovement(); //Mostly just the physics for turning
    
    //Turning
    topturn = normalturn; //This is here because the max angle at which you can turn is different for the states
    direction += angle_difference(angle_z,direction)/32; //A placeholder solution for switching from drifting to normal; very bad solution and very jank
    
    //Accelerating
    if (kGas){
        speed += (topspd-speed)/accel;
    }  
    if (speed > topspd) speed = topspd;
    
    //Bounce
    if (kBounce and turndir != 0){
        motion_add(angle_z+90*turndir,6); //This i used to test getting pushed to the side in the normal state
    }  
    
    //friction
    if (!kGas){
       friction=fric; //friction is only there when the gas button isnt pressed for consistant movement; id need some way to add friction into the side directions in this state
    }
    else{
       friction=0;
    }
    if (turnspd != 0 and kDrift) state = states.drift;
    
    Code for drifting (most of this is similar to the previous, but with the different phyiscs):
    Code:
    scrInputs();
    scrMovement();
    
    //Turning
    topturn = driftspd;
    
    //Accelerating
    if (kGas){
        motion_add(angle_z,turnaccel)
        if (speed > topspd) speed = topspd;
    }
    
    //friction
    if (abs(speed) > 0){
       friction=fric;
    }
    else{
       friction=0;
    }
    
    if (turnspd = 0 or !kDrift) state = states.normal;
    
    I think it's good if i learn how to do this kind of stuff with custom speed variables, so help would be really appreciated. If there's anything i haven't said that you would need to help me on this, please tell me.
     
    Last edited: Dec 22, 2019
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,434
    motion_add is just

    hspd += cos(angle_z)*turn_accel;
    vspd -= sin(angle_z)*turn_accel;

    Can't give more trig lessons cuz i i tta get to work.
     
  3. Trace

    Trace Member

    Joined:
    Jan 14, 2019
    Posts:
    2
    Alright, i managed to replicate the normal state with custom numbers. What's still a problem though is the friction for the drifting.
     

Share This Page