Legacy GM Variable is 0, but sign(variable) is -1

P

PWL

Guest
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
}
 

Simon Gust

Member
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:
You could achieve a similar effect by just doing this:

Code:
xSpeed *= 0.99
or

Code:
xSpeed = lerp(xSpeed, 0, 0.01)
 
P

PWL

Guest
Why do I need to abs a value that's is 0? That's the whole problem.

You could achieve a similar effect by just doing this:

Code:
xSpeed *= 0.99
or

Code:
xSpeed = lerp(xSpeed, 0, 0.01)
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.
 

Simon Gust

Member
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.
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
 
P

PWL

Guest
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?
 

Bingdom

Googledom
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?
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:
Top