Light year distance calculation

General discussion for players of Oolite.

Moderators: winston, another_commander

Post Reply
User avatar
phkb
Impressively Grand Sub-Admiral
Impressively Grand Sub-Admiral
Posts: 4830
Joined: Tue Jan 21, 2014 10:37 pm
Location: Writing more OXPs, because the world needs more OXPs.

Light year distance calculation

Post by phkb »

What's the correct way of calculating the distance in LY between two systems? I know I can do this using Javascript, but if I were doing it outside the system, using two sets of co-ordinates, the standard geometric function SQRT((X2-X1)^2 + (Y2-Y1)^2) doesn't seem to match the actual distances in game.

Also, on the Wiki, for the New Galaxy Guide, there's a clickable sector map, and in the Wiki code page it has entries like this for each planet:

Code: Select all

{{ClickMapEntry|Name=Tibedied|Sector=1|XPos=2|YPos=90|XPosRel=7|YPosRel=136}}
.What are the XPosRel and YPosRel values and how do they relate to the XPos and YPos values.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Light year distance calculation

Post by cim »

phkb wrote:
the standard geometric function SQRT((X2-X1)^2 + (Y2-Y1)^2) doesn't seem to match the actual distances in game.
There are two things that stop this working.
  1. Both coordinates are measured 0-255, but the Y-coordinate describes half the distance
  2. For compatibility with Elite, the calculation uses integer maths to calculate in units of 0.4 LY
Here's a JS version of the formula.

Code: Select all

var dx = x1 - x2;
var dy = Math.floor((y1 - y2)/2);
return (Math.floor(Math.sqrt((dx*dx)+(dy*dy)))*0.4);
The XPosRel and YPosRel coordinates appear to have been normalised to put both X and Y into the same units, which I suspect - without checking - is "pixels on the map on the Wiki".
User avatar
phkb
Impressively Grand Sub-Admiral
Impressively Grand Sub-Admiral
Posts: 4830
Joined: Tue Jan 21, 2014 10:37 pm
Location: Writing more OXPs, because the world needs more OXPs.

Re: Light year distance calculation

Post by phkb »

Thanks, cim!
User avatar
Cholmondely
Archivist
Archivist
Posts: 5364
Joined: Tue Jul 07, 2020 11:00 am
Location: The Delightful Domains of His Most Britannic Majesty (industrial? agricultural? mainly anything?)
Contact:

Re: Light year distance calculation

Post by Cholmondely »

cim wrote: Mon Jan 12, 2015 7:22 am
phkb wrote:
the standard geometric function SQRT((X2-X1)^2 + (Y2-Y1)^2) doesn't seem to match the actual distances in game.
There are two things that stop this working.
  1. Both coordinates are measured 0-255, but the Y-coordinate describes half the distance
  2. For compatibility with Elite, the calculation uses integer maths to calculate in units of 0.4 LY
Here's a JS version of the formula.

Code: Select all

var dx = x1 - x2;
var dy = Math.floor((y1 - y2)/2);
return (Math.floor(Math.sqrt((dx*dx)+(dy*dy)))*0.4);
The XPosRel and YPosRel coordinates appear to have been normalised to put both X and Y into the same units, which I suspect - without checking - is "pixels on the map on the Wiki".
Can anybody help with this, please?

Cody & I want to finish off this page and get it up on the wiki.

Code: Select all

== Map ==
[[Sector7/The Ceraso Hub (Region)|The Ceraso Hub]] consists of the systems [[Sector7/Anisinza|Anisinza]], [[Sector7/Ceraso|Ceraso]], [[Sector7/Ceerti|Ceerti]] and [[Sector7/Gebeti|Gebeti]].
This is the problematic bit: how on earth do I calculate the XPosRel & YPosRel values?

Code: Select all

