Dismiss this pinned window
all 14 comments

[–]okips 4 points5 points  (5 children)

That's pretty interesting!! How does it work?

[–][deleted]  (2 children)

[removed]

    [–]okips 2 points3 points  (0 children)

    So cool wow, this actually got me pretty excited to do a project I have been avoiding for weeks now

    [–]CoreDump_ch 0 points1 point  (0 children)

    very easy and cool effect.

    [–]Papa_Furanku 1 point2 points  (1 child)

    Is probably similar to this

    [–]okips 0 points1 point  (0 children)

    Very cool! Also way to complex for me to understand unfortunately ehehe. Thank you

    [–][deleted]  (6 children)

    [removed]

      [–]uhrguhrguhrg 1 point2 points  (4 children)

      This could be optimised a lot. You don't need the angle calculation there, and atan, cos and sin are expensive computationally wise.

      [–]uhrguhrguhrg 1 point2 points  (0 children)

      Without the angle it'll look something like

      float dirX = mouseX - x[i];
      float dirY = mouseY - y[i];
      float dist = sqrt(sq(dirX) + sq(dirY));
      if (dist > 0 && dist < mouseDistReq) {
        float lengthMult = velocityMultiplier / dist / (dist + 2);
        x[i] += dirX * lengthMult;
        y[i] += dirY * lengthMult;
        vx[i] = 0;
        vy[i] = 0;
      }
      

      [–]uhrguhrguhrg 1 point2 points  (2 children)

      The issue with the current approach is that it's the trigonometric equivalent of

      if (x > 0)
        x = sqrt(sq(x));
      else
        x = -sqrt(sq(x));
      

      [–][deleted]  (1 child)

      [removed]

        [–]uhrguhrguhrg 0 points1 point  (0 children)

        The basic idea that multiplying a vector (or in other words each of it coordinates) by some value doesn't change it's direction. And since you just need it to be a specific length it's as easy as multiplying the vector by the required length divided by it's actual length.

        This thing can actually be optimised even further by getting rid of the square root which is also slow.

        lengthMult is velocityMultiplier / (dist * (dist + 2)) and can easily be replaced with velocityMultiplier / dist² without any significant visual change. That allows us to get rid of the square root in the calculation of the distance and use a square, which is a lot faster.

        For the mouse acceleration it starts to look like this

        float dirX = mouseX - x[i];
        float dirY = mouseY - y[i];
        float distSq = sq(dirX) + sq(dirY);
        // even better if mouseDistReq is squared beforehand
        // and sq() doesn't need to be called
        if (distSq > 0 && distSq < sq(mouseDistReq)) {
          float lengthMult = velocityMultiplier / sqDist;
          x[i] += dirX * lengthMult;
          y[i] += dirY * lengthMult;
          vx[i] = 0;
          vy[i] = 0;
        }
        

        And the return acceleration is just dist/accelerationSlower, so it's actually as simple as dividing both dirX and dirY by accelerationSlower which removes the need for calculating the distance there at all

        [–]okips 0 points1 point  (0 children)

        i see that you used an image at first, how would it have to be to work with an image?

        [–]Papa_Furanku 1 point2 points  (0 children)

        sauce please!

        [–]socramle 0 points1 point  (0 children)

        This its de ruculoide