What is the exact shape of a curved rail?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

Khagan wrote:
Thu Oct 26, 2023 5:50 am
They may perhaps be 'splines' in some generic sense of the term, but they are certainly not Bezier curves or anything closely related to them (e.g. Catmull-Rom), which is what is normally meant by the term in the context of computer graphics (and the way it has been used in this thread). The latter are polynomial curves (typically cubic), while the Euler/clothoid spiral involves transcendental functions (specifically, Fresnel integrals).
I would say splines are not curves themselves, rather method to generate them(the generic sense), different spline could yield the same curve with different control points and weights so i was wrong comparing them to clothoids which are curves themselves.

I understand that Bezier curves or Catmull-Rom woulnd't yield the exact same curves as a clothoid too ,what you are saying, but i don't know of at least 2 points :

1) Is it possible using certain transcendantal weight maybe to create a spline (not cubic nor catmull-rom, maybe degree k or something, or with a special conditions on linear derivatives) that could be used to generate "exact" clothoids segments ? That seemed difficult to find on the internet, maybe i was lacking keywords, or i just don't understand and can't recognize when i find something.

2)Another source of interrogation was for real life railroad, maybe those are not "perfect" euler/clothoid, maybe because of their transcendental nature the real life means of making train railroad is only an approximation due to them being built by segments of finite lengh and limited curvature precision or something. Maybe the real life process to make rail tracks is based on the clothoid for idealised design, but when it comes to actually making it as a physical item it is only an approximation. I was wondering then if there was a method to approximate those based on spline. In such case no longer to try and generate an "exact" clothoid, but something that could be made arbitrarily precise with an increasing number of control point.

I think this second research was more successfull , i found this article https://www.researchgate.net/publicatio ... ta_Fitting
which i could understand the introduction at least and it refers to railtracks amongst other things :)
Abstract
Euler spirals are a kind of fair curves that have linear curvatures in terms of arc length. However,
Euler spirals cannot be represented by polynomials exactly.
In this paper, we propose an algorithm
for approximating a segment of Euler spiral which interpolates two specified points and two tangents
at the points by a B-spline curve. The approximating B-spline curve is obtained as the solution to a
differential equation which satisfies the boundary conditions. Similar to the exact interpolating Euler
spiral, the interpolating B-spline curve is also fair and has approximate linear curvature. Furthermore,
an algorithm for fitting data points by a sequence of smoothly connected fair B-spline curves is also
developed
Given the amount of sources of the paper i assume this is a documented quite recent topic, and i read the balden sentence as a confirmation of your quote.
Further in the article they mention other existing ways of approximating the spiral some using splines and later what's detailled in the article is a method of approximation using B-splines but that's difficult to understand for me.
Due to complicated transcendental function representation of Euler spirals, it is difficult or
expensive to compute with Euler spirals. In particular, for rendering purpose, Euler spirals are
often approximated by other discrete or continuous curves [9] [10]. In [2], a segment of Euler
spiral is approximated by a B´ezier curve using Taylor expansion but this approximation is limited
in the tangent angle interval [0, π/2].
When an Euler spiral is approximated by an s-power series
[11], it yields a polynomial form but the calculus of the coefficients is complicated. Euler spiral
can also be approximated by an arc spline
[3], but the obtained curve is not curvature continuous.
B-spline curves and surfaces are widely used in CAD/CAM systems and they can be used as an
efficient tool for finding the solutions to differential equations [14]. This paper presents a strategy
for approximating Euler spirals which interpolate specified points and tangents by B-spline curves
It doesn't mean i can understand the presented strategy, but it's quite a recent paper, that would explain why there is no vulgarization of fancy youtube videos yet. I found the easier way to vizualize/begin understanding is to think about the curvature, and the rate at which it changes, meaning its derivative rather than the complicated method to generate them. Because seeing the differences in the derivative is accessible to me , whereas reading the formula with the polynomials i can't make sense of them without the plotting step, which i couldn't do without understanding the formula, which i couldn't do without seeing them plotted first .... :cry:

The more i learn the more mysterious the potential exact shape of a curved rail becomes :)

edit : The looping in roller coasters are called "clothoid loop", this is the most accessible i found x) https://www.youtube.com/watch?v=3Kzl2suBE2w

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

A spline in this context is a piecewise polynomial function, where the value and some derivatives are typically constant across transitions. It is normally specified as a linear combination of control points, the combination changing with time. The details matter more for modeling than the nature of the underlying curve. A Bézier curve is a special case of a B-spline. A B-spline can be split into Bézier curves. They can all be cubic or have any other degree. Other types of splines share the underlying structure in one form or another. I happened to use cubic Bézier curves, with intermediate control points generated as an offset from the endpoints.

I would say a non-uniform rational B-spline (NURBS) is the most a spline can be. Control points form a convex hull of the curve. It can be evaluated very stably using only recursive interpolation. Control points and knots cover the degrees of freedom exactly, including cases like double knots. It can be left open-ended or it can have multiple knots to have ends like a Bézier curve. I’m a little conflicted about the rational part because it’s orthogonal to the spline itself. You could feed homogeneous control points through a weight-agnostic spline to the same end. Regardless of how you characterize that, one thing it allows you to represent is an exact circular arc. The catch is that you lose speed constancy.

