#issue with my code

1 messages · Page 1 of 1 (latest)

still delta
#

dash_state = function(){

if (vel_x > 0) vel_x = -.2;
if (vel_y > 0) vel_y = -.2;    

if(input_check("dash")){
    
    if (!instance_exists(Obj_dash_circle) && !instance_exists(Obj_dash_arrow)){
        
        var _dir;
        
        var _circle = instance_create_depth(x, y - 12, -1, Obj_dash_circle);
        
        var _arrow = instance_create_depth(_circle.x, _circle.y, 0, Obj_dash_arrow);
        
        if (gamepad_is_connected(0)){
            _dir = point_direction(0, 0, gamepad_axis_value(0, gp_axislh), gamepad_axis_value(0, gp_axislv));
        }
        else{
            _dir = point_direction(_circle.x, _circle.y, mouse_x, mouse_y);
        }
        
        var _x = _circle.x + lengthdir_x(_circle.sprite_width/2, _dir);
        var _y = _circle.y +lengthdir_y(_circle.sprite_height/2, _dir);
        
        _circle.x = x;
        _circle.y = y - 12;
        _arrow.image_angle = _dir;
        _arrow.x = _x;
        _arrow.y = _y;
    }
}
else if (input_check_released("dash")){
    
    var _dir;
    
    var _circle = Obj_dash_circle;
    
    if (gamepad_is_connected(0)){
        _dir = point_direction(0, 0, gamepad_axis_value(0, gp_axislh), gamepad_axis_value(0, gp_axislv));
    }
    else{
        _dir = point_direction(_circle.x, _circle.y, mouse_x, mouse_y);
    }
    
    instance_destroy(Obj_dash_circle);
    instance_destroy(Obj_dash_arrow);
    
    vel_x = 0;
    vel_y = 0;
    can_move = 1.5;
    
    dash_number--;
    
    vel_x = lengthdir_x(dash_spd * sign(_dir), _dir);
    vel_y = lengthdir_y(dash_spd * sign(_dir), _dir);
    
    state = free_moviment;
}

}

umbral sentinel
#

So, consider this example.

#

Ignore the random letters, mario moves and then his speed decays to 0.

#

The movement is powered by some simple code

#
if ( _move == 0 || abs( hSpeed ) > _max )
    hSpeed -= ( abs( hSpeed ) < 0.1 ) ? hSpeed : 0.1 * sign( hSpeed );
else if ( _move != 0 )
    hSpeed    = clamp( hSpeed + sign( _move ) * 0.33, -_max, _max );
#

The important part is this, the first line:

#
if ( _move == 0 || abs( hSpeed ) > _max )
#

If _move is 0 or Mario's speed is higher than his max, decay.

#

In your code you rely on the button press to decide whether or not to decay.

#

In fact, your code relies entirely on dash being pressed

