Math smoothing position of two objects

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am search a math formula to solve this problem, preferably in C#



  • A object should be to near smooth an other object and stop if reached the position of the destination object.

  • The destination object can change the position before the second object reach and yet the moving is smooting again if possible.

  • I need a min and max acceleration config

code example without smoothing



var x1 = 100;
var x2 = 50;

while (x1 >= x2)

x2++;



update example base on the answer of vadim123



var minimum = 0.5;
var maximum = 5.0;
var frac = 1 / 20.0;

var x1 = 100.0;
var x2 = 50.0;

while (x1 >= x2)

x2 = x2 + Math.Min(maximum, Math.Max(minimum, (x1 - x2) * frac));







share|cite|improve this question

























    up vote
    1
    down vote

    favorite












    I am search a math formula to solve this problem, preferably in C#



    • A object should be to near smooth an other object and stop if reached the position of the destination object.

    • The destination object can change the position before the second object reach and yet the moving is smooting again if possible.

    • I need a min and max acceleration config

    code example without smoothing



    var x1 = 100;
    var x2 = 50;

    while (x1 >= x2)

    x2++;



    update example base on the answer of vadim123



    var minimum = 0.5;
    var maximum = 5.0;
    var frac = 1 / 20.0;

    var x1 = 100.0;
    var x2 = 50.0;

    while (x1 >= x2)

    x2 = x2 + Math.Min(maximum, Math.Max(minimum, (x1 - x2) * frac));







    share|cite|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am search a math formula to solve this problem, preferably in C#



      • A object should be to near smooth an other object and stop if reached the position of the destination object.

      • The destination object can change the position before the second object reach and yet the moving is smooting again if possible.

      • I need a min and max acceleration config

      code example without smoothing



      var x1 = 100;
      var x2 = 50;

      while (x1 >= x2)

      x2++;



      update example base on the answer of vadim123



      var minimum = 0.5;
      var maximum = 5.0;
      var frac = 1 / 20.0;

      var x1 = 100.0;
      var x2 = 50.0;

      while (x1 >= x2)

      x2 = x2 + Math.Min(maximum, Math.Max(minimum, (x1 - x2) * frac));







      share|cite|improve this question













      I am search a math formula to solve this problem, preferably in C#



      • A object should be to near smooth an other object and stop if reached the position of the destination object.

      • The destination object can change the position before the second object reach and yet the moving is smooting again if possible.

      • I need a min and max acceleration config

      code example without smoothing



      var x1 = 100;
      var x2 = 50;

      while (x1 >= x2)

      x2++;



      update example base on the answer of vadim123



      var minimum = 0.5;
      var maximum = 5.0;
      var frac = 1 / 20.0;

      var x1 = 100.0;
      var x2 = 50.0;

      while (x1 >= x2)

      x2 = x2 + Math.Min(maximum, Math.Max(minimum, (x1 - x2) * frac));









      share|cite|improve this question












      share|cite|improve this question




      share|cite|improve this question








      edited Aug 7 at 18:56
























      asked Aug 7 at 18:34









      Tom Baires

      83




      83




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I don't know C#, but here is some pseudocode. Let $MIN_V$ and $MAX_V$ be the minimum and maximum velocity, respectively. Let $FRAC$ be a smoothing coefficient, between $0$ and $1$, to be chosen for desired level of smoothing.



          $$x2 = x2 + min(MAX_V, max(MIN_V, (x1-x2)*FRAC ))$$



          Explanation: We will add $(x1-x2)*FRAC$ to $x1$ at each time step; if $FRAC=1/3$, then we will close one third of the distance at each time step. However, this is clipped to be within the max and min velocity. Without clipping, we go too fast at the start, and very very slow at the end (never arriving).






          If you really want max and min acceleration (not velocity), the problem is impossible to solve exactly -- a plan to approach $x2$ at maximal speed, slowing down as fast as possible, could be ruined by $x2$ suddenly coming closer.




          share|cite|improve this answer





















          • How i can start slow and increase the speed to the end?
            – Tom Baires
            Aug 7 at 18:57










          • Don't you want to start fast and decrease the speed as they get closer?
            – user1949350
            Aug 7 at 19:13










          • @user1949350 i want start and stop slow and the time between fast.
            – Tom Baires
            Aug 7 at 19:31










          • If you increase FRAC, you will increase speed.
            – vadim123
            Aug 7 at 19:57










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "69"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2875251%2fmath-smoothing-position-of-two-objects%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I don't know C#, but here is some pseudocode. Let $MIN_V$ and $MAX_V$ be the minimum and maximum velocity, respectively. Let $FRAC$ be a smoothing coefficient, between $0$ and $1$, to be chosen for desired level of smoothing.



          $$x2 = x2 + min(MAX_V, max(MIN_V, (x1-x2)*FRAC ))$$



          Explanation: We will add $(x1-x2)*FRAC$ to $x1$ at each time step; if $FRAC=1/3$, then we will close one third of the distance at each time step. However, this is clipped to be within the max and min velocity. Without clipping, we go too fast at the start, and very very slow at the end (never arriving).






          If you really want max and min acceleration (not velocity), the problem is impossible to solve exactly -- a plan to approach $x2$ at maximal speed, slowing down as fast as possible, could be ruined by $x2$ suddenly coming closer.




          share|cite|improve this answer





















          • How i can start slow and increase the speed to the end?
            – Tom Baires
            Aug 7 at 18:57










          • Don't you want to start fast and decrease the speed as they get closer?
            – user1949350
            Aug 7 at 19:13










          • @user1949350 i want start and stop slow and the time between fast.
            – Tom Baires
            Aug 7 at 19:31










          • If you increase FRAC, you will increase speed.
            – vadim123
            Aug 7 at 19:57














          up vote
          0
          down vote



          accepted










          I don't know C#, but here is some pseudocode. Let $MIN_V$ and $MAX_V$ be the minimum and maximum velocity, respectively. Let $FRAC$ be a smoothing coefficient, between $0$ and $1$, to be chosen for desired level of smoothing.



          $$x2 = x2 + min(MAX_V, max(MIN_V, (x1-x2)*FRAC ))$$



          Explanation: We will add $(x1-x2)*FRAC$ to $x1$ at each time step; if $FRAC=1/3$, then we will close one third of the distance at each time step. However, this is clipped to be within the max and min velocity. Without clipping, we go too fast at the start, and very very slow at the end (never arriving).






          If you really want max and min acceleration (not velocity), the problem is impossible to solve exactly -- a plan to approach $x2$ at maximal speed, slowing down as fast as possible, could be ruined by $x2$ suddenly coming closer.




          share|cite|improve this answer





















          • How i can start slow and increase the speed to the end?
            – Tom Baires
            Aug 7 at 18:57










          • Don't you want to start fast and decrease the speed as they get closer?
            – user1949350
            Aug 7 at 19:13










          • @user1949350 i want start and stop slow and the time between fast.
            – Tom Baires
            Aug 7 at 19:31










          • If you increase FRAC, you will increase speed.
            – vadim123
            Aug 7 at 19:57












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I don't know C#, but here is some pseudocode. Let $MIN_V$ and $MAX_V$ be the minimum and maximum velocity, respectively. Let $FRAC$ be a smoothing coefficient, between $0$ and $1$, to be chosen for desired level of smoothing.



          $$x2 = x2 + min(MAX_V, max(MIN_V, (x1-x2)*FRAC ))$$



          Explanation: We will add $(x1-x2)*FRAC$ to $x1$ at each time step; if $FRAC=1/3$, then we will close one third of the distance at each time step. However, this is clipped to be within the max and min velocity. Without clipping, we go too fast at the start, and very very slow at the end (never arriving).






          If you really want max and min acceleration (not velocity), the problem is impossible to solve exactly -- a plan to approach $x2$ at maximal speed, slowing down as fast as possible, could be ruined by $x2$ suddenly coming closer.




          share|cite|improve this answer













          I don't know C#, but here is some pseudocode. Let $MIN_V$ and $MAX_V$ be the minimum and maximum velocity, respectively. Let $FRAC$ be a smoothing coefficient, between $0$ and $1$, to be chosen for desired level of smoothing.



          $$x2 = x2 + min(MAX_V, max(MIN_V, (x1-x2)*FRAC ))$$



          Explanation: We will add $(x1-x2)*FRAC$ to $x1$ at each time step; if $FRAC=1/3$, then we will close one third of the distance at each time step. However, this is clipped to be within the max and min velocity. Without clipping, we go too fast at the start, and very very slow at the end (never arriving).






          If you really want max and min acceleration (not velocity), the problem is impossible to solve exactly -- a plan to approach $x2$ at maximal speed, slowing down as fast as possible, could be ruined by $x2$ suddenly coming closer.





          share|cite|improve this answer













          share|cite|improve this answer



          share|cite|improve this answer











          answered Aug 7 at 18:41









          vadim123

          73.8k895184




          73.8k895184











          • How i can start slow and increase the speed to the end?
            – Tom Baires
            Aug 7 at 18:57










          • Don't you want to start fast and decrease the speed as they get closer?
            – user1949350
            Aug 7 at 19:13










          • @user1949350 i want start and stop slow and the time between fast.
            – Tom Baires
            Aug 7 at 19:31










          • If you increase FRAC, you will increase speed.
            – vadim123
            Aug 7 at 19:57
















          • How i can start slow and increase the speed to the end?
            – Tom Baires
            Aug 7 at 18:57










          • Don't you want to start fast and decrease the speed as they get closer?
            – user1949350
            Aug 7 at 19:13










          • @user1949350 i want start and stop slow and the time between fast.
            – Tom Baires
            Aug 7 at 19:31










          • If you increase FRAC, you will increase speed.
            – vadim123
            Aug 7 at 19:57















          How i can start slow and increase the speed to the end?
          – Tom Baires
          Aug 7 at 18:57




          How i can start slow and increase the speed to the end?
          – Tom Baires
          Aug 7 at 18:57












          Don't you want to start fast and decrease the speed as they get closer?
          – user1949350
          Aug 7 at 19:13




          Don't you want to start fast and decrease the speed as they get closer?
          – user1949350
          Aug 7 at 19:13












          @user1949350 i want start and stop slow and the time between fast.
          – Tom Baires
          Aug 7 at 19:31




          @user1949350 i want start and stop slow and the time between fast.
          – Tom Baires
          Aug 7 at 19:31












          If you increase FRAC, you will increase speed.
          – vadim123
          Aug 7 at 19:57




          If you increase FRAC, you will increase speed.
          – vadim123
          Aug 7 at 19:57












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2875251%2fmath-smoothing-position-of-two-objects%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Mutual Information Always Non-negative

          Why am i infinitely getting the same tweet with the Twitter Search API?