Can anyone explain to me the math behind this code ? Calculating a bullet's trajectory in 2D











up vote
2
down vote

favorite












following the book Beggining c++ game programming by John Horton on Chapter 9 the author explains us how our character can shoot a bullet, the thing is that there is very little explanations on what we are actually doing so I was hoping anyone could help me out.



So we know our bullet's starting location as it is the player's position and it's target location, the mouse position.



My main struggle comes from a "shoot" function, I don't really understand it's purpose and how the math is done in it. Here is John's explanation on it :




"Now we use a bit of simple trigonometry to determine the gradient of
travel for a bullet. The progression horizontally and vertically of a
bullet must vary based on the slope of the line created by drawing
between the start and target of a bullet. The rate of change cannot be
the same or very steep shots will arrive at the horizontal location
before the vertical location, and vice versa for shallow shots."




To do so he does the following :



gradient = (startX - targetX) / (startY - targetY)



This is the first weird thing to me, isn't the gradient supposed to be dY/dX ?
I found out that what we are actually calculating here is 1/gradient, am I wrong ?



His explanation for that is :




The following code first derives the gradient based on the equation of a line.




Can anyone explain to me ?



After that here are John's words :




"Next we calculate a ratio of horizontal to vertical distance by
dividing our bullet's speed ( m_BulletSpeed ) by one plus the
gradient. This will allow us to change the bullet's horizontal and
vertical position by the correct amount each frame, based on the
target the bullet is heading toward."




The formula is RatioXY = m_BulletSpeed / (1+gradient)



This is complete fog to me, I don't understand what RatioXY is, why we calculate it and how he came up with this formula.



Note : m_BulletSpeed is known



And finally, from what I understand we set a separate speed for X and Y to have an overall uniform speed.



m_BulletDistanceY = ratioXY



m_BulletDistanceX = ratioXY * gradient



But since I understood barely anything of the function I don't really understand how he came up with these formulas either.



Thank you to anyone taking the time to read my long post lol










share|cite|improve this question







