Sign in to follow this  
Followers 0
FeXoR

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

18 posts in this topic

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])};

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

And sry for my poor english. :unsure:

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

You can edit your post.

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.mozilla.org/en/JavaScript/Guide/Statements

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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!

About the ";" thx for the statements, I'll read it when I have time (x)) but I closely listen to your advises!

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 :crazy:

Is there anything wrong with that? :blink:

Edited by FeXoR

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites

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. :banger:

I will learn... ;)

Edited by FeXoR

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites

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!!!! :ok:

Edited by FeXoR

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0