<div style="position:relative">{{ClickMapHeader|Sector=7|Image=Sector7_The_Ceraso_Hub_RegionMap.png}}
{{ClickMapEntry|Name=Anisinza|Sector=7|XPos=19|YPos=14|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceraso|Sector=7|XPos=13|YPos=1|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceerti|Sector=7|XPos=1|YPos=9|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Gebeti|Sector=7|XPos=0|YPos=15|XPosRel=???|YPosRel=??}}
{{ClickMapFooter}}</div>

Code: Select all

{{SectorTableHeader}}
{{SectorTableEntry|Name=Anisinza|Sector=7|System=90|XPos=19|YPos=14|Government=3|Economy=6|Techlevel=7|Population=3.4|Inhabitants=Human Colonists|Productivity=7616|Hubcount=2|Description=The world Anisinza is scourged by deadly disease.}}

{{SectorTableEntry|Name=Ceraso|Sector=7|System=113|XPos=13|YPos=1|Government=5|Economy=1|Techlevel=11|Population=4.7|Inhabitants=Fierce Yellow Fat Humanoids|Productivity=30456|Hubcount=3|Description=The planet Ceraso is reasonably fabled for its exciting sit coms and its inhabitants' exceptional love for food blenders.}}

{{SectorTableEntry|Name=Ceerti|Sector=7|System=62|XPos=1|YPos=9|Government=1|Economy=3|Techlevel=7|Population=3.5|Inhabitants=Fierce Slimy Frogs|Productivity=8120|Hubcount=2|Description=This world is a revolting little planet.}}

{{SectorTableEntry|Name=Gebeti|Sector=7|System=126|XPos=0|YPos=15|Government=1|Economy=7|Techlevel=2|Population=1.3|Inhabitants=Large Green Lizards|Productivity=1560|Hubcount=2|Description=Gebeti is very fabled for its ancient Gebetian Et banana plantations.}}
{{SectorTableFooter}}
[[Category:Sector7/Region|The Ceraso Hub]]
{{RoutesAndRegionsSector7}}
Comments wanted:
Missing OXPs? What do you think is missing?
Lore: The economics of ship building How many built for Aronar?
Lore: The Space Traders Flight Training Manual: Cowell & MgRath Do you agree with Redspear?
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: Light year distance calculation

Post by Cody »

I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
User avatar
hiran
Theorethicist
Posts: 2403
Joined: Fri Mar 26, 2021 1:39 pm
Location: a parallel world I created for myself. Some call it a singularity...

Re: Light year distance calculation

Post by hiran »

Cholmondely wrote: Mon Nov 01, 2021 11:48 pm
Can anybody help with this, please?

Cody & I want to finish off this page and get it up on the wiki.

Code: Select all

== Map ==
[[Sector7/The Ceraso Hub (Region)|The Ceraso Hub]] consists of the systems [[Sector7/Anisinza|Anisinza]], [[Sector7/Ceraso|Ceraso]], [[Sector7/Ceerti|Ceerti]] and [[Sector7/Gebeti|Gebeti]].
This is the problematic bit: how on earth do I calculate the XPosRel & YPosRel values?

Code: Select all

<div style="position:relative">{{ClickMapHeader|Sector=7|Image=Sector7_The_Ceraso_Hub_RegionMap.png}}
{{ClickMapEntry|Name=Anisinza|Sector=7|XPos=19|YPos=14|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceraso|Sector=7|XPos=13|YPos=1|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceerti|Sector=7|XPos=1|YPos=9|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Gebeti|Sector=7|XPos=0|YPos=15|XPosRel=???|YPosRel=??}}
{{ClickMapFooter}}</div>
[/code]
I guess what you are after is ultimately an HTML Image Map.
However, as people think HTML is too complicated Wiki applications were created that allow simple text editing and they automatically translate to HTML. Over time they needed more power, and one by one a lot of HTML features became accessible via Wikis - but they still tend to translate.

So you need to learn how to create an Image Map in MediaWiki. Unfortunately I cannot find any documentation for MediaWiki and ClickMapEntry or other such keywords. However MediaWiki documentation would be the authorative source, unless it comes from a plugin that was installed on top.

There is a chance that these values are not used at all. When I checked one of these maps, it seems all ClickMapEntries get intermingled into rectangular areas five pixels high and five pixels wide.
Sunshine - Moonlight - Good Times - Oolite
User avatar
PhantorGorth
---- E L I T E ----
---- E L I T E ----
Posts: 647
Joined: Wed May 20, 2009 6:48 pm
Location: Somewhere off the top left of Galaxy 1 map

Re: Light year distance calculation

Post by PhantorGorth »

Cholmondely wrote: Mon Nov 01, 2021 11:48 pm
cim wrote: Mon Jan 12, 2015 7:22 am
phkb wrote:
the standard geometric function SQRT((X2-X1)^2 + (Y2-Y1)^2) doesn't seem to match the actual distances in game.
There are two things that stop this working.
  1. Both coordinates are measured 0-255, but the Y-coordinate describes half the distance
  2. For compatibility with Elite, the calculation uses integer maths to calculate in units of 0.4 LY
Here's a JS version of the formula.

Code: Select all

var dx = x1 - x2;
var dy = Math.floor((y1 - y2)/2);
return (Math.floor(Math.sqrt((dx*dx)+(dy*dy)))*0.4);
The XPosRel and YPosRel coordinates appear to have been normalised to put both X and Y into the same units, which I suspect - without checking - is "pixels on the map on the Wiki".
Can anybody help with this, please?

Cody & I want to finish off this page and get it up on the wiki.

Code: Select all

== Map ==
[[Sector7/The Ceraso Hub (Region)|The Ceraso Hub]] consists of the systems [[Sector7/Anisinza|Anisinza]], [[Sector7/Ceraso|Ceraso]], [[Sector7/Ceerti|Ceerti]] and [[Sector7/Gebeti|Gebeti]].
This is the problematic bit: how on earth do I calculate the XPosRel & YPosRel values?

Code: Select all

<div style="position:relative">{{ClickMapHeader|Sector=7|Image=Sector7_The_Ceraso_Hub_RegionMap.png}}
{{ClickMapEntry|Name=Anisinza|Sector=7|XPos=19|YPos=14|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceraso|Sector=7|XPos=13|YPos=1|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceerti|Sector=7|XPos=1|YPos=9|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Gebeti|Sector=7|XPos=0|YPos=15|XPosRel=???|YPosRel=??}}
{{ClickMapFooter}}</div>

Code: Select all

{{SectorTableHeader}}
{{SectorTableEntry|Name=Anisinza|Sector=7|System=90|XPos=19|YPos=14|Government=3|Economy=6|Techlevel=7|Population=3.4|Inhabitants=Human Colonists|Productivity=7616|Hubcount=2|Description=The world Anisinza is scourged by deadly disease.}}

{{SectorTableEntry|Name=Ceraso|Sector=7|System=113|XPos=13|YPos=1|Government=5|Economy=1|Techlevel=11|Population=4.7|Inhabitants=Fierce Yellow Fat Humanoids|Productivity=30456|Hubcount=3|Description=The planet Ceraso is reasonably fabled for its exciting sit coms and its inhabitants' exceptional love for food blenders.}}

{{SectorTableEntry|Name=Ceerti|Sector=7|System=62|XPos=1|YPos=9|Government=1|Economy=3|Techlevel=7|Population=3.5|Inhabitants=Fierce Slimy Frogs|Productivity=8120|Hubcount=2|Description=This world is a revolting little planet.}}

{{SectorTableEntry|Name=Gebeti|Sector=7|System=126|XPos=0|YPos=15|Government=1|Economy=7|Techlevel=2|Population=1.3|Inhabitants=Large Green Lizards|Productivity=1560|Hubcount=2|Description=Gebeti is very fabled for its ancient Gebetian Et banana plantations.}}
{{SectorTableFooter}}
[[Category:Sector7/Region|The Ceraso Hub]]
{{RoutesAndRegionsSector7}}
I have figured out how it works.

The image of the map has margins that it needs to make sure no star system is too close to the edge of the map. The click test image image on https://wiki.alioth.net/index.php/User:Treczoks is 774px x 390px (width x height). So assuming an even margin all the way round the margin value m must follow the equation (774 - 2m)/(390 - 2m) = 2 which gives a value of m = 3px. (If you are wondering why it equals 2, that is because the galaxy maps are 2 x 1 in shape.)

So there is an left-margin and top-margin for the image to the (0, 0) co-ordinate. 3px for both in this case. There would also be a right-margin and bottom-margin both 3px too.

By inspecting the page the clickable points are overlaid divs with dimensions 5px x 5px. So lets call this box to have box-width and box-height both equal to 5px in this case.

Taking the above into account and that the Y-axis is half the scale of the x-axis. Both co-ordinates for (XPos and YPos for the systems) range 0 to 256 each (therefore horizontal-range and vertical-range equal 256 each) from top left of the map then:

Code: Select all

XPosRel = XPos / horizontal-range * (width - left-margin - right-margin) + left-margin - ((box-width - 1) / 2)

YPosRel = YPos / vertical-range * (height - top-margin - bottom-margin) + top-margin - ((box-height - 1) / 2)
Given that the XPosRel and YPosRel are both integers in the examples on that page you may need to to round those calculated value to the nearest integer.

Looking at the image Cemave at the top has a YPos of 0 is at the fifth pixel from the top not 3 and Laeden at the bottom has a YPos of 254 and is at the fifth pixel from the bottom of the image. So It looks like the map has been shifted 2 pixels down to centre it vertically. There is likely to be a horizontal centring going on too so you would need to adjust the map width and height ranges (the horizontal-range and vertical-range) and margins accordingly. Please note that the values of XPosRel and YPosRel used by the page are not quite centred on the systems but as long as you get the ranges and margins correct my formulae above should work.

Phantor Gorth
Chat and relax with other commanders in the [url=irc://irc.oftc.net/oolite]DS's Seedy Space Bar[/url]. The Coolest Bar in the Eight.

Phantor's OXPs: [EliteWiki] GalCop Rewards and [EliteWiki] Safe Docking
User avatar
Cholmondely
Archivist
Archivist
Posts: 5364
Joined: Tue Jul 07, 2020 11:00 am
Location: The Delightful Domains of His Most Britannic Majesty (industrial? agricultural? mainly anything?)
Contact:

Re: Light year distance calculation

Post by Cholmondely »

PhantorGorth wrote: Tue Feb 22, 2022 6:43 am
Cholmondely wrote: Mon Nov 01, 2021 11:48 pm
Can anybody help with this, please?

Cody & I want to finish off this page and get it up on the wiki.

Code: Select all

== Map ==
[[Sector7/The Ceraso Hub (Region)|The Ceraso Hub]] consists of the systems [[Sector7/Anisinza|Anisinza]], [[Sector7/Ceraso|Ceraso]], [[Sector7/Ceerti|Ceerti]] and [[Sector7/Gebeti|Gebeti]].
This is the problematic bit: how on earth do I calculate the XPosRel & YPosRel values?

Code: Select all

<div style="position:relative">{{ClickMapHeader|Sector=7|Image=Sector7_The_Ceraso_Hub_RegionMap.png}}
{{ClickMapEntry|Name=Anisinza|Sector=7|XPos=19|YPos=14|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceraso|Sector=7|XPos=13|YPos=1|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Ceerti|Sector=7|XPos=1|YPos=9|XPosRel=???|YPosRel=??}}
{{ClickMapEntry|Name=Gebeti|Sector=7|XPos=0|YPos=15|XPosRel=???|YPosRel=??}}
{{ClickMapFooter}}</div>
I have figured out how it works.

The image of the map has margins that it needs to make sure no star system is too close to the edge of the map. The click test image image on https://wiki.alioth.net/index.php/User:Treczoks is 774px x 390px (width x height). So assuming an even margin all the way round the margin value m must follow the equation (774 - 2m)/(390 - 2m) = 2 which gives a value of m = 3px. (If you are wondering why it equals 2, that is because the galaxy maps are 2 x 1 in shape.)

So there is an left-margin and top-margin for the image to the (0, 0) co-ordinate. 3px for both in this case. There would also be a right-margin and bottom-margin both 3px too.

By inspecting the page the clickable points are overlaid divs with dimensions 5px x 5px. So lets call this box to have box-width and box-height both equal to 5px in this case.

Taking the above into account and that the Y-axis is half the scale of the x-axis. Both co-ordinates for (XPos and YPos for the systems) range 0 to 256 each (therefore horizontal-range and vertical-range equal 256 each) from top left of the map then:

Code: Select all

XPosRel = XPos / horizontal-range * (width - left-margin - right-margin) + left-margin - ((box-width - 1) / 2)

YPosRel = YPos / vertical-range * (height - top-margin - bottom-margin) + top-margin - ((box-height - 1) / 2)
Given that the XPosRel and YPosRel are both integers in the examples on that page you may need to to round those calculated value to the nearest integer.

Looking at the image Cemave at the top has a YPos of 0 is at the fifth pixel from the top not 3 and Laeden at the bottom has a YPos of 254 and is at the fifth pixel from the bottom of the image. So It looks like the map has been shifted 2 pixels down to centre it vertically. There is likely to be a horizontal centring going on too so you would need to adjust the map width and height ranges (the horizontal-range and vertical-range) and margins accordingly. Please note that the values of XPosRel and YPosRel used by the page are not quite centred on the systems but as long as you get the ranges and margins correct my formulae above should work.

Phantor Gorth
Alas, while I think I vaguely comprehend what you are doing (margins, et al) I've no idea as to how to use it (we non-programmers...).

By the way, I've put up a wiki user-page for you to mangle as you see fit.
Comments wanted:
Missing OXPs? What do you think is missing?
Lore: The economics of ship building How many built for Aronar?
Lore: The Space Traders Flight Training Manual: Cowell & MgRath Do you agree with Redspear?
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Light year distance calculation

Post by Switeck »

This comes up from time-to-time...
viewtopic.php?f=2&t=9729

Complete with the weirdness for edge cases:
viewtopic.php?f=3&t=17402

And even weirder edge cases when dealing with misjumps:
viewtopic.php?p=263285#p263285
viewtopic.php?f=2&t=8912&start=75#p128864
viewtopic.php?p=128025#p128025
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Light year distance calculation

Post by Switeck »

The 1st link:
viewtopic.php?f=2&t=9729
...has the best formula examples for both distances and times.

Up to you to mention the others or not...even this thread.
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: Light year distance calculation

Post by Cody »

Switeck wrote: Tue Jun 28, 2022 9:48 pm
And even weirder edge cases when dealing with misjumps:
You don't say!
I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
User avatar
Cholmondely
Archivist
Archivist
Posts: 5364
Joined: Tue Jul 07, 2020 11:00 am
Location: The Delightful Domains of His Most Britannic Majesty (industrial? agricultural? mainly anything?)
Contact:

Re: Light year distance calculation

Post by Cholmondely »

Cody wrote: Wed Jun 29, 2022 11:50 am
You don't say!
Have you seen? Alnivel fixed our problems with the regional map for the Lizard's Claw!
Comments wanted:
Missing OXPs? What do you think is missing?
Lore: The economics of ship building How many built for Aronar?
Lore: The Space Traders Flight Training Manual: Cowell & MgRath Do you agree with Redspear?
Post Reply