Splines are still somewhat relevant to Euler spirals. Even if the curve itself can only be approximated, the changing curvature can be expressed as a linear spline. A spline could cover any length of track including straights, curves and transitions. You wouldn’t really do this, not as a base specification because of the instability of integration, but it’s worth drawing the comparison.

User avatar
Khagan
Fast Inserter
Fast Inserter
Posts: 233
Joined: Mon Mar 25, 2019 9:40 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by Khagan »

quale wrote:
Thu Oct 26, 2023 2:59 pm
A spline in this context is a piecewise polynomial function
I agree that 'piecewise' is a important distinguishing feature. Typically they are not of interest for their own sake, but as approximants or interpolants.

The answer to
mmmPI wrote:
Thu Oct 26, 2023 11:41 am
1) Is it possible using certain transcendantal weight maybe to create a spline (not cubic nor catmull-rom, maybe degree k or something, or with a special conditions on linear derivatives) that could be used to generate "exact" clothoids segments ?
is a firm 'No', but the answer to
2)[...] I was wondering then if there was a method to approximate those based on spline. In such case no longer to try and generate an "exact" clothoid, but something that could be made arbitrarily precise with an increasing number of control point.
is 'Yes, in principle', and the article quoted above seems to be a fairly serious attempt to turn that into a practical 'Yes'.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

Khagan wrote:
Fri Oct 27, 2023 12:24 am
quale wrote:
Thu Oct 26, 2023 2:59 pm
A spline in this context is a piecewise polynomial function
I agree that 'piecewise' is a important distinguishing feature. Typically they are not of interest for their own sake, but as approximants or interpolants.
Thank you for highlighting what i missed, those 2 words word i didn't realize were to be understood with a precise mathematical meaning. It also didn't occured to me that splines are based on linear interpolation and if you try to piece them together it would always yield polynomial function which by definition are not transcendental, which the Euler's spiral is.
quale wrote:
Thu Oct 26, 2023 2:59 pm
I would say a non-uniform rational B-spline (NURBS) is the most a spline can be. Control points form a convex hull of the curve. It can be evaluated very stably using only recursive interpolation. Control points and knots cover the degrees of freedom exactly, including cases like double knots. It can be left open-ended or it can have multiple knots to have ends like a Bézier curve. I’m a little conflicted about the rational part because it’s orthogonal to the spline itself. You could feed homogeneous control points through a weight-agnostic spline to the same end. Regardless of how you characterize that, one thing it allows you to represent is an exact circular arc. The catch is that you lose speed constancy.
You lost me there at 'i'm a little conflicted' ... Looking at NURBS on wikipedia didn't helped me compared to the earlier 1h video on splines where they are mentionned at the end amongst the 'curiosity' for lack of a better word from me. I read in the previous article that one way to work with Euler's/Clothoid was to approximate them using "arc spline". But it didn't know what it was, the rest of the paper is about approximation using B-splines. I supposed it's because it didn't occured to me that you can draw circle without using sin or cos, just by writing the equation under the general form. x²+y²+ax+by whereas for the euler's spiral it is required to use sin and cos function or sqrt(x), which then disqualify it as a polynomial function.

This 25 sec video of a drawing of Euler's spiral with the formula made it clear to me https://www.youtube.com/watch?v=ziaV0-uaaGga after several watch at 0.25 speed, although the trajectory of the 2 points that are the "center of the curvature" could have been highlighted for even better clarity. I don't have in mind the formula of the splines and maybe couldn't recognize they could be written under an other form by myself, though i have in mind the geometric interpretation from those videos. ( which makes it hard to realize what i see is the result of a polynomial functions ). Splines are the result of LINEAR interpolation, whereas for Euler's spirals drawing, the points used to generate the curve wouldn't move in a line, but would wiggle due to them being located based on functions that include cos and sin.

Most of what i could find for NURBS was in 3D which i suppose it what you called orthogonal to the spline.( the rationnal part) It's interesting in itself, but in relation to the shape of the curved rail, i think it's illustrating "the more you learn, the more you realize there are things you know nothing about" to me. I can't really gauge the likelyhood that the approximation using exotic splines you are refering to could have been used to shape the rail. (Nurbs or the later one) . You seem to consider that it wouldn't be fit for a programming approximation of curved rail due to the induced imprecision in the method to math out the distance ? (following quote)
quale wrote:
Thu Oct 26, 2023 2:59 pm
Splines are still somewhat relevant to Euler spirals. Even if the curve itself can only be approximated, the changing curvature can be expressed as a linear spline. A spline could cover any length of track including straights, curves and transitions. You wouldn’t really do this, not as a base specification because of the instability of integration, but it’s worth drawing the comparison.
At least what i'm getting there is that as a form of generalization if Factorio was made with curved rail based on Euler's spiral transition to mimick real life railroad , then doing it with spline approximation wouldn't make as much sense as just using splines for another kind of approximation of the curve itself, not an approximation of an euler's spiral curved rail, as the splines one could use for that latter case are not as easy to use in programming compared to the former spline approximating an arc circle ( those you plotted ) ?
Khagan wrote:
Fri Oct 27, 2023 12:24 am
is 'Yes, in principle', and the article quoted above seems to be a fairly serious attempt to turn that into a practical 'Yes'.
Well, i'm glad i had not everything wrong :) but "it exist maybe" doesn't mean it's what was used in factorio, especially if it's not something known as robust, i don't think one when doing trains in factorio was thinking it has to be realistic absolutely, rather a mix of illusion of realism and performance.

