Two math-related questions

What's a good way to compare the distance between two variables? Like a conditional branch that asks, if 'Variable_1' is within 3 integers of 'Variable_2' then... (no matter if either variable is positive or negative)

Also, is there a good formula to make one number closer to\more like another number? Like a formula that would make x closer to y, no matter if x is less than or greater than y. And x's final value would exponentially change less and less as it's starting value get's closer to what y is.

Thanks in advance!
For the first, you could do something like (if Variable_1 - Variable_2 >= 3 OR Variable_2 - Variable1 >=3 (do whatever))

For the second, if X < Y then X+1 else if Y > X then X-1

If you're just doing it in the editor without script, you may need to make extra variables to hold the answer or compare or whatever. If you're scripting, please note that the above is all fake code...in fact, it might not even make sense, so please ask for clarity if you need it. There are undoubtedly other and probably better ways to do what you want, but I think that will cover it...either way, good luck!
Distance, huh? Surely this can't be a coincidence based on what I was working on just recently.

FhK7jE1.png


So @Solr maybe this can spark some ideas. This is a page I made that tracks the "distance" between you and a designated enemy NPC.

I made it specifically so the game knows how far away you are from said NPC, and it works like such:
* Enemy X/Y are stored in two variables
* Player X/Y are stored in two variables (universal)
* Player X/Y are subtracted by enemy X/Y, dictating how far away you are from them

* The subtractions are stored as their own variables called Distance

It will automatically track how far away you are from the NPC regardless of where you go on the X/Y axis.

Regardless of which values are negative or positive, the conditional branches will guarantee that they don't cancel out to zero. Example-

(3X,-3Y) and (-3X,-3Y) should both equal to 6 distance on both ends.

Perhaps you can use this knowledge to have a system not related to map coordinates where if Variable A is bigger than Variable B, it's subtracted instead of added. I dunno.
Distance, huh? Surely this can't be a coincidence based on what I was working on just recently.

FhK7jE1.png


So @Solr maybe this can spark some ideas. This is a page I made that tracks the "distance" between you and a designated enemy NPC.

I made it specifically so the game knows how far away you are from said NPC, and it works like such:
* Enemy X/Y are stored in two variables
* Player X/Y are stored in two variables (universal)
* Player X/Y are subtracted by enemy X/Y, dictating how far away you are from them

* The subtractions are stored as their own variables called Distance

It will automatically track how far away you are from the NPC regardless of where you go on the X/Y axis.

Regardless of which values are negative or positive, the conditional branches will guarantee that they don't cancel out to zero. Example-

(3X,-3Y) and (-3X,-3Y) should both equal to 6 distance on both ends.

Perhaps you can use this knowledge to have a system not related to map coordinates where if Variable A is bigger than Variable B, it's subtracted instead of added. I dunno.
@RCXDan I'm a bit confused though, suppose Player X = -3 and Event X = 3, wouldn't -3 - 3 = -6?

Edit:

Oh sorry, I see what you mean 🙂 But how would I get the absolute value of that integer?
@RCXDan I'm a bit confused though, suppose Player X = -3 and Event X = 3, wouldn't -3 - 3 = -6?

Yes. But for the purposes of my system, negatives are also counted in conditional branches.

-6 distance and +6 distance accomplish the same thing, since they're effectively the same amount of tiles between you and the enemy NPC... just on opposite ends.
Oh sorry, I see what you mean 🙂 But how would I get the absolute value of that integer?

Does this work:

$game_variables[3] = ($game_variables[1] - $game_variables[2]).abs
Does this work:

$game_variables[3] = ($game_variables[1] - $game_variables[2]).abs
@rpgLord69
Thank you so much, it works!!! Thank you, @RCXDan as well for the original suggestion of subtracting the variables.

Originally, I was also designing a system to that involved determining a player's position on the map, but I thought of another niche use for it that I wanted to use it for (as long I was able to find a way to express the distance between two variables as an absolute value, which I am now able to 🙂 )
Capture5.PNG

@BenSD Thanks for the input as well! I suppose the VXA engine doesn't have an easily accessible way to let me mess around with freaking slope formulas; ...it's probably for the best.

I was thinking more of like if I want x to be 'more like' y, and if x's starting value is 2 and y is 10, then x would become 5.

4 would become 7
7 would become 9
14 would become 11
(something like this)

I guess I'll find some more hamfisted way to do this, thanks for the response though.
The rest of what you're looking for is something like:

VarX -= (VarX - VarY) / 2

if VarX is 3 and VarY is 8 Var 1 will add 5.5 (double negative), if VarX is 8 and VarY is 3 it will subtract 2.5. at this point, you could choose to round, floor or ceiling, depending on where you want the decimals to go. essentially cuts half the distance each time it's called.

i'm not at all certain of the coding conventions for VXAce, so I can't help with specifically how to give that to you.
The rest of what you're looking for is something like:

VarX -= (VarX - VarY) / 2

if VarX is 3 and VarY is 8 Var 1 will add 5.5 (double negative), if VarX is 8 and VarY is 3 it will subtract 2.5. at this point, you could choose to round, floor or ceiling, depending on where you want the decimals to go. essentially cuts half the distance each time it's called.

i'm not at all certain of the coding conventions for VXAce, so I can't help with specifically how to give that to you.
@deus69xxx I see. I'm not sure how to express operators like -= in ruby; coding was never my strong point. This is what I think I am looking for though, thanks for giving me a place to start at least 🙂
@Solr -= is the ruby coding. it's used for changing a variable by an amount.

this_value = 100
that value = 20
this_value -= that value

now this_value == 80
@Solr -= is the ruby coding. it's used for changing a variable by an amount.

this_value = 100
that value = 20
this_value -= that value

now this_value == 80
@Roninator2 Oh so "VarX -= (VarX - VarY) / 2" literally means "set new VarX to: VarX - (VarX - VarY) / 2". I kind of recognized the '-=' operator from community college python\windows powershell class, but I didn't realize the syntax also worked in ruby and I forgot what it did haha. Thanks for setting me in the right direction; I'll try to create and something and I'll post the results 🙂

Original thread: https://forums.rpgmakerweb.com/threads/two-math-related-questions.146324/