New contributor




Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    following the book Beggining c++ game programming by John Horton on Chapter 9 the author explains us how our character can shoot a bullet, the thing is that there is very little explanations on what we are actually doing so I was hoping anyone could help me out.



    So we know our bullet's starting location as it is the player's position and it's target location, the mouse position.



    My main struggle comes from a "shoot" function, I don't really understand it's purpose and how the math is done in it. Here is John's explanation on it :




    "Now we use a bit of simple trigonometry to determine the gradient of
    travel for a bullet. The progression horizontally and vertically of a
    bullet must vary based on the slope of the line created by drawing
    between the start and target of a bullet. The rate of change cannot be
    the same or very steep shots will arrive at the horizontal location
    before the vertical location, and vice versa for shallow shots."




    To do so he does the following :



    gradient = (startX - targetX) / (startY - targetY)



    This is the first weird thing to me, isn't the gradient supposed to be dY/dX ?
    I found out that what we are actually calculating here is 1/gradient, am I wrong ?



    His explanation for that is :




    The following code first derives the gradient based on the equation of a line.




    Can anyone explain to me ?



    After that here are John's words :




    "Next we calculate a ratio of horizontal to vertical distance by
    dividing our bullet's speed ( m_BulletSpeed ) by one plus the
    gradient. This will allow us to change the bullet's horizontal and
    vertical position by the correct amount each frame, based on the
    target the bullet is heading toward."




    The formula is RatioXY = m_BulletSpeed / (1+gradient)



    This is complete fog to me, I don't understand what RatioXY is, why we calculate it and how he came up with this formula.



    Note : m_BulletSpeed is known



    And finally, from what I understand we set a separate speed for X and Y to have an overall uniform speed.



    m_BulletDistanceY = ratioXY



    m_BulletDistanceX = ratioXY * gradient



    But since I understood barely anything of the function I don't really understand how he came up with these formulas either.



    Thank you to anyone taking the time to read my long post lol










    share|cite|improve this question







    New contributor




    Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      following the book Beggining c++ game programming by John Horton on Chapter 9 the author explains us how our character can shoot a bullet, the thing is that there is very little explanations on what we are actually doing so I was hoping anyone could help me out.



      So we know our bullet's starting location as it is the player's position and it's target location, the mouse position.



      My main struggle comes from a "shoot" function, I don't really understand it's purpose and how the math is done in it. Here is John's explanation on it :




      "Now we use a bit of simple trigonometry to determine the gradient of
      travel for a bullet. The progression horizontally and vertically of a
      bullet must vary based on the slope of the line created by drawing
      between the start and target of a bullet. The rate of change cannot be
      the same or very steep shots will arrive at the horizontal location
      before the vertical location, and vice versa for shallow shots."




      To do so he does the following :



      gradient = (startX - targetX) / (startY - targetY)



      This is the first weird thing to me, isn't the gradient supposed to be dY/dX ?
      I found out that what we are actually calculating here is 1/gradient, am I wrong ?



      His explanation for that is :




      The following code first derives the gradient based on the equation of a line.




      Can anyone explain to me ?



      After that here are John's words :




      "Next we calculate a ratio of horizontal to vertical distance by
      dividing our bullet's speed ( m_BulletSpeed ) by one plus the
      gradient. This will allow us to change the bullet's horizontal and
      vertical position by the correct amount each frame, based on the
      target the bullet is heading toward."




      The formula is RatioXY = m_BulletSpeed / (1+gradient)



      This is complete fog to me, I don't understand what RatioXY is, why we calculate it and how he came up with this formula.



      Note : m_BulletSpeed is known



      And finally, from what I understand we set a separate speed for X and Y to have an overall uniform speed.



      m_BulletDistanceY = ratioXY



      m_BulletDistanceX = ratioXY * gradient



      But since I understood barely anything of the function I don't really understand how he came up with these formulas either.



      Thank you to anyone taking the time to read my long post lol










      share|cite|improve this question







      New contributor




      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      following the book Beggining c++ game programming by John Horton on Chapter 9 the author explains us how our character can shoot a bullet, the thing is that there is very little explanations on what we are actually doing so I was hoping anyone could help me out.



      So we know our bullet's starting location as it is the player's position and it's target location, the mouse position.



      My main struggle comes from a "shoot" function, I don't really understand it's purpose and how the math is done in it. Here is John's explanation on it :




      "Now we use a bit of simple trigonometry to determine the gradient of
      travel for a bullet. The progression horizontally and vertically of a
      bullet must vary based on the slope of the line created by drawing
      between the start and target of a bullet. The rate of change cannot be
      the same or very steep shots will arrive at the horizontal location
      before the vertical location, and vice versa for shallow shots."




      To do so he does the following :



      gradient = (startX - targetX) / (startY - targetY)



      This is the first weird thing to me, isn't the gradient supposed to be dY/dX ?
      I found out that what we are actually calculating here is 1/gradient, am I wrong ?



      His explanation for that is :




      The following code first derives the gradient based on the equation of a line.




      Can anyone explain to me ?



      After that here are John's words :




      "Next we calculate a ratio of horizontal to vertical distance by
      dividing our bullet's speed ( m_BulletSpeed ) by one plus the
      gradient. This will allow us to change the bullet's horizontal and
      vertical position by the correct amount each frame, based on the
      target the bullet is heading toward."




      The formula is RatioXY = m_BulletSpeed / (1+gradient)



      This is complete fog to me, I don't understand what RatioXY is, why we calculate it and how he came up with this formula.



      Note : m_BulletSpeed is known



      And finally, from what I understand we set a separate speed for X and Y to have an overall uniform speed.



      m_BulletDistanceY = ratioXY



      m_BulletDistanceX = ratioXY * gradient



      But since I understood barely anything of the function I don't really understand how he came up with these formulas either.



      Thank you to anyone taking the time to read my long post lol







      graphing-functions coordinate-systems ratio programming






      share|cite|improve this question







      New contributor




      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|cite|improve this question







      New contributor




      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|cite|improve this question




      share|cite|improve this question






      New contributor




      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 16 at 10:27









      Matt

      111




      111




      New contributor




      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Matt is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3000978%2fcan-anyone-explain-to-me-the-math-behind-this-code-calculating-a-bullets-traj%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Matt is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Matt is a new contributor. Be nice, and check out our Code of Conduct.













          Matt is a new contributor. Be nice, and check out our Code of Conduct.












          Matt is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3000978%2fcan-anyone-explain-to-me-the-math-behind-this-code-calculating-a-bullets-traj%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bundesstraße 106

          Verónica Boquete

          Ida-Boy-Ed-Garten