I found this video https://www.youtube.com/watch?v=CCpi0eNks94 that defines points to measure the transition in the context of highway aligment made with euler's spiral transition, the example piece is looking like a curved rail in factorio. It's not splined-based approximation, it's maybe more akin to what one would find in real-life approximation.

But unlike in factorio, it is a transition from say an horizontal line to a vertical line ( 1/4 circle), using 3 segments only, ( spiral-transition __ piece of arc circle__spiral transition) whereas factorio would be using 5 ( [straight-horizontal][curvy-transition-to-diagonal][diagonal-track][curvy-transition-away-from-diagonal][straight-vertical]). This not looking at the rail piece but the overall shape of the 1/4 circle. The curved rail itself being composed of [straight-horizontal][curvy-transition-to-diagonal-diagonal][diagonal-track], while that last [diagonal-track] extend over 3 different rail pieces in a 1/4 factorio rail circle.

Where i'm going with all this is that , the way i found clothoid transition done on the internet wouldn't yield the same kind of curve that we find in factorio,( segmentation of 1/4 circle doen't include diagonal tracks segment unless you use 4 transitions and not just 2 as in the example) but it gives relations between distances and name the interesting points, and explain how to locate points along such spiral transitions using the so called deflection angle. This is an approximation though not sure if it's fit to make a model for rail that would be performant, precise and somewhat realistic, in a videogame, despite those curves being those for real life railroad. The other method to locate point along such transition from x to y or the reverse i don't think are those needed to compute a train distance travelled along such curves but are more precise involving sqrt(x) though.

I'm not sure this is going to the direction of a more precise definition of the shape of the curved rail but on the other hand, boskid is not usually shy of technical details, and using the word "crazy math" to sum it up means to me that it is going to be particularly difficult to find the exact method that yielded the precise lengh of the curved piece, maybe impossible, but it's interesting to learn all those things along the way :)

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

mmmPI wrote:
Sat Oct 28, 2023 12:39 pm
Most of what i could find for NURBS was in 3D which i suppose it what you called orthogonal to the spline.( the rationnal part) It's interesting in itself, but in relation to the shape of the curved rail, i think it's illustrating "the more you learn, the more you realize there are things you know nothing about" to me. I can't really gauge the likelyhood that the approximation using exotic splines you are refering to could have been used to shape the rail. (Nurbs or the later one) . You seem to consider that it wouldn't be fit for a programming approximation of curved rail due to the induced imprecision in the method to math out the distance ? (following quote)
I meant orthogonal in that they are separable concepts. Splines can live on their own. Homogeneous coordinates can live on their own. Using a spline with homogeneous coordinates automatically makes it a rational spline. They combine perfectly but there is no substance binding them.

Whatever Factorio does has to be super simple, computationally speaking. Splines definitely fit the bill. The core of the position function can be a mere ((a*t+b)*t+c)*t+d, dwarfed by surrounding logic. They and not much else fit boskid’s description: single continuous function fitting arbitrary control points, speed constancy violation, ramp split into multiple segments while implying that it could be just one.
mmmPI wrote:
Sat Oct 28, 2023 12:39 pm
quale wrote:
Thu Oct 26, 2023 2:59 pm
Splines are still somewhat relevant to Euler spirals. Even if the curve itself can only be approximated, the changing curvature can be expressed as a linear spline. A spline could cover any length of track including straights, curves and transitions. You wouldn’t really do this, not as a base specification because of the instability of integration, but it’s worth drawing the comparison.
At least what i'm getting there is that as a form of generalization if Factorio was made with curved rail based on Euler's spiral transition to mimick real life railroad , then doing it with spline approximation wouldn't make as much sense as just using splines for another kind of approximation of the curve itself, not an approximation of an euler's spiral curved rail, as the splines one could use for that latter case are not as easy to use in programming compared to the former spline approximating an arc circle ( those you plotted ) ?
If I wanted to extend a road to connect to another road in another town, would I describe it as 2.3 km straight, 27° right, 3.7 km straight, 15° left, 2.6 km straight? Nowhere did I say where it actually is beyond the initial condition. Any error or shortcoming in my specification or your evaluation will cause you to miss the mark. The longer the road is, the greater the error will be. It’s a useful description of what it’s like to drive on the road. It’s an important criterion in the road’s design. In a perfect mathematical world, it’s all you need. That’s the level at which the spline from my quote exists. But for actual construction, the road will be specified with direct reference to locations.
mmmPI wrote:
Sat Oct 28, 2023 12:39 pm
I found this video https://www.youtube.com/watch?v=CCpi0eNks94 that defines points to measure the transition in the context of highway aligment made with euler's spiral transition, the example piece is looking like a curved rail in factorio. It's not splined-based approximation, it's maybe more akin to what one would find in real-life approximation.
The curves are split into segments for very different reasons. Clothoids exist to ease in and out of curves. Factorio doesn’t have those. Its trains are fine jolting around as long as they’re fun to watch. Curves are split to give you more freedom with its modular prefabs. The segments deliberately don’t only go together one way. The 1.1 curve is split internally to allow its endpoints to line up with the grid.

