Page 5 of 5

Re: [MOD 0.17] VehicleSnap

Posted: Sun Sep 14, 2025 4:21 pm
by Zaflis
I'm not sure how i feel about the patch as in including it in or not. It's true the angles between cardinals and diagonals are off, i took a little screenshot of current rails:
rail-angles.png
rail-angles.png (1.55 MiB) Viewed 702 times
Your math is also something i don't follow :P
My thought after was to record those key angles into a table and then interpolate current angle to closest from the table. It'd just be a table of 16 numbers.

But i never considered this mod to actually use more than 8 rail directions, the extra angles between were just for immersion. I set it to 8 in mod settings myself.

Re: [MOD 0.17] VehicleSnap

Posted: Tue Oct 07, 2025 2:22 pm
by Egor
Zaflis wrote: Sun Sep 14, 2025 4:21 pm But i never considered this mod to actually use more than 8 rail directions, the extra angles between were just for immersion. I set it to 8 in mod settings myself.
Ok, you are using only 8 directions when playing the game.
Nevertheless the most popular user choice is 16, not 8.
So thank you for making your mod useful for all users, not only for users sharing your position.
"Snapping to Rail Angles" feature is just one more step to make your mod useful for everyone.
I believe it would be a valuable addition to VehicleSnap.

Do you remember your own description of the mod?
> Have you ever faced a problem when trying to drive exactly along a road, coastline or railroad, that car moves a tiny bit off to side?
You have mentioned "moving along a railroad" as what your mod was designed for.
So, it is quite natural for a mod user to expect that this feature will be in sync with Factorio rail system.

In my opinion, the angle (360°/16) does not have any practical value in the game.
All useful angles in Factorio equals to arctg(m/n) with some small integers m and n.
For example, the "half-diagonal rail" angle is not rail-specific: some of my defense structures have wall borders with the same angle (1 step north and 2 steps east, repeat).

Zaflis wrote: Sun Sep 14, 2025 4:21 pm Your math is also something i don't follow :P
I agree, my code is not a good code, it is not easy-to-understand.

My algo of calculating the snapping angle consists of three steps:
1. Calculate which 1/8 part of 360° the current orientation is inside. I used your own formula "multiply and round" here.
2. The current 1/8 consists of two UNEQUAL halves (1/16 each), so, direct angle comparison is used to determine which 1/16 is ours.
3. The current 1/16 consists of 2 equal 1/32 (or 3 equal 1/48, etc), so again using "multiply and round".
Zaflis wrote: Sun Sep 14, 2025 4:21 pm My thought after was to record those key angles into a table and then interpolate current angle to closest from the table. It'd just be a table of 16 numbers.
Yes, your approach would lead to a more-easy-to-understand code.
It looks like a good idea to write your own implementation instead of using my code as-is.

Re: [MOD 0.17] VehicleSnap

Posted: Tue Oct 07, 2025 2:31 pm
by BlueTemplar
Egor wrote: Tue Oct 07, 2025 2:22 pm [...]
In my opinion, the angle (360°/16) does not have any practical value in the game.
All useful angles in Factorio equals to arctg(m/n) with some small integers m and n.
For example, the "half-diagonal rail" angle is not rail-specific: some of my defense structures have wall borders with the same angle (1 step north and 2 steps east, repeat).
[...]
Zaflis wrote: Sun Sep 14, 2025 4:21 pm [...]
i took a little screenshot of current rails:
09-14-2025, 19-19-38.png
[...]
Given the above, it would be amazing if you re-posted that picture, but with show-tile-grid on !

Re: [MOD 0.17] VehicleSnap

Posted: Wed Nov 26, 2025 5:36 pm
by Zaflis
BlueTemplar wrote: Tue Oct 07, 2025 2:31 pm Given the above, it would be amazing if you re-posted that picture, but with show-tile-grid on !
I edited the previous post, it includes grid now. I need it myself too to find out actual angles, unless there is a list for those numbers somewhere?

I also did a test of the angle table with Unity editor and it worked very easily, and was actually quite cool to see.

Code: Select all

    int nearestI = -1;
    float nearest = 999f;
    for (int i = 0; i < lockaxis.Length; i++) {
      float d = Mathf.Abs(AngleDist(value, lockaxis[i]));
      if (d < nearest) {
        nearestI = i;
        nearest = d;
      }
    }
Looking to put out a new mod version of it, but there are some nuances to this. For example i don't want or need to table all the angles, only the 8 uncommon ones. I can then compare the best lockaxis-table value to the mathematically best axis and choose nearest, but that should only be used if player's mod settings are exactly 16 or 32 angles i think. Those between and before are divided differently altogether.

Current table i've gotten is roughly

Code: Select all

-- 0.07386350632 measured from train wagon orientation, still off very tiny bit
local rail_est = 0.07405 -- Best estimate so far 0.07405
local lockAxis = {
  rail_est,     0.25-rail_est, 0.25+rail_est, 0.5-rail_est,
  0.5+rail_est, 0.75-rail_est, 0.75+rail_est, 1.0-rail_est }
