# getDistance(point1, point2) and getDirection(point1, point2) from one point to another

17 replies to this topic

### #1 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 29 February 2012 - 05:15 PM

There are some functions i missed inside the rmgen .js files.

Here are some suggestions i find useful and put inside my random map as helper functions.
The "getDirection" function is a bit fuzzy but works (consistent with the unit placement orientation AFAIK).
Might be written more sober though.
I skipped checking the given operands in any way but that might be wise...

```// Function to get the distance between 2 points
function getDistance(point1, point2) {return Math.pow(Math.pow(point1[0] - point2[0], 2) + Math.pow(point1[1] - point2[1], 2), 1/2)};

// Function to get the distance between 2 points given in seperate coordinates
function getDistanceXZ(x1, z1, x2, z2) {return getDistance([x1, z1], [x2, z2])};

// Function to get the direction from one point to another
function getDirection(point1, point2)
{
var vector = [point2[0] - point1[0], point2[1] - point1[1]];
var output = Math.acos(vector[0]/getDistance(point1, point2));
if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(point1, point2)))};
return (output + PI/2) % (2*PI);
};

// Function to get the direction from one point to another given in seperate coordinates
function getDirectionXZ(x1, z1, x2, z2) {return getDirection([x1, z1], [x2, z2])};
```

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #2 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 29 February 2012 - 08:24 PM

The getDirection function fails if both points are the same.
So here's a version that don't...

```// Function to get the direction from one point to another
function getDirection(point1, point2)
{
var vector = [point2[0] - point1[0], point2[1] - point1[1]];
var output = 0;
if (vector[0] !== 0 || vector[1] !== 0)
{
output = Math.acos(vector[0]/getDistance(point1, point2));
if (vector[1] > 0) {output = PI + (PI - Math.acos(vector[0]/getDistance(point1, point2)))};
};
return (output + PI/2) % (2*PI);
};
```

Edited by FeXoR, 04 March 2012 - 09:32 AM.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #3 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 29 February 2012 - 08:30 PM

Inside the 'if' statement the 'var' in front of 'output' is not needed, but does'nt hurt AFAIK...

And sry for my poor english.
• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #4 quantumstate

quantumstate

Primus Pilus

• WFG Retired
• 1,150 posts

Posted 29 February 2012 - 10:50 PM

You should definitely remove the second var, it will confuse people who are used to block level scoping. Also in terms of formatting you should either use the style:

```if (foo)
{
bar;
}
```

or

```if (foo)
bar;
```

(Obviously you cannot use the second if you have multiple statements.)

The function is pretty confusing to read, using arccos() is unconventional. I would recommend using Math.atan2(), it will return the correct angle in the range -pi to pi.

I would also suggest renaming the function to getAngle(), this is less ambiguous.
• 0

Jonathan Waller [ aka quantumstate ]

Wildfire Games Programmer
Contact me: jonathanmarkwaller at gmail dot com

Support Wildfire Games!

### #5 Spahbod

Spahbod

Triplicarius

• WFG Programming Team
• 596 posts

Posted 01 March 2012 - 09:43 AM

Nice functions. But the thing we usually use for direction is tangent itself not the angle. So it is better not to use arcs in a separate function to save performance.
• 0
Omid Davoodi [ aka Spahbod ]

Wildfire Games Random Map Designer, Low-level Programmer
Contact me: myops37@yahoo.com

Support Wildfire Games!

### #6 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 01 March 2012 - 10:18 AM

You should definitely remove the second var, it will confuse people who are used to block level scoping.

I removed the "var" in my script, just didn't want to spam posts.

Also in terms of formatting you should either use the style:

```if (foo)
{
bar;
}
```

or

```if (foo)
bar;
```

(Obviously you cannot use the second if you have multiple statements.)

THX, I'm not familiar with javascript. I don't exactly know when to use ";" at the end of the line and it seldom raises warnings when not present. I saw some missing ones in other scripts so if I do something wrong, plz let me know.

The function is pretty confusing to read, using arccos() is unconventional. I would recommend using Math.atan2(), it will return the correct angle in the range -pi to pi.

I would also suggest renaming the function to getAngle(), this is less ambiguous.

Nice functions. But the thing we usually use for direction is tangent itself not the angle. So it is better not to use arcs in a separate function to save performance.