There is one more curiosity I thought about. Both the old and the new pieces will exist during a transitional period. That means five-way junctions can exist. If you’re driving a train in manual mode, where will it go with its choice of three directions? You could effectively lock manual trains out of the network. Another funny consequence is that diagonal rails can be placed with half of the usual spacing. I presume that will only cause collisions, but hey.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

quale wrote:
Tue Oct 31, 2023 3:20 pm
I meant orthogonal in that they are separable concepts. Splines can live on their own. Homogeneous coordinates can live on their own. Using a spline with homogeneous coordinates automatically makes it a rational spline. They combine perfectly but there is no substance binding them.

Whatever Factorio does has to be super simple, computationally speaking. Splines definitely fit the bill. The core of the position function can be a mere ((a*t+b)*t+c)*t+d, dwarfed by surrounding logic. They and not much else fit boskid’s description: single continuous function fitting arbitrary control points, speed constancy violation, ramp split into multiple segments while implying that it could be just one.
No wonder why you lost me there, homogeneous coordinates is an unknown concept to me. And the quick research shown no relations to things i've used or done in the past.

I understand this is what boskid said about the new rails, but i was still on the old rails, wondering how did the lengh was defined for the curved section and why the diagonal and straight portion of the curved rail have their current (1.1) lengh.

I understood that you plotted cubic splines to illustrate what would spline used for the 2.0 rail would yield as curve but they were not identical to the shape we were shown in the FFF, at least 1 yellow curve standed out but maybe i got that wrong ? There may be slight difference in the way the 2.0 curve was designed but still using spline ? as they seem to fit the description of what was used. Is there still something that doesn't connect ? the lengh of the 2.0 rails are known, aren't they all fitting with your initial plot/spline 'guess' to answer your own question from the first post ?
quale wrote:
Thu Oct 26, 2023 2:59 pm
If I wanted to extend a road to connect to another road in another town, would I describe it as 2.3 km straight, 27° right, 3.7 km straight, 15° left, 2.6 km straight? Nowhere did I say where it actually is beyond the initial condition. Any error or shortcoming in my specification or your evaluation will cause you to miss the mark. The longer the road is, the greater the error will be. It’s a useful description of what it’s like to drive on the road. It’s an important criterion in the road’s design. In a perfect mathematical world, it’s all you need. That’s the level at which the spline from my quote exists. But for actual construction, the road will be specified with direct reference to locations
again i'm not sure i'm following you here, I meant with the sentence you answered to, that it's unlikely that 1.1 rail shape would have been made with a spline to approximate and euler's spiral transition, because making 1.1 rail shape using a spline to approximate 1/4 circle is more computer-friendly and the euler's spiral transition although may explain the "crazy math" boskid mentionned when refering to the 1.1 rails and be "more realistic" is overall not worth the trouble but we still don't know the exact math that yielded the 1.1 curve rail right ?