As it seems 1:2 is triangular ratio for the odd rail pieces, but math fails on this one ever-so-slightly and i had to go with trial and error repeatedly.

Re: [MOD 0.17] VehicleSnap

Posted: Fri Dec 12, 2025 9:39 pm
by Egor
Zaflis wrote: Wed Nov 26, 2025 5:36 pm

Code: Select all

-- 0.07386350632 measured from train wagon orientation, still off very tiny bit
local rail_est = 0.07405 -- Best estimate so far 0.07405
local lockAxis = {
  rail_est,     0.25-rail_est, 0.25+rail_est, 0.5-rail_est,
  0.5+rail_est, 0.75-rail_est, 0.75+rail_est, 1.0-rail_est }
As it seems 1:2 is triangular ratio for the odd rail pieces, but math fails on this one ever-so-slightly and i had to go with trial and error repeatedly.
Why estimating the angle instead of using its exact value?
As you have noticed, it is "1:2 triangular ratio", so in Lua it may be calculated as

Code: Select all

local rail_est = math.atan(1/2) / (2*math.pi)   -- approx 0.0737918

Re: [MOD 0.17] VehicleSnap

Posted: Fri Dec 12, 2025 10:15 pm
by Zaflis
Egor wrote: Fri Dec 12, 2025 9:39 pm Why estimating the angle instead of using its exact value?
As you have noticed, it is "1:2 triangular ratio", so in Lua it may be calculated as

Code: Select all

local rail_est = math.atan(1/2) / (2*math.pi)   -- approx 0.0737918
Because when i tried with 0.07386350632 earlier vs your 0.0737918, my earlier estimate was still larger number. The value 0.07405 that i came up was definitely better at driving along the rail line in those odd angles for long periods of time. Using smaller values caused several pixels more sway.
As the value i got was directly taking a train wagon orientation from the game itself when it is on that angle rail, so it was as exact for game as it can be, and yet it was not as good in practise. Something to do with how car behaves perhaps, how its drag is calculated, rounding or whatever...

Re: [MOD 0.17] VehicleSnap

Posted: Thu Dec 18, 2025 5:39 pm
by Egor
Indeed, in "Sandbox" mode, after driving forward for 10 km I found myself 10 m away from imaginary half-diagonal railway.

Re: [MOD 0.17] VehicleSnap

Posted: Thu Dec 18, 2025 10:17 pm
by Zaflis
Egor wrote: Thu Dec 18, 2025 5:39 pm Indeed, in "Sandbox" mode, after driving forward for 10 km I found myself 10 m away from imaginary half-diagonal railway.
It's impossible to get it accurate, you can try. The mathematical value for it would take you like 100m away at that distance.

Re: [MOD 0.17] VehicleSnap

Posted: Fri Dec 19, 2025 9:13 pm
by Egor
I have fixed the problem, see my patch on github. But again, it's hard-to-understand code.

The idea is following: at the moment when car orientation get snapped, we remember the trajectory (the "ray") which the car must follow in the future.
The ray is just 3 numbers: start_x, start_y and orientation.
Later, on every tick, we check the deviation between the ray and the current car position, and make attempt to return the car back to the ray by changing car orientation.
The bigger the deviation, the stronger the correcting attempt is.
If deviation is too big (above 0.025 tiles) we give up and forget the ray.
In practice, the car path is located inside a stripe 0.01-0.02 tiles width around the ray.

So, it works, "10 km test" passed!

Re: [MOD 0.17] VehicleSnap

Posted: Sat Dec 20, 2025 5:51 am
by Zaflis
That looks more like its own fork, not a patch ;) I wasn't thinking of changing to whole mod so much for this little topic. I don't mind you/others releasing forks if they want, would be kind to mention my mod though if it was used as a base though.

Yeah you can set a line towards a goal, but what if you predicted the orientation wrong initially? It's quite an odd angle there requiring arctan(), and even then not necessarily work as expected. If you say it works, sure...

Re: [MOD 0.17] VehicleSnap

Posted: Sat Dec 20, 2025 6:30 am
by Egor
Zaflis wrote: Sat Dec 20, 2025 5:51 amwhat if you predicted the orientation wrong initially?
How could it be predicted incorrectly?
There are only 16 correct orientations, and we know for sure which one of them was selected by the car driver.
While the car driver is changing car orientation manually, the mod stops modifying the orientation automatically, this logic is already implemented in your mod.
So, no problems are possible here.

I cannot publish the fork as I'm a freeloader - I don't own the game copy, so the factorio mods website is read-only for me.
And it is not a good practice to create a fork on every little improvement.
Users want a single mod instead of many different forks (how to merge them all to accumulate all the improvements?).
Fork is a right way to do only if the previous mod owner stopped supporting his mod.
But VehicleSnap is not abandoned yet :-)