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
graphing-functions coordinate-systems ratio programming
New contributor
add a comment |
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
graphing-functions coordinate-systems ratio programming
New contributor
add a comment |
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
graphing-functions coordinate-systems ratio programming
New contributor
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
graphing-functions coordinate-systems ratio programming
New contributor
New contributor
New contributor
asked Nov 16 at 10:27
Matt
111
111
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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