[libadwaita/wip/exalm/spring-animation-swipes: 24/36] fix more math! now it works correctly!




commit f75de41618d5d142c692faf39b2f0bc99d0b0231
Author: Manuel Genovés <manuel genoves gmail com>
Date:   Sat Dec 4 21:46:31 2021 +0100

    fix more math! now it works correctly!

 src/adw-spring-animation.c | 35 +++++++++++++----------------------
 1 file changed, 13 insertions(+), 22 deletions(-)
---
diff --git a/src/adw-spring-animation.c b/src/adw-spring-animation.c
index 717d16f8..9e2c99c5 100644
--- a/src/adw-spring-animation.c
+++ b/src/adw-spring-animation.c
@@ -59,7 +59,6 @@ static GParamSpec *props[LAST_PROP];
  */
 static double
 oscillate (AdwAnimation *animation,
-           double        offset,
            guint         time)
 {
   AdwSpringAnimation *self = ADW_SPRING_ANIMATION (animation);
@@ -74,14 +73,10 @@ oscillate (AdwAnimation *animation,
   double beta = b / (2 * m);
   double omega0 = sqrt (k / m);
 
-  double x0 = offset;
+  double x0 = self->value_from - self->value_to;
 
   double envelope = exp (-beta * t);
 
-  /* Transformation so the velocity is in the same units as value_from and value_to */
-  if (!G_APPROX_VALUE (self->value_from, self->value_to, FLT_EPSILON))
-    v0 = self->initial_velocity / (self->value_from - self->value_to);
-
   /*
    * Solutions of the form C1*e^(lambda1*x) + C2*e^(lambda2*x)
    * for the differential equation m*ẍ+b*ẋ+kx = 0
@@ -91,18 +86,18 @@ oscillate (AdwAnimation *animation,
   if (beta < omega0) {
     double omega1 = sqrt ((omega0 * omega0) - (beta * beta));
 
-    return -x0 + envelope * (x0 * cos (omega1 * t) + ((beta * x0 + v0) / omega1) * sin (omega1 * t));
+    return self->value_to + envelope * (x0 * cos (omega1 * t) + ((beta * x0 + v0) / omega1) * sin (omega1 * 
t));
   }
 
   /* Overdamped */
   if (beta > omega0) {
     double omega2 = sqrt ((beta * beta) - (omega0 * omega0));
 
-    return -x0 + envelope * (x0 * coshl (omega2 * t) + ((beta * x0 + v0) / omega2) * sinhl (omega2 * t));
+    return self->value_to + envelope * (x0 * coshl (omega2 * t) + ((beta * x0 + v0) / omega2) * sinhl 
(omega2 * t));
   }
 
   /* Critically damped */
-  return -x0 + envelope * (x0 + (beta * x0 + v0) * t);
+  return self->value_to + envelope * (x0 + (beta * x0 + v0) * t);
 }
 
 static guint
@@ -143,22 +138,22 @@ adw_spring_animation_estimate_duration (AdwAnimation *animation)
    * Newton's root finding method is a good candidate in this particular case:
    * https://en.wikipedia.org/wiki/Newton%27s_method
    */
-  y0 = oscillate (animation, -1, x0*1000);
-  m = (oscillate (animation, -1, (x0 + DELTA) * 1000) - y0) / DELTA;
+  y0 = oscillate (animation, x0*1000);
+  m = (oscillate (animation, (x0 + DELTA) * 1000) - y0) / DELTA;
 
-  x1 = (1 - y0 + m * x0) / m;
-  y1 = oscillate (animation, -1, x1*1000);
+  x1 = (self->value_to - y0 + m * x0) / m;
+  y1 = oscillate (animation, x1*1000);
 
-  while (ABS (1 - y1) > self->epsilon) {
+  while (ABS (self->value_to - y1) > self->epsilon) {
     if (i>1000)
       return 0;
     x0 = x1;
     y0 = y1;
 
-    m = (oscillate (animation, -1, (x0 + DELTA) * 1000) - y0) / DELTA;
+    m = (oscillate (animation, (x0 + DELTA) * 1000) - y0) / DELTA;
 
-    x1 = (1 - y0 + m * x0) / m;
-    y1 = oscillate (animation, -1, x1*1000);
+    x1 = (self->value_to - y0 + m * x0) / m;
+    y1 = oscillate (animation, x1*1000);
     i++;
   }
 
@@ -185,11 +180,7 @@ adw_spring_animation_calculate_value (AdwAnimation *animation,
   if (t >= self->estimated_duration)
     return self->value_to;
 
-  if (G_APPROX_VALUE (self->value_from, self->value_to, FLT_EPSILON)){
-    return self->value_from + oscillate (animation, 0, t);
-  }else{
-    return adw_lerp (self->value_from, self->value_to, oscillate (animation, -1, t));
-  }
+  return oscillate (animation, t);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]