-=
in javascript. does that change the value itself?
You are not setting e anywhere in the comilation and value itself cannot be changed
(minus doesn’t update but just returns the answer)
Yep it does. Don’t get me wrong, the numbers are right, but by wrong separation. I try to convert them right now. Maybe it works
Not the best solution, but good for now
Thank you for your attention
I have to correct myself. e isn’t chaning in JS either
I guess it’s the operators that don’t really mach to kotlin. And i don’t know the equivalent operators to kotlin and they are nowhere to find.
//This is pure Java for that
public Float calculate(float t, float b, float c, float d) {
if ((t/=d) < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
} else {
return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
}
}
Kotlin don’t let me use (t/=d) nor t-=
And if i write it that way, that kotlin don’t refuse to calculate, only the first to numbers are correct. Add the end it resulst in 7490411.0 at 100
Is there a math geek in this forum who can me explain why?
That is the working (not really) kotlinversion of it with the 7490411.0 at the end
fun bounceEaseOut(t:Float, b:Float=0f, c:Float=100f, d:Float=100f):Float {
if ((t/d) < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
return c*(7.5625f*(t-(1.5f/2.75f))*t + .75f) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625f*(t-(2.25f/2.75f))*t + .9375f) + b;
} else {
return c*(7.5625f*(t-(2.625f/2.75f))*t + .984375f) + b;
}
}
i though ```
(t/=d) is just t/d or t.div(d). Apperantly it’s not. Because if i compare the numbers results from Javascript and Kotlin, they are different (0.1 and 0.01) and worse
I also though e-= is e - but it’s not. Number don’t match at all
Both in kotlin and in java t /= d
is the same as t = t / d
. The difference is that in kotlin /=
is a statement and not an expression. This means that you can’t use /=
within another expression.
t /= d // this is allowed and is equal to t = t / d
a + (t /= d) // this is not allowed, because /= is a statement and not an expression
In java the second line is valid (I think) but in any case it’s considered bad practise in most situations.
t / d
is equal to t.div(t)
. Both only calculate the result and return it, but don’t modify the original value.
From what I can tell your code should look something like
fun bounceEaseOut(t:Float, b:Float=0f, c:Float=100f, d:Float=100f):Float {
t /= d
if (t < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
return c*(7.5625f*(t-(1.5f/2.75f))*t + .75f) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625f*(t-(2.25f/2.75f))*t + .9375f) + b;
} else {
return c*(7.5625f*(t-(2.625f/2.75f))*t + .984375f) + b;
}
}
At least if you fix the issue you have with t /= d
. I have no idea if the rest is correct since I don’t really know what you want to calculate.
Thank you but nope. Same results. Nothing has changed.
And it makes sence now (e, or t now) is changing as well. I know i said it’s not changing
But t -= reduces the value of t. And that isn’t happening, because t -= is not allowed in kottlin.
Edit: Sorry, your code indead worked better. I removed the line t/=d because it gave me an error and i thought you meant it as a comment
fun bounceEaseOut(dt:Float, b:Float=0f, c:Float=100f, d:Float=100f):Float {
var t=dt //<- had to change iot that way
t /= d
if (t < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
return c*(7.5625f*(t-(1.5f/2.75f))*t + .75f) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625f*(t-(2.25f/2.75f))*t + .9375f) + b;
} else {
return c*(7.5625f*(t-(2.625f/2.75f))*t + .984375f) + b;
}
}
Doing better, but not really easeing. More of a flickering. And still ends with more than 100
Any other solutions
For better understanding
bounceEaseOut is meant to be as it’s name says, Bounce
So percentage should be returned the same way you drop a ball. Fall to the ground, bounces back, to the ground bounces back a bitt less and so on, till it’s rest on the ground (100%)
The code now (with your help) does this
To the ground, start from the half way (no bounce back) and falls, than again a bit back(without bounce back) and so on. That’s what i meant by flickering
And again. Im pretty sure it’s because t is never chaning it’s value
Example Line:
```
return c*(7.5625f*(t-(1.5f/2.75f))*t + .75f) + b;
```
t is never changing it’s value.
Should be
```
return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
```
which isn’t allowed
t must reduce it’s value and than multiply with the new value of t
*t + .75f) + b;
And that is way there is no real bouncing
GOT IT
fun bounceEaseOut(dt:Float, b:Float=0f, c:Float=100f, d:Float=100f):Float {
var t=dt
t /= d
if (t < (1/2.75f)) {
return c*(7.5625f*t*t) + b
} else if (t < (2/2.75f)) {
var dt=t-(1.5f/2.75f)
return c*(7.5625f*(dt)*dt + .75f) + b
} else if (t < (2.5/2.75)) {
var dt=t-(2.25f/2.75f)
return c*(7.5625f*(dt)*dt + .9375f) + b
} else {
var dt=t-(2.625f/2.75f)
return c*(7.5625f*(dt)*dt + .984375f) + b
}
}
Thank you to all of you, especially @Wasabi375
When im done with all my easing i will post my functions in this thread for those who care
Just a small nitpick, but you are shadowing the already-existent dt
parameter. Instead, you can just reuse the t
variable, which will turn this line into t -= (1.5f/2.75f)
Additionally, there are some Kotlin idioms that can be applied here to make this code so much cleaner, but like obviously most people wouldn’t necessarily think of using them especially for a small function like this one. For the educational value, though, here’s the same code but more Kotlin-y:
fun bounceEaseOut(t: Float, b: Float = 0f, c: Float = 100f, d: Float = 100f): Float {
// It's idiomatic to shadow a parameter to turn it into a var if that is needed by your code
var t = t / d
// b gets added to each value in the end, and so it can be moved to the start of the when expression. The when now is only responsible for calculating the multiplier for c, which reduces duplication and the possibility that later on someone might make a mistake and change the return value of a branch
return b + c * when {
t < 1 / 2.75f -> 7.5625f * t * t
t < 2 / 2.75f -> {
t -= 1.5f / 2.75f
7.5625f * t * t + .75f
}
t < 2.5f / 2.75f -> {
t -= 2.25f / 2.75f
7.5625f * t * t + .9375f
}
else -> {
t -= 2.625f / 2.75f
7.5625f * t * t + .984375f
}
}
}
Also, I’d say that the when expression could probably be moved to its own separate val multiplier
because of its size, but this should also be fine for now.
Nope. I’ve tried that in before because of my original function from Javascript.
Kotlin returned me an error while trying to redefine or change this value
So that was a workarround
It should only give you an error if you are using that assignment as part of an expression. I meant that you can replace the dt = t - ...
part with just t -= ...
on its own separate line, basically in the same manner that the code in my reply uses it as.
That’s exactly the error “assignment and expression” when i do t-=
Even when it’s on its own separate line? Ok one second, I’ll make a demo and see what will happen because if so then something else must be wrong
Ahh, got you. I was reading to fast. No, on seperate line it should work, your right
d /=r on a seperate line is working too
Thank you. I don’t know why i have not came up on myself with it
Yeah I know exactly what you mean. Happens a lot to me tbh. Regardless, I made a demo and I think the results seem to be what you wanted. For these calls:
println(bounceEaseOut(100f))
println(bounceEaseOut(0f))
println(bounceEaseOut(50f))
println(bounceEaseOut(25f))
It prints:
100.0
0.0
76.5625
47.265625
So I think that the t -=
trick seems to be working