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