heinrich5991 / libtw2

Some Teeworlds stuff in Rust.™
Apache License 2.0
49 stars 17 forks source link

Hook is weaker while hooking to left #89

Open gerdoe-jr opened 8 months ago

gerdoe-jr commented 8 months ago

https://github.com/heinrich5991/libtw2/blob/b9286674da94d3d45b9c10ffce517af394e2d58c/world/src/lib.rs#L737-L748

After changing this code to next given hook problem disappears though

pub fn saturated_add(min: f32, max: f32, value: f32, modifier: f32) -> f32 {
    if modifier < 0.0 {
        if value < min {
            return value;
        } else {
            return min.max(value + modifier);
        }
    } else {
        if value > max {
            return value;
        } else {
            return max.min(value + modifier);
        }
    }
}
heinrich5991 commented 8 months ago

I don't understand what's different about the new code. Can you give example values where they differ?

gerdoe-jr commented 8 months ago

I have no example values, but gameplay videos, you can see the difference

https://github.com/heinrich5991/libtw2/assets/51330274/ef04f236-0993-4d6b-80f0-d35b419cf066

https://github.com/heinrich5991/libtw2/assets/51330274/0e9e3913-4552-48b2-9e62-695c22485d71

heinrich5991 commented 8 months ago

If you can generate these examples, perhaps you could run both functions and issue a println! statement when their result differs?

Patiga commented 7 months ago

I was curious about the difference between those two functions as well. I created a small playground project to get value mismatches. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=708e855ce0717c7690ccb6db3994cdb3

heinrich5991 commented 7 months ago

The difference happens when the updated value is still outside the range, but closer to the accepted range:

E.g. range -1..0, value 2, modifier -1. The old code outputted 0 (a bug, I think), the new code outputs 1. Thanks for the clear reproduction example, @Patiga!

@gerdoe-jr Can you create a pull request? :)