The International Simutrans Forum

 

Author Topic: [Patch, newer exists] Check tightness of railway curve?  (Read 4914 times)

0 Members and 1 Guest are viewing this topic.

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20917
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
[Patch, newer exists] Check tightness of railway curve?
« on: December 21, 2008, 12:33:54 AM »
I am currently working on a patch to change the way that trains handle corners (instead of corners giving trains extra friction, they just reduce the speed limit to a set fraction of the original speed limit, 0.75 for 45 degree corners, 0.5 for 90 degree corners, which, I think, is more realistic). However, I am having great trouble finding out a means of determining programatically how sharp that any given corner is.

I am looking at the "ribi" enum values in ribi_t (ribi.h), which make sense in themselves. However, when I compared "alte_fahrtrichtung" (old direction) with "fahrtrichtung" (direction) I got some odd results: using debug mode, I discovered that the problem lay with the values of fahrtrichtung and alte_fahrtrichtung themselves: even on a 90 degree bend, instead of going from, say, 1 (North) to 2 (East), it would first go to 3 (North-East). Sometimes the values would be quite idiosyncratic (returning a 315 degree turn on occasions!), but I have not been able to reproduce that reliably.

I should be most grateful for any assistance.

Edit: I have done some further research. I have set up a line of code to call ribi_t::gib_dir(fahrtrichtung) at every hop, and add all the results to a 16-sized array of dir numbers. On an oval shaped railway track, with four 90 degree bends, I get the following:

5, 5, 5, 5, 3, 0, 2, 1, 1, 1, 1, 1, 1, 1, 7, 4

For reference, the values for "dir" (different to ribi, oddly) are:

South: 0
West: 1
South-West: 2
South-East: 3
North: 4
East: 5
North-East: 6
North-West: 7

Can anyone explain this somewhat odd behaviour?
« Last Edit: December 30, 2008, 11:09:01 PM by prissi »

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10822
  • Languages: De,EN,JP
Re: Check tightness of railway curve?
« Reply #1 on: December 21, 2008, 04:40:56 PM »
This is the way the system works. The dir is the direction a vehicle is going. Simutrans is only able to detect changes (and at that monent apart froma diagonal V all changes are 45°). I see no easy way to achieve what you want without adding a lot more code or look a whole convois (i.e. a curve is defined not sharp, if the diagonal way is longer than the convoi) like OpenTTD does.

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20917
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Check tightness of railway curve?
« Reply #2 on: December 21, 2008, 06:33:41 PM »
Prissi,

thank you for your reply :-) It seems that Simutrans will not make a 90-degree turn in a single step, so what I have done is set up an array of previous directions and written a routine to check the maximum deviation from the current angle in the previous X hops, and apply a speed limit based on that. The advantage of that approach is manifold: the number of previous hops to be checked can be varied depending on the speed limit so that, the faster that the track is supposed to be, the straighter that it has to be. Also, a pair of 45 degree corners right next to each other will count on faster track as a 90 degree corner, whereas, if there was a substantial space between them, the speed reduction would be a great deal lower. It also allows for tilting trains to be simulated (in terms of game mechanics, at least, if not graphics) by reducing the speed limit reduction effect on corners if the vehicle is marked as "is_tilting".

Unfortunately, the code seems to behave oddly: on perfectly straight sections of track, the trains will sometimes slow down to half their speed (as if they were going around a 90 degree corner); conversely, trains will often take sharp corners at full speed. I have attached a patch with the code that I have written so far. I should be very grateful if you or somebody could take a look at it to see if you can diagnose what the problem is.

Thank you very much for your help so far.

Edit: I have continued to experiment, and my results get more bizarre. Using a different system for checking whether the train is currently turning a corner, I still get extremely odd results in that the corner often registers as much tighter than it really is. Even sometimes a 45 degree turn after a long stretch of straight track will enforce a speed restriction as if it was a turn of 135 degrees(!): checking using the debugger, the array of previous directions contains some seriously odd values (such as south, southeast, southeast, north) where there is no track anywhere that has those properties. Can anyone give any explanation as to what is causing this bizarre behaviour and how to get a sensible measure of the tightness of a bend?
« Last Edit: December 21, 2008, 09:29:48 PM by jamespetts »

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10822
  • Languages: De,EN,JP
Re: Check tightness of railway curve?
« Reply #3 on: December 21, 2008, 10:24:59 PM »
Sorry, I do not have time to debug your code. There are still debugged patches pending.

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20917
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Check tightness of railway curve?
« Reply #4 on: December 21, 2008, 10:41:43 PM »
Sorry, I do not have time to debug your code. There are still debugged patches pending.

I understand that your time is limited. Perhaps you could give some indication of how the direction systems work so that I could debug it myself?

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10822
  • Languages: De,EN,JP
Re: Check tightness of railway curve?
« Reply #5 on: December 21, 2008, 11:36:58 PM »
This is difficult and cost me a month to completely understand it. Sorry, no shortcut possible imho.