Oh, well, figured out the function myself because I didn't find one in the helper functions in the rmgen .js scripts and needed it.The main thing is a function with the same functionality despite the name and the exact code.So please add something like that in the scripts.If I find the time I will adjust the code to you'r recommendations.

Edited by FeXoR, 01 March 2012 - 10:20 AM.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #7 Ykkrosh

Ykkrosh

Primus Pilus

• WFG Programming Team
• 4,928 posts

Posted 01 March 2012 - 12:51 PM

Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions.
• 0
Philip Taylor [aka Ykkrosh]

Wildfire Games Programmer
Contact me: philip@wildfiregames.com

### #8 quantumstate

quantumstate

Primus Pilus

• WFG Retired
• 1,150 posts

Posted 01 March 2012 - 12:58 PM

I removed the "var" in my script, just didn't want to spam posts.

THX, I'm not familiar with javascript. I don't exactly know when to use ";" at the end of the line and it seldom raises warnings when not present. I saw some missing ones in other scripts so if I do something wrong, plz let me know.

There should be a ; after every statement in javascript. It would be good for you to learn about this, knowing the language well will allow oyu to write better code. Try reading https://developer.mo...uide/Statements
• 0

Jonathan Waller [ aka quantumstate ]

Wildfire Games Programmer
Contact me: jonathanmarkwaller at gmail dot com

Support Wildfire Games!

### #9 Spahbod

Spahbod

Triplicarius

• WFG Programming Team
• 596 posts

Posted 01 March 2012 - 01:03 PM

`Oh, well, figured out the function myself because I didn't find one in  the helper functions in the rmgen .js scripts and needed it.The main  thing is a function with the same functionality despite the name and the  exact code.So please add something like that in the scripts.If I find  the time I will adjust the code to you'r recommendations.`

As I said before I am creating more functions for rmgen. But currently my rmgen library and random map scripts are full of buggy things related to the new features I'm planning for alpha 10.
• 0
Omid Davoodi [ aka Spahbod ]

Wildfire Games Random Map Designer, Low-level Programmer
Contact me: myops37@yahoo.com

Support Wildfire Games!

### #10 Ykkrosh

Ykkrosh

Primus Pilus

• WFG Programming Team
• 4,928 posts

Posted 01 March 2012 - 01:07 PM

There should be a ; after every statement in javascript.

Depends what you mean by "should" . I think the language rule is basically that you can always replace a semicolon by a newline, but our (rather incomplete) coding conventions are that you should always include an explicit semicolon.
• 0
Philip Taylor [aka Ykkrosh]

Wildfire Games Programmer
Contact me: philip@wildfiregames.com

### #11 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 01 March 2012 - 01:53 PM

Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions.

Uh, didn't think of it! But isn't the map generated on the hosting computer and then passed to the other players?

And that kind of functions are used in many (if not all) RMGs like sin and cos.

I don't what to argue about how to implement them (and that my code sucks and is only meant as an example) but I feel they are really needed!

As I said before I am creating more functions for rmgen. But currently my rmgen library and random map scripts are full of buggy things related to the new features I'm planning for alpha 10.

I have no problem with that, I'll be patient and rewrite my files when it's added.

But until then I "have" to use my own functions even if my RMGs only work in single player... because I'm hot now and I admire it

Is there anything wrong with that?

Edited by FeXoR, 01 March 2012 - 02:56 PM.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #12 Ykkrosh

Ykkrosh

Primus Pilus

• WFG Programming Team
• 4,928 posts

Posted 01 March 2012 - 02:44 PM

But isn't the map generated on the hosting computer and then passed to the other players?

No - every player runs the script independently. (The generated map data can be quite large, so it would be slow to upload it to every player.)

And that kind of functions are used in many (if not all) RMGs like sin and cos.

That's potentially problematic. There's some examples here of where functions return different results; presumably the values often get rounded to integer tiles, but in some cases the difference will cause the value to cross the rounding boundary and cause OOS. (The OOS check doesn't verify the terrain data directly, so you'll only notice when the difference influences the behaviour of an entity.)
• 0
Philip Taylor [aka Ykkrosh]

Wildfire Games Programmer
Contact me: philip@wildfiregames.com

### #13 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 01 March 2012 - 03:11 PM

So the thing to do is only setting objects on tiles (eventually by rounding but only if necessary), never on coordinates (pair of floats) and hope that no missmatch will occur.

