• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Accel and Decel physics

Erayd

Member
I created code for a platform to get faster each update until halfway and then slow down, but its acting strange. In one direction is speeds up and then only gets two updates to slow down, then the other direction gets 6 or 7 slow down frames.

This is the physics code for the platform, what am I missing or doing wrong?
Code:
//Accelerate faster each step up to 50% of the way then decellerate
    if(stepCount >= update){
        if((rangeX > 0 && phy_com_x < startX + (rangeX/2)) || (rangeX < 0 && phy_com_x > startX + (rangeX/2))){ //X axis
            if(rangeX > 0 && !readyX) physics_apply_force(phy_com_x, phy_com_y, phy_mass*(pushX + diffX), 0);
            if(rangeX < 0 && !readyX) physics_apply_force(phy_com_x, phy_com_y, phy_mass*-(pushX + diffX), 0);
            if(diffX + accelX < terminalSpeed) diffX += accelX;
            else diffX = terminalSpeed;
        }
        if((rangeY > 0 && phy_com_y < startY + (rangeY/2)) || (rangeY < 0 && phy_com_y > startY + (rangeY/2))){ //Y axis
            if(rangeY > 0 && !readyY) physics_apply_force(phy_com_x, phy_com_y, 0, phy_mass*(pushY + diffY));
            if(rangeY < 0 && !readyY) physics_apply_force(phy_com_x, phy_com_y, 0, phy_mass*-(pushY + diffY));
            if(diffY + accelY < terminalSpeed) diffY += accelY;
            else diffY = terminalSpeed;
        }
        //Decelerate after reaching the halfway point
        if((rangeX > 0 && phy_com_x >= startX + (rangeX/2)) || (rangeX < 0 && phy_com_x <= startX + (rangeX/2))){ //X axis
            show_debug_message(string(phy_speed_x));
            if(rangeX > 0 && !readyX) physics_apply_force(phy_com_x, phy_com_y, phy_mass*-(pushX + diffX), 0);
            if(rangeX < 0 && !readyX) physics_apply_force(phy_com_x, phy_com_y, phy_mass*(pushX + diffX), 0);
            if(diffX - decelX > -terminalSpeed) diffX -= decelX;
            else diffX = -terminalSpeed;
        }
        if((rangeY > 0 && phy_com_y >= startY + (rangeY/2)) || (rangeY < 0 && phy_com_y <= startY + (rangeY/2))){ //Y axis
            if(rangeY > 0 && !readyY) physics_apply_force(phy_com_x, phy_com_y, 0, phy_mass*-(pushY + diffY));
            if(rangeY < 0 && !readyY) physics_apply_force(phy_com_x, phy_com_y, 0, phy_mass*(pushY + diffY));
            if(diffY - decelY > -terminalSpeed) diffY -= decelY;
            else diffY = -terminalSpeed;
        }
        stepCount = 0;
    }

//Changing states
    if((rangeX > 0 && phy_com_x > startX + rangeX) || (rangeX < 0 && phy_com_x < startX + rangeX) || (rangeX == 0)) readyX = true;
    if((rangeY > 0 && phy_com_y > startY + rangeY) || (rangeY < 0 && phy_com_y < startY + rangeY) || (rangeY == 0)) readyY = true;
    if(readyX) phy_speed_x = 0;
    if(readyY) phy_speed_y = 0;
    if(((obj_Player.platform == id) && (transX && transY)) || ((obj_Player.platform != id) && (readyX && readyY))) changeDir = true;
    if(changeDir){
        readyX = false;
        readyY = false;
        diffX = 0;
        diffY = 0;
        startX = phy_com_x;
        startY = phy_com_y;
        rangeX = -rangeX;
        rangeY = -rangeY;
        changeDir = false;
        transX = false;
        transY = false;
    }
Creation code
Code:
///Vars
phy_fixed_rotation = true;

changeDir = false;
startX = phy_com_x;
startY = phy_com_y;
readyX = false;
transX = false;
readyY = false;
transY = false;
diffX = 0;
diffY = 0;
dropAway = false;
oldXSpeed = 0;
oldYSpeed = 0;
stepCount = 0;

//Customizable variables
//Starting push
pushX = 30;
pushY = 30;

//Acceleration
accelX = 30;
accelY = 30;
decelX = 30;
decelY = 30;

//Range
rangeX = 60;
rangeY = 0;

//Top Speed
terminalSpeed = 70;

//Step count to update speeds on (Smaller equals faster
update = 10;
 
Top