What you describe i understand as relating the complexity to build a road or a highway, in real life, using the notion of linear change in curvature ( what is the delta that should trend toward infinitly small ? ) rather than x-y approximated coordinates ( absolute coordinate for arbitary number of points in the curve approximated individually with non-conpounding error ) . One being the idealized curve from a driver referential in terms of speed and ease to drive (and also the definition of the eurler's spiral), the other, the method to buid this curve in real life possibly using different methods such as approximations so you don't have to build the curve progressively from a to b but can start the work at several places at once.

I'm not sure how it relate to the possibility to compute precisely enough for factorio simulation the distance travelled along the curves.

quale wrote:
Thu Oct 26, 2023 2:59 pm
The curves are split into segments for very different reasons. Clothoids exist to ease in and out of curves. Factorio doesn’t have those. Its trains are fine jolting around as long as they’re fun to watch. Curves are split to give you more freedom with its modular prefabs. The segments deliberately don’t only go together one way. The 1.1 curve is split internally to allow its endpoints to line up with the grid.
I get this. But at the same time, the 1.1 rail has a segment whose shape is a curve that's lining with the grid in such a way that its lengh is difficult to compute back even knowing the distances right ?
quale wrote:
Thu Oct 26, 2023 2:59 pm
There is one more curiosity I thought about. Both the old and the new pieces will exist during a transitional period. That means five-way junctions can exist. If you’re driving a train in manual mode, where will it go with its choice of three directions? You could effectively lock manual trains out of the network. Another funny consequence is that diagonal rails can be placed with half of the usual spacing. I presume that will only cause collisions, but hey.
I'm still not sold on the exact shape of the curved rail x)
nor 1.1, how did the lengh of the 3 segment was computed ?
nor 2.0 because your plot seem to show a variation ? but maybe i didn't got properly that one curve is just the illustration of taking a 12:5 curve instead of the one picked for 2.0 as that would give a closer approximation to the real arc circle, although not as flexible for gameplay/making modular rail piece ?

I found another article which i found is linking well the formulas the explanations and the diiferent plots and end up with another way of approximating the Euler's spiral for computer, for "curiosity", https://xixixao.github.io/euler-spiral-explanation/. It is showing the formula and visual for an approximation using Taylor series, since it was mentionned in the previous article.

I suppose that the "old" rails will not be used when someone is using the keyboard for manual driving during the transition period so if you are in a 5 way split, if you pick "right", it will not go for full diagonal instead only half diagonal = You would go on the larger circle if there is 2 tangent circle and you enter them from the tangente because the newer rail are creating larger radius circle.

I don't understand the half usual spacing for diagonal, isn't that already the case that you can place a rail in each diagonal instead of 1/2 because sometimes it will create a "bottom oriented triangle" and sometimes the mirror version ?

User avatar
Khagan
Fast Inserter
Fast Inserter
Posts: 233
Joined: Mon Mar 25, 2019 9:40 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by Khagan »

mmmPI wrote:
Tue Oct 31, 2023 8:33 pm
homogeneous coordinates is an unknown concept to me.
Welcome to the wonderful world of projective geometry!

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

I’m getting a little bogged down by the quoteception. It’s hard to consolidate when an idea derails and that derailment sparks its own derailments, all while trying to move forward as well.
mmmPI wrote:
Tue Oct 31, 2023 8:33 pm
I understand this is what boskid said about the new rails, but i was still on the old rails, wondering how did the lengh was defined for the curved section and why the diagonal and straight portion of the curved rail have their current (1.1) lengh.
It turns out that 1.1 rail sprites are square-based, even if completed rails don’t look that way. That means that especially the diagonal end of a curve needs to be straight to line up properly regardless of what connects to it. The gauge constant of 35/64 = 0.546875 might be related to that. If you divide it by √2, you do indeed get the diagonal segment length to 13 significant digits. The rest is filled by maximizing the circular arc.
mmmPI wrote:
Tue Oct 31, 2023 8:33 pm
I understood that you plotted cubic splines to illustrate what would spline used for the 2.0 rail would yield as curve but they were not identical to the shape we were shown in the FFF, at least 1 yellow curve standed out but maybe i got that wrong ? There may be slight difference in the way the 2.0 curve was designed but still using spline ? as they seem to fit the description of what was used. Is there still something that doesn't connect ? the lengh of the 2.0 rails are known, aren't they all fitting with your initial plot/spline 'guess' to answer your own question from the first post ?
I only used control points from 1.1 and 2.0—positions with directions—and took my own liberty filling in the gaps. You’ve seen the fruits of that. Good and similar but not an exact match. I didn’t think it would be productive to look too deeply into something we can’t even access.

What is this deviation you’re talking about? How my blue and orange curves are not the same? Both are my own fabrication. Blue uses a 2:1 endpoint direction so it can properly connect to a straight piece. That is my actual proposal. Orange uses 12:5 for the sake of showing how closely the spline can match an actual circular arc, maintaining a constant speed. Its g plot is visually perfectly flat. I overlapped it first of all because it was a modification of blue. Then I left it there to contrast it against blue. It’s not an error. It’s there to illustrate my narrative from that post.

I did think about 2.0 lengths now. They pretend to constitute a perfect circle of radius 13. The curves have lengths 13 arctan(5/12) and 13 arctan(7/17), matching all given digits. Especially the latter deviates from the actual curve by a relatively large amount (1% over my spline), which would be part of another explanation for violation of speed constancy. Such a barefaced lie clearly doesn’t truthfully represent the position function. It does hint that the circle played a central role. That may or may not say something about the position function.

The next step is to plot over images. The final shape image from FFF 377 is very clean and high-res. The 2:1 direction is evidently correct. It would be really weird if it weren’t. The blue cardinal-adjacent spline is very close already. Bumping its presumed length slightly to 5.25 makes it visually perfect. The red ordinal-adjacent spline is lacking. It does become visually perfect when I drop its presumed length to 4.75, severely compromising speed constancy. I could tweak these slightly better still by adjusting the intermediate control points separately. Of course things might differ under the hood, splines being magnificent shapeshifters.

One thing coming out of this is that my own splines have been doing a stellar job compared to these observations from 2.0. Who even wants Factorio? Heh. We’ll see, though. Some of this is obviously speculative.
mmmPI wrote:
Tue Oct 31, 2023 8:33 pm
I don't understand the half usual spacing for diagonal, isn't that already the case that you can place a rail in each diagonal instead of 1/2 because sometimes it will create a "bottom oriented triangle" and sometimes the mirror version ?
The grid is displaced between the old and new diagonals.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by boskid »

After some consideration i do not think describing exact position functions would do anything bad, i would actually like a nice compact spline to exist that would match existing position functions as it would allow me to get rid of Math::sin and Math::cos usage.

Curved-A rail has position function directly based on Math::sin and Math::cos where the angle is given by distance/Radius, where Radius=13 and it multiplies the sin/cos results by 13

Curved-B rail has position function directly based on Math::sin and Math::cos where the angle is given by (5.132284556 + distance)/Radius where Radius=13. Value of sin/cos is multiplied by an "effective radius" which is a linear function of the distance, at the half-diagonal end it has value of 13 and on the diagonal end it has value of 9sqrt(2) so it behaves as if the circle was shrinking in radius as the position moves along the circle.

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

boskid wrote:
Wed Nov 01, 2023 2:40 pm
After some consideration i do not think describing exact position functions would do anything bad, i would actually like a nice compact spline to exist that would match existing position functions as it would allow me to get rid of Math::sin and Math::cos usage.
If you did fit a spline to the function, it would hardly differ. They’re really good at this. The glaring thing is that you’ve thrown C^1 smoothness out of the window (most rail joints involving a curve change both speed and direction instantly), along with an attempt at preserving speed within the curve. Whatever slight error you add on top of that is nothing.

I made another mockup in Desmos. Found you can use images and custom colors. Red is your function. White is my original spline (not trying to match your function; that would be much closer) on top. I’m not sure either one is a great visual match. Really depends on the specific piece. Not that anyone would really notice the difference in practice.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by boskid »

It would be a no brainer for me to change used reference function f(x) into a spline g(x) or anything else g(x) if |f(x) - g(x)| < 0.01 tile for every x from 0 up to rail length. Differences above 0.01 up to 0.05 i would have to test in game to verify if they look correct. Above 0.05 it is a clear no for me. Most likely the thresholds would be slightly higher for a difference that is along the curve, but if its too far to the side of a position function it would be really easy to notice the wheels not aligning with rails.

User avatar
Khagan
Fast Inserter
Fast Inserter
Posts: 233
Joined: Mon Mar 25, 2019 9:40 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by Khagan »

boskid wrote:
Wed Nov 01, 2023 2:40 pm
i would actually like a nice compact spline to exist that would match existing position functions as it would allow me to get rid of Math::sin and Math::cos usage.
You can get a exact circular shape without cos and sin by using a rational quadratic spline. But the spline parameter is not proportional to path length, so keeping the speed constant would be tricky.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by boskid »

Btw interesting tool. Raw rail ramp position function is quite easy to draw, it does not preserve speed but that does not matter as it is resampled and then interpolated so the parameter speed is correct (this is why the ramp length is not 16 but slightly more)

Code: Select all

f_{clamp}\left(v,d,u\right)=\min\left(u,\ \max\left(d,\ v\right)\right)
\left(15+16\cdot t,\ 3.5+1.5\sin\left(\frac{f_{clamp}\left(16t-8,-7,7\right)}{7}\cdot\frac{\pi}{2}\right)\right)

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

boskid wrote:
Wed Nov 01, 2023 7:39 pm
It would be a no brainer for me to change used reference function f(x) into a spline g(x) or anything else g(x) if |f(x) - g(x)| < 0.01 tile for every x from 0 up to rail length. Differences above 0.01 up to 0.05 i would have to test in game to verify if they look correct. Above 0.05 it is a clear no for me. Most likely the thresholds would be slightly higher for a difference that is along the curve, but if its too far to the side of a position function it would be really easy to notice the wheels not aligning with rails.
It’s getting a little weird giving advice to the devs. Yes, that’s easily possible. I got C^0 within 0.0002. A C^1 fit (determined entirely by the endpoints of the original function and its derivative), which doesn’t help much because the original functions violate it between each other, is still within 0.0009. It’s hard to overstate how good these things are. The merits of the original functions and fixed-point quantization are far more significant.

I made some more plots. I included a polynomial expansion of one of the curves, adjusted so t goes up to the curve length instead of 1, to show how it can be evaluated very cheaply. The CPU doesn’t care about daunting precomputed constants. :)