Things to avoid are then:
- ceil and floor (small missmatches of a natural number would often cause inconsistence)
- Using polar coordinates for entity/terrain placement (as I have done in my RMG, even if I round afterwards this would not be wise)

...and for sure other things I've done allrdy.

I will learn...

Edited by FeXoR, 01 March 2012 - 03:14 PM.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #14 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 03 March 2012 - 12:18 PM

Be careful that trigonometric functions in JS don't return precisely identical results on different platforms (Windows vs Linux etc), so they'll probably cause out-of-sync errors in multiplayer. It's best to avoid them entirely if at all possible (and prefer using vectors maths rather than trigonometry); otherwise we'll probably have to replace the standard versions of the functions with 'safe' (but likely slow and imprecise) versions.

After a little wile I noticed that it's quite impossible avoiding placing objects on coordinates (z and z in floats).
So where can I find a documentation of that 'vector maths' you are suggesting.
I only found vector math libraries but don't know if there's a default lib in JS for it and which one you use allrdy (if so).

I noticed though that may functions in the rmgen libs use trigonometric functions.

Please, answer this one quick so I avoid doing things I have to rewrite.

Thanks.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #15 quantumstate

quantumstate

Primus Pilus

• WFG Retired
• 1,150 posts

Posted 03 March 2012 - 12:57 PM

After a little wile I noticed that it's quite impossible avoiding placing objects on coordinates (z and z in floats).
So where can I find a documentation of that 'vector maths' you are suggesting.
I only found vector math libraries but don't know if there's a default lib in JS for it and which one you use allrdy (if so).

I noticed though that may functions in the rmgen libs use trigonometric functions.

Please, answer this one quick so I avoid doing things I have to rewrite.

The trig functions are being replaced with safe versions, I have done sin and cos, and will be doing atan2 sometime soon. The replacements should be pretty accurate (about 10 decimal places) and reasonably fast, if you avoid using them in big loops then it should be fine.

Vector maths is a general area of mathematics, for map making you would only need a basic knowledge though, I don't know where you would go to learn it though. I don't think there are any libraries used in javascript for the game, I just wrote the operations myself when working on the advanced attack code.
• 0

Jonathan Waller [ aka quantumstate ]

Wildfire Games Programmer
Contact me: jonathanmarkwaller at gmail dot com

Support Wildfire Games!

### #16 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 04 March 2012 - 12:33 AM

The trig functions are being replaced with safe versions, I have done sin and cos, and will be doing atan2 sometime soon. The replacements should be pretty accurate (about 10 decimal places) and reasonably fast, if you avoid using them in big loops then it should be fine.

Vector maths is a general area of mathematics, for map making you would only need a basic knowledge though, I don't know where you would go to learn it though. I don't think there are any libraries used in javascript for the game, I just wrote the operations myself when working on the advanced attack code.

I don't have problems with vector maths, I have the problem that I don't want to what Ykkrosh told me to avoid. And I don't know how to avoid it.

If you wrote functions with about 4 decimal points higher accuracy (The error in trigonometric functions in JS in different OS I rad about in posts was about 10**(-6)) wouldn't it be a good idea to include them in other areas of the game as well?

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps

### #17 historic_bruno

historic_bruno

Primus Pilus

• WFG Programming Team
• 2,656 posts

Posted 04 March 2012 - 12:37 AM

If you wrote functions with about 4 decimal points higher accuracy (The error in trigonometric functions in JS in different OS I rad about in posts was about 10**(-6)) wouldn't it be a good idea to include them in other areas of the game as well?

We've got replacements for sin, cos, atan, and atan2 now, see Math.js. They automatically replace the originals in simulation, random maps, etc.
• 0
Ben Brian [ aka historic_bruno ]

Wildfire Games Programmer
Contact me: ben [at] wildfiregames [dot] com

### #18 FeXoR

FeXoR

Centurio

• WFG Programming Team
• 986 posts

Posted 04 March 2012 - 12:51 AM

We've got replacements for sin, cos, atan, and atan2 now, see Math.js. They automatically replace the originals in simulation, random maps, etc.

THX, THX, THX!!!!

Edited by FeXoR, 04 March 2012 - 01:11 AM.

• 0

Florian Finke [aka FeXoR]

Wildfire Games Random Maps