Distance and Absolute Value
#1
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|
# Example 1
|5 - 2| = |3| = 3
# Example 2
|-2 - 5| = |-7| = 7
# Example 3
|3 -(-2)| = |3 + 2| = |5| = 5

In Ruby and RGSS it would look like this:

Code:
dx = (x1 - x2).abs #=> Integer
dy = (y1 - y2).abs #=> Integer
d  = (dx + dy).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
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }




Users browsing this thread: 2 Guest(s)