Edit: there was a slight mistake in the original link, corrupting the original A function. It didn’t affect anything else and has been fixed.
Last edited by quale on Thu Nov 02, 2023 6:34 pm, edited 1 time in total.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

Khagan wrote:
Tue Oct 31, 2023 10:00 pm
Welcome to the wonderful world of projective geometry!
Oh, i've heard of ballistic before, but im not sure the train can be considered a projectile.
( sorry couldn't resist the joke let's not go into further digression x) ).
quale wrote:
Thu Nov 02, 2023 1:23 pm
It’s getting a little weird giving advice to the devs.
No it's not, i do it plenty of times when asked for thoughts in the FFF, other players too , "giving" advice is not weird, it shows interest, though the process through which they are received and extrapolating from myself as example, there has to be a lot of uninformed opinion because there's so many different things in a video game and my knowledge is so small in comparaison, statistically what's weird is that you seem to know what you are talking about :D

More seriously it's quite comon for forum user to share knowledge and boskid was a forum user before being a dev :) , that was announced in an FFF ! Unfortunatly not all forum members are good programmer, or known things about mathemathics, so if you want ME to understand you'd have to give much more detailed explanations than just plots, which you did and i'm thankful. I'm sorry for the quoteception, i tried my best to focus on the title of the thread but also took the opportunity to ask question on things i am not knowleadgeable about and found interesting which could explain why the approach looks erratic.
quale wrote:
Wed Nov 01, 2023 1:13 pm
I only used control points from 1.1 and 2.0—positions with directions—and took my own liberty filling in the gaps. You’ve seen the fruits of that. Good and similar but not an exact match. I didn’t think it would be productive to look too deeply into something we can’t even access.
That was not clear to me sorry for the confusion, i was seeing the plots but wasn't understanding anything so i thought to watch the video about splines to try and understand, and from there it was only new doors to the unknown that i didn't know even existed before.

