GM:S 1.4 Variable is 0, but sign(variable) is -1

Discussion in 'Programming' started by PWL, Oct 5, 2017.

  1. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    I wish I could use this for controlling my xSpeed, but strange things happen to it.
    The point is to slow down xSpeed to 0 eventually, no matter if it should decrease from 9 or increase from -7.
    Code:
    xSpeed = xSpeed + (-sign(xSpeed) * 0.01);
    However, sign(xSpeed) is not always 0 even though xSpeed validates to 0.
    Can someone tell me how this thing still shows a message?
    Code:
    if(xSpeed == 0 && sign(xSpeed) != 0){
        show_message(xSpeed); // Output: -0.00
    }
    
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,246
    Try absing the value
    Code:
    var xpsd = abs(xSpeed);
    if(xpsd == 0 && sign(xpsd) != 0){
        show_message(xSpeed);
    }
    
    Edit: shouldn't work
    Code:
    var num = min(xSpeed, 0.01);
    xSpeed -= num;
    
    This makes the xSpeed not switch between 0.00 and -0.01 every frame.
     
    Last edited: Oct 5, 2017
  3. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,353
    You could achieve a similar effect by just doing this:

    Code:
    xSpeed *= 0.99
    
    or

    Code:
    xSpeed = lerp(xSpeed, 0, 0.01)
    
     
  4. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Why do I need to abs a value that's is 0? That's the whole problem.

    I don't really like this approach as I want it to decrease by the same amount every time, in my example 0.01. Both of your examples uses a percentage of the current xSpeed-value and this will also result in xSpeed not ever reaching 0, but over time like 0.0000001.
     
  5. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,246
    Ignore the upper code.
    This should make it reduce to 0 exactly
    Code:
    var num = min(xSpeed, 0.01); // if xSpeed is 0, xSpeed will be reduced by 0.
    xSpeed -= num;
    
    http://yal.cc/r/gml/?mode=&lzgml=MQ...SeAIqAAawQeHIUCkaCeLzeZ18fPgdm6TjAMDw3CGuIwQA
     
  6. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,680
    To make that one line and work with negative values. ;)
    Code:
    xSpeed -= clamp(xSpeed, -0.01, 0.01);
     
    IndianaBones likes this.
  7. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    Yes, that is absolutely how I'd do it if it wasn't going to reduce to 0 from both negative and positive values.

    Code:
    xSpeed = max(0, xSpeed - 0.1); // So why does this work
    xSpeed += -sign(xSpeed) * 0.1; // While this flicker between 0.00 and -0.10?
     
  8. Bingdom

    Bingdom Googledom

    Joined:
    Jul 1, 2016
    Posts:
    1,680
    How are you modifying xSpeed?
    Maybe there's a very tiny value.

    Are you using HTML? For HTML, values aren't always exact and you'll need to use floor() a lot for comparisons.
    See this (scroll to bottom)

    The code I just provided does reduce to 0 for both negative and positive values. ;)

    First line grabs the highest value. The second argument could equal -0.00001
     
    Last edited: Oct 5, 2017

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