Distance and Absolute Value - Printable Version +- Save-Point (https://www.save-point.org) +-- Forum: Games Development (https://www.save-point.org/forum-4.html) +--- Forum: Tutorials (https://www.save-point.org/forum-19.html) +--- Thread: Distance and Absolute Value (/thread-8658.html) |
Distance and Absolute Value - kyonides - 01-28-2023 Distance and Absolute Value
This topic is pretends to help a future generation of game making developers or even future programmers and scripters and coders. Our main question is how do I measure the distance between point A and B? So let's go back to 7th grade stuff here.
The calculation can done by adding the Distance of the X coordinate and the Distance of the Y coordinate. In some countries they would use the following notation to declare that they are using absolute values on a piece of paper. Code: |a - b| = |b - a| In Ruby and RGSS it would look like this: Code: dx = (x1 - x2).abs #=> Integer The pipes there are a symbol for the absolute value, that will end up being used as distance, but in Ruby you would call the abs method instead. Integer there is a placeholder for a positive value, in this case there are no decimals nor fractions involved. #=> is just a Rubyist's way to tell you what might be its return value from a human perspective without executing that part of the code. # marks the beginning of a inline comment. The equation tells us that no matter which one you subtract from the other value, the absolute value remains the same and will always be positive. So if you get as a result -7 or 7, the real distance will always be 7. You only use the negative value as a way to tell the player or game dev if the direction is negative. How does that translate to RGSS or even JS in the RM series? It's quite simple. -X means to the left hand side while -Y will always be to the upper border of the game window. If you just need either the distance on the X or Y axis, you gotta skip the third line of code then. Well, I'm leaving here some interesting read for geeks just in case they care about weird programming stuff. Content Hidden |