Then i was always more or less confused between what you thought was 1.1 what you wanted to know about 1.1 and 2.0 and what you thought 2.0 could be and what the plots were representing. And as all this is new to me i have hard time connecting the dots between the mathematical concept and the actual plots with the parameters that make sense and what they represent. I'm not going to pretend i understand everything now, but your explanations clarified what i was looking at, i will give it a second third and more thought because it's still unclear how you go from the general concept of spline, to choosing one and choosing the parameter so that it creates the curves from the game. Though maybe that's because i can't speak the gamedev langage. ( i have noticed i can only understand around 40% of your messages boskid i'm sorry, i need to get more math lesson ).

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

mmmPI wrote:
Thu Nov 02, 2023 5:21 pm
Oh, i've heard of ballistic before, but im not sure the train can be considered a projectile.
Let me tell you about the sport of peep bowling!
mmmPI wrote:
Thu Nov 02, 2023 5:21 pm
quale wrote:
Thu Nov 02, 2023 1:23 pm
It’s getting a little weird giving advice to the devs.
No it's not, i do it plenty of times when asked for thoughts in the FFF, other players too , "giving" advice is not weird, it shows interest, though the process through which they are received and extrapolating from myself as example, there has to be a lot of uninformed opinion because there's so many different things in a video game and my knowledge is so small in comparaison, statistically what's weird is that you seem to know what you are talking about :D
People often try to flesh things out at a level of detail they don’t understand in the least. That’s one thing. To go from a question of my own to effectively being commissioned for free is another.
mmmPI wrote:
Thu Nov 02, 2023 5:21 pm
i will give it a second third and more thought because it's still unclear how you go from the general concept of spline, to choosing one and choosing the parameter so that it creates the curves from the game. Though maybe that's because i can't speak the gamedev langage. ( i have noticed i can only understand around 40% of your messages boskid i'm sorry, i need to get more math lesson ).
It certainly isn’t common knowledge. Splines are pretty intuitive, though. Have you ever manipulated a Bézier curve in a graphical editor? You can drag control points with quite predictable results. The visual construction of Bézier curves is also quite pleasing. It can be an art like that. But it’s also a science. The C^1 drop-in replacements followed entirely from their nature, only waiting to be found. I like those junctions where hard science is also appealing.

mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

quale wrote:
Fri Nov 03, 2023 1:35 pm
mmmPI wrote:
Thu Nov 02, 2023 5:21 pm
Oh, i've heard of ballistic before, but im not sure the train can be considered a projectile.
Let me tell you about the sport of peep bowling!
That's where i learned about ballistic ! But i didn't know one could use a train too, thanks you for providing the useful keywords to find other practitionners. I'm attaching my own experiment for reference :
Bellysta
quale wrote:
Thu Nov 02, 2023 1:23 pm
People often try to flesh things out at a level of detail they don’t understand in the least. That’s one thing. To go from a question of my own to effectively being commissioned for free is another.
I don't know, on forums it's possible to have interactions with people from very different background and generation without knowing which leads to difficulty to find a level of technicality in the discussion that is suitable for the different member's understanding and ease of expression. When reading your initial question it was possibly as you describe someone trying to flesh things out with formulas and numbers, but further discussions leads to deeper and deeper definition quickly which eventually got noticed and answered by dev. You got your answer for free but it took a little time to establish that you wouldn't be overwhelmed by the answer by some back and forth with someone like me or other more knowledgeable forum member that would try to help for free too, and if such interaction can yield some observations/agreement that can yield positive implications for the Factorio game all i can see is some positive. But that's necessary somehow if you want to be understood and you do something which is not comon knowledge, that you have to explain for the others to agree.

Now of course you don't need everyone to agree/understand, if it's me, i can't commission you for explaining to me in a way that i would understand, that may be beyond anyone teaching's skill, no offense to your math skills , your original question didn't mention splines, it was introduced in the discussion, i feel interested i try to follow and document, i also ask questions, in case you are happy to discuss something you like i can provide 1 interested reader in between 2 session of space exploration :)
quale wrote:
Thu Nov 02, 2023 1:23 pm
It certainly isn’t common knowledge. Splines are pretty intuitive, though. Have you ever manipulated a Bézier curve in a graphical editor? You can drag control points with quite predictable results. The visual construction of Bézier curves is also quite pleasing. It can be an art like that. But it’s also a science. The C^1 drop-in replacements followed entirely from their nature, only waiting to be found. I like those junctions where hard science is also appealing.
I've watch the 1 hour video several time, and the other one on the bézier curves, i've used them in graphical editor or games not knowing they were splines and the formula associated with the different icons to change how the control point interact with the curves, i could connect the dots while seeing the videos, but apart from occasionnal hobby project or things related to factorio it must be 10 years that i haven't plotted things with their XY coordinates, let alone something more complex. I think last time i needed those math was to make a character move in more than 8 direction in some javascript game i made as pretext to practice those math i like to learn nowadays. I can clearly see the potential use for spline, for the animation of the character, i was already using lerps, which i learned about doing that project. But this is a step further and is related to a real game i play a lot :)