#
    if(input_check("dash")){
#

If the player doesn't press dash, none of this code runs, and that includes the code that is supposed to decay your speed.

still delta
#

so what i can do to fix?

umbral sentinel
#

It seems like you should be able to fix it just by getting rid of that last part.

#

In this case, when dash isn't pressed, you run the second part of the code.

#

I'm honestly not quite sure because it's all a bit confusing. You have a lot of things in this pot.

#
        var _dir;

        var _circle = Obj_dash_circle;

        if (gamepad_is_connected(0)){
            _dir = point_direction(0, 0, gamepad_axis_value(0, gp_axislh), gamepad_axis_value(0, gp_axislv));
        }
        else{
            _dir = point_direction(_circle.x, _circle.y, mouse_x, mouse_y);
        }

        instance_destroy(Obj_dash_circle);
        instance_destroy(Obj_dash_arrow);

        vel_x = 0;
        vel_y = 0;
        can_move = 1.5;

        dash_number--;

        vel_x = lengthdir_x(dash_spd * sign(_dir), _dir);
        vel_y = lengthdir_y(dash_spd * sign(_dir), _dir);

        state = free_moviment;
#

Like here, you are running code that should happen when you release.

#

But you also set your vel_ to 0, then change your dash number, then set your vel_ to another number, then change your state.

#

And it's like, what is this code supposed to do?

#

Clean up after the dash?

#

Decay your speed?

#

It's tricky to figure out exactly what has gone wrong because it's hard to tell what this code is supposed to do.

still delta
#

okay, let me explain

#

there, when the code enters in dash state

#

it's create two instances, the circle and the arrow, with it the player stop and it's cant move, so with it the player points with the mouse or joystick where he wants to dash to. when he holds the dash input he keeps there choosing, but when he realise the input, the player dash to the direction where the arrow is pointing to and he got into the free moviment state again

umbral sentinel
#

Ah. Then this code would have nothing to do with the movement.

#

Well, beyond the initial impulse to send them flying.

#

So your issue is this then

#
    if (vel_x > 0) vel_x = -.2;
    if (vel_y > 0) vel_y = -.2;
#

If this is meant to decay your speed it has a few issues.

#

First off, it would only work when moving right or down, since > 0 is down and right are positive values.

#

Secondly, it would also only work if the velocity was a multiple of 0.2, since if you reach 0.1 and then subtracted 0.2 you'd end up with -0.1 which would then cause you to move up and/or left.

still delta
#

i also tried != 0 but didn't work

umbral sentinel
#

What we want is something more like this:

still delta
#

as well lerp and approach

#

okay, sorry continue

umbral sentinel
#
if ( vel_x != 0 ) {
  if ( abs( vel_x ) < 0.2 )
    vel_x = 0;
  else
    vel_x -= sign( vel_x ) * 0.2;
}
#

Woof, a bit more complex, eh?

#

But let's at least consider what it's doing:

#

First we say if we are moving non-zero, so left or right in this case. Then we check if the absolute value of our speed is less than 0.2. If it is we just set our speed to 0, this makes sure it will always come to rest at 0.

#

Lastly, we subtract the sign of our velocity times the speed of our decay, 0.2, so we'll either add or subtract based on which direction we were moving.

still delta
#

ooooh nonono sorry

#

i want to slowly reduce the values to 0

#

like, the .2 was supposed to be how much it's removes until becomes 0

umbral sentinel
#

Exactly.

#

That's what we'd be doing here, we just solve for both problems above: it works whether we were moving in a positive or negative direction, and it ensures we reach 0.

#

If you look closely, you'll see this approach is used in my Mario example above:

#
hSpeed -= ( abs( hSpeed ) < 0.1 ) ? hSpeed : 0.1 * sign( hSpeed );
#

Yes, it's more compact, but check those overlaps: the absolute speed is less than 0.1 we subtract hSpeed (which results in 0) and if not, we decay towards zero by using the sign of our speed times our decay.

#

And what does that result in? Mario's speed always decays towards 0.

#

This is just a more expanded version

#
if ( vel_x != 0 ) {
  if ( abs( vel_x ) < 0.2 )
    vel_x = 0;
  else
    vel_x -= sign( vel_x ) * 0.2;
}```
#

But it does the same thing: if our speed is not zero, we check if it's less than our decay speed. If so, set it to 0. Otherwise subtract our decay speed from our velocity. This causes our speed to move each frame towards 0.

#

You need to do it to both x and y, but the idea is the same on both axis ... sort of.

#

Gravity is gravity but I'm not covering that here.

still delta
#

so what

#

wait*

#

in my moviment code, i have this code ```gml if (input_check("left") || input_check("right")) {
move_dir = input_check_opposing("left", "right");
move_spd = approach(move_spd, spd_max, .2)
}
else{move_spd = approach(move_spd, spd_min, .2)}

#

its like, my attempt do a momentum code

#

momentum/acceleration

#

if i put that code you share as well inside the else, it's will works?

#

just asking

umbral sentinel
#

I'm guessing that approach does much what I've described above so it's hard to say.

#

I'd need to see what that function does.

still delta
#

wait

#

/// @function approach(_a, _b, _amount);
/// @description approach(_a, _b, _amount)
/// @param {real} _a Start point
/// @param {real} _b End point
/// @param {real} _amount How fast it's goes

function approach(_a, _b, _amount)
{
    if (argument0 < argument1)
    {
        argument0 += argument2;
        if (argument0 > argument1)
            return argument1;
    }
    else
    {
        argument0 -= argument2;
        if (argument0 < argument1)
            return argument1;
    }
    return argument0;
}
#

here

umbral sentinel
#

Based on what this says, if you wanted to decay your speed to 0, the end point needs to be 0.

#
else{move_spd = approach(move_spd, 0, .2)}
#

I'm fairly sure that would work.

still delta
#

okay

#

so back to the original topic

#

i just replace that old code with this new one

#

like this

umbral sentinel
#

Keep in mind this is only operating on vel_x.

#

If you want to decay vel_y you have to do it to that as well.

#

Computers don't guess, and they don't infer. They just follow instructions. If you don't tell it to do something, it won't.

#

As you already have this approach function we could leverage that work to make it simpler.

still delta
#

so i just need to adapt into vel_y as well

umbral sentinel
#
vel_x = approach( vel_x, 0, 0.2 );
vel_y = approach( vel_y, 0, 0.2 );
#

Since that function is doing what the code I suggested does, but as a function. Which is why functions are nice: we take general code and use it for specific purposes.

still delta
#

...

#

i can't believe i got streed, panic, anxiety and stuff

#

with something i already had the soluction

#

qwp

#

thanks anyway. and sorry for anything

umbral sentinel
#

You are welcome.