Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Calculating the gradient of a line  (Read 2846 times)

0 Members and 3 Guests are viewing this topic.

Crembotz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Calculating the gradient of a line
« on: July 15, 2019, 10:34:21 pm »
Hi everyone.

I'm currently learning SFML by reading "Beginning C++ Game Programming" by John Horton. In chapter 9, we develop a top-down shooter. In pages 277 - 279, he describes the formula he used to calculate a gradient as (x2-x1)/(y2-y1), this is used then used in the following variable:
float ratioXY = m_BulletSpeed/(1+gradinet);// Calculate the ratio between x and y
which is then used to calculate how far the bullet will travel horizontally and vertically each frame:
m_BulletDistanceY = ratioXY;
 m_BulletDistanceX = ratioXY * gradient;
My question is, why did he calculate the inverse gradient? Why the regular formula, which is (y2-y1)/(x2-x1) causes the bullet to behave incorrectly(not moving towards its intended target, moves toward a different direction)?
Thank you very much for your time!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Calculating the gradient of a line
« Reply #1 on: July 16, 2019, 09:45:51 pm »
I don't suppose it matters which dimension you put first; it depends on which way it will be used. Who says which dimension is what :D
If y was first (y2-y1)/(x2-x1), I would presume that the gradient would be multiplied with m_BulletDistanceY instead.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Calculating the gradient of a line
« Reply #2 on: July 16, 2019, 09:48:53 pm »
gradient  = (x2-x1) / (y2-y1)     // ratio dx / dy
ratioXY   = speed / (1+gradient)  // gradient is in the denominator => ratio dy / dx
                                  // (ignoring the 1)
distanceY = ratioXY               // it travels so much in Y direction
distanceX = ratioXY * gradient
          = distanceY * gradient
          = distanceY * dx / dy   // Y distance multiplied with d(X/Y) = X distance
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Crembotz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Calculating the gradient of a line
« Reply #3 on: July 16, 2019, 10:40:30 pm »
I don't suppose it matters which dimension you put first; it depends on which way it will be used. Who says which dimension is what :D
If y was first (y2-y1)/(x2-x1), I would presume that the gradient would be multiplied with m_BulletDistanceY instead.
I'm sorry, could you please explain what you mean by first?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Calculating the gradient of a line
« Reply #4 on: July 17, 2019, 06:41:22 pm »
If you have a point a x1=5 and a point at x2=7, then 5-7 = -2 so it goes to the negative direction, but 7-5 = 2 so it goes to the positive direction.

This can also be solved by using the absolute value and multiplying by the direction you need to go, but if you know the direction from the start, you can also just change the parameters around.

It's really just some math. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Calculating the gradient of a line
« Reply #5 on: July 17, 2019, 08:09:59 pm »
Also, I would highly encourage you to learn vector algebra. It's one of the most intuitive fields of math and can make a lot of geometric relations really simple to model.

In your example, instead of separate X and Y coordinates, you might have a position and direction vector, plus a scalar (float) for the speed.
sf::Vector2f position = ...;
sf::Vector2f direction = ...;
float speed = ...

The velocity would be the speed multiplied by the direction. The next position would simply be the current position, plus the velocity vector, scaled by the passed time:
sf::Vector2f velocity = speed * direction;
sf::Time passedTime = ...;

sf::Vector2f nextPosition = position + velocity * passedTime.asSeconds();

To get started, maybe look for an introductory course on vectors on the Internet. Regarding API, you can use SFML for the basic types and operations and my library Thor for advanced vector operations.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Crembotz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Calculating the gradient of a line
« Reply #6 on: July 17, 2019, 09:00:08 pm »
Also, I would highly encourage you to learn vector algebra. It's one of the most intuitive fields of math and can make a lot of geometric relations really simple to model.

In your example, instead of separate X and Y coordinates, you might have a position and direction vector, plus a scalar (float) for the speed.
sf::Vector2f position = ...;
sf::Vector2f direction = ...;
float speed = ...

The velocity would be the speed multiplied by the direction. The next position would simply be the current position, plus the velocity vector, scaled by the passed time:
sf::Vector2f velocity = speed * direction;
sf::Time passedTime = ...;

sf::Vector2f nextPosition = position + velocity * passedTime.asSeconds();

To get started, maybe look for an introductory course on vectors on the Internet. Regarding API, you can use SFML for the basic types and operations and my library Thor for advanced vector operations.

Thanks for the tip but I'm already quite familiar with vectors, it's just that the author's choice to go against a well-known formula really confused me, but I've got it figured it out thanks to you guys. The conclusions I reached regarding this section:
1. The numerator can be either the difference in the X-axis or the Y-axis, it doesn't matter as long you take care to multiply the variable that represents the object's velocity in that same axis by the gradient(in this case, since the difference in the X-axis in the numerator, then m_BulletDistanceX is multiplied by gradient).
Why is it so? Because the ratio between the velocity in both axes has to be gradient.
2. Which leads me to my second point, the variable ratioXY(bad name if you ask me, because it does NOT represent the ratio between the two axes) is quite pointless and can be exchanged by any other constant or discarded completely. From what I gather, it's here to further control the object's velocity in accordance with the distance it has to travel. Also dividing by (1+gradient) can be exchanged by any other action including gradient or it can be discarded completely if you're not interested in that "extra-control over the value" stuff.

And that's the weird way my brain works, hope this helps anyone and thank you guys so much for your help!!