I've done some pretty extensive research on traffic flows along the years of playing videos games, as the underlying principles applies in many games and in real life ( trains or car or people on path even molecules in pipes or datas ) it gives supports for memorization of difficult concepts , but it seem quite an unpopular topic of discussion at parties so i keep it mostly on video games forum :D Science so vast one gotta be a newb almost everywhere , if discussion aren't focussed they bound to be of low level of technicality or super long so that most people understand most things. As much as i like science and learning new things i wouldn't to makes things extra slow by requiring extra explanations, but i'd appreciate being directed toward ressources that someone with more knowledge could identify as providing means for me (or others that may read the discussions later ) to reduce the gaps of understanding, if you don't mind sharing a few of those places where hard science can be fun to watch without being harmful :)

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by boskid »

So in the end polynomials were so good that i decided to switch to them instead of using sin/cos, divisions etc because that simplifies generated code while keeping the accuracy good enough. MapPosition's resultion is 1/256th which means errors below 0.001 are just nothing. In order to reduce more operations i also decided to embed the rail length inside of the polynomial coefficients because that lets me avoid one extra multiplication. I decided to perform some model identification using ARX approach with Chebyshev nodes also ignoring that ends of the position function are slightly off because that does not matter due to MapPosition's resolution and i managed to get even smaller numeric errors (which also does not matter). For visualizations i was adding my functions on top of https://www.desmos.com/calculator/8cecvnru9y

For Curved-A rail i got those:

Code: Select all

  static constexpr double xcoef[4] = { -0.0001006957601695,  0.0006303089061514, -0.0390775228531406, 0.0001929289349940 };
  static constexpr double ycoef[4] = {  2.0000180760089301, -1.0001059650840689,  0.0000918676400977, 0.0009646884174780 };

Code: Select all

q_{x}\left(d\right)\ =-0.0001006957601695+d\cdot0.0006303089061514\ -d^{2}\cdot0.0390775228531406+d^{3}\cdot0.0001929289349940
q_{y}\left(d\right)\ =\ 2.0000180760089301-d\cdot1.0001059650840689+d^{2}\cdot0.0000918676400977+d^{3}\cdot0.0009646884174780
q\left(d\right)\ =\ \left(-2+q_{y}\left(d\right),\ 13+q_{x}\left(d\right)\right)
y=1000\left|q\left(x\cdot5.132284556\right)-a_{f}\left(x\right)\right|
and for Curved-B rail i got those:

Code: Select all

  static constexpr double xcoef[4] = { 0.9999296427835839, -0.4336250111930321, -0.0343689574670654, 0.0006729125223222 };
  static constexpr double ycoef[4] = { 2.0000700817657306, -0.9029070058349963,  0.0190210273499390, 0.0007210510062280 };

Code: Select all

p_{x}\left(d\right)\ =0.9999296427835839-d\cdot0.4336250111930321\ -d^{2}\cdot0.0343689574670654+d^{3}\cdot0.0006729125223222
p_{y}\left(d\right)\ =\ 2.0000700817657306-d\cdot0.9029070058349963+d^{2}\cdot0.0190210273499390+d^{3}\cdot0.0007210510062280
p\left(d\right)\ =\ \left(-7+p_{y}\left(d\right),\ 11+p_{x}\left(d\right)\right)
y=1000\left|p\left(x\cdot5.077891568\right)-b_{f}\left(x\right)\right|

mmmPI
Smart Inserter
Smart Inserter
Posts: 2747
Joined: Mon Jun 20, 2016 6:10 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by mmmPI »

Someone made a mod to build wall shaped using splines :
https://mods.factorio.com/mod/Bezierio

I thought it would be fit to mention it here, because it is possible to make wall following rail curves !

It works a little similar to recursive blueprint, you output two sets of x and y coordinate for the 2 end of the curve, and 2 sets of u and v coordinate for the green vector originating from those location to draw the purple curve, which you can auto-blueprint a wall onto, of a thickness to configure.

You don't need to understand what an ARX approach or a Chebyshev node is to use !

It was interesting to learn about those mathematical concept and see them used for something that make sense.
defensivespline.png
defensivespline.png (797.1 KiB) Viewed 1122 times

quale
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Thu Aug 15, 2019 4:16 pm
Contact:

Re: What is the exact shape of a curved rail?

Post by quale »

Bézierio wrote: A mod for building analytically defined defensive lines.
Ha!
mmmPI wrote:
Thu Nov 16, 2023 8:20 pm
You don't need to understand what an ARX approach or a Chebyshev node is to use !
That’s the more intuitive spline nature at work. Here it deviates in application, though. Chebyshev nodes are useful in raw polynomial interpolation using a single polynomial, determining sample points that will likely give a good fit. They also betray the resolution distribution and ringing propensity of this approach. One benefit of splines is that they aid segmentation, working around the limitations of a single high-degree polynomial. But in this case there weren’t really any bad choices. We were talking about a single low-degree segment that’s easy to fit to the whole rail piece anyway.

One thing I’d like to point out is that the trig functions in the original are themselves implemented using a polynomial approximation. They just tend to be implemented to a much higher standard. That is why they are relatively slow.

Post Reply

Return to “Gameplay Help”