Animated Objects: Following The Track

View previous topic View next topic Go down

Animated Objects: Following The Track

Post by leezer3 on Sat Aug 27, 2016 1:16 pm

The most recent build of openBVE now has a new function available for use in animated objects-
TrackFollowerFunction

One of the biggest problems in animating a moving train (Or other item!) is that previously we couldn't go around corners, or up hills.

TrackFollowerFunction provides a considerable improvement on this, as follows:

  • The result of this function should be the distance in meters for which we wish the object to be translated.
  • This translation is then applied along the path of Rail 0.
  • Finally, any other translations & rotations (From TranslateXFunction etc.) are applied to give the object's final position.


Limitations:

  • Only Rail 0's path is supported.
  • Horizontal & vertical positioning of the object must be done via the Position command in your .animated file, **not** via the routefile.
  • Sharp curves don't work as well as loose curves.
  • The further horizontally you go from Rail 0, the worse the results- Both of these are to do with the fundamental mathematical principle that a curve with a different radii will have a different length Razz


Finally, the video:


Cheers

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by leezer3 on Sat Aug 27, 2016 1:30 pm

For reference, this is the code used in the animation above:
Code:
[Object]
States = ..\cl47_Intercity_Swallow\47826_A.csv
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 5

;Coach 1
[Object]
States = ..\coaches\GUV_blue_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 24.8

;Wheelset1
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 16.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 18.9
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 30.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 33.1

;Coach 2
[Object]
States = ..\coaches\mk1_bk_itc_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 44.8

;Wheelset2
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 36.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 38.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 50.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 53.1

;Coach 3
[Object]
States = ..\coaches\mk1_FO_itc_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 64.8

;Wheelset3
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 56.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 58.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 70.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 73.1

;Coach 4
[Object]
States = ..\coaches\mk1_FO_itc_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 84.8

;Wheelset4
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 76.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 78.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 90.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 93.1

;Coach 5
[Object]
States = ..\coaches\mk1_TSO_blue_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 104.8

;Wheelset5
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 96.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 98.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 110.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 113.1

;Coach 6
[Object]
States = ..\coaches\mk1_TSO_blue_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 124.8

;Wheelset6
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 116.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 118.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 130.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 133.1

;Coach 7
[Object]
States = ..\coaches\mk1_FO_itc_n.b3d
Position = 4,0,0
TrackFollowerFunction = -mod[10 * time, 1000] + 144.8

;Wheelset7
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 136.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 138.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 150.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 153.1

;Coach 8
[Object]
States = ..\coaches\mk1_BCK_blue_n.b3d
Position = 4,0,0
RotateYFunction = 3.14159265
TrackFollowerFunction = -mod[10 * time, 1000] + 164.8

;Wheelset8
[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 156.5

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 158.9

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 170.7

[Object]
States = ..\wagons\wheelset_solid.b3d
Position = 4,0.425,0
TrackFollowerFunction = -mod[10 * time, 1000] + 173.1

You'll note that the offset for each coach/ wheelset is added to the result of the TrackFollowerFunction- This is to ensure that they're correctly offset when on the bend.
The X position value of 4 is needed to move our objects to the adjacent track.

Cheers

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 3:32 pm

Yeeeaaah!


phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 4:13 pm

OK, here's a nicer video, with the arrival and departure of a Királyliget bound train, and in between an oncoming Sólyomvár bound train arrives as well. Great stuff, Chris!


phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 4:42 pm

Can I ask for some help? I'm not very good at animation scripts. What I'd like to reach is that the oncoming train with the TracFollowerFunction ran only once, and that would be when the train I'm driving gets to a certain distance from it. Is this possible, or will it always re-appear again in cycles?

Here's the Siemens Desiro I used in the videos: https://dl.dropboxusercontent.com/u/51947703/BVE/Desiro-TrackFollowerFunction.7z

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by leezer3 on Sat Aug 27, 2016 10:40 pm

This is a somewhat hacky solution to your problem:
Code:
TrackFollowerFunction = if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]

A breakdown of the function:
if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]
This is the 'hacky' part- We're relying on the fact that the value will only ever equal zero when the object is first loaded by the renderer.

if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]
When the condition is met, move our object 1000m forwards.

if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]
Otherwise, test if our value is greater than zero.

if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]
If true, just return the value (Don't move the object)

if[value == 0, value + 1000, if[value < 0, value, value - 10 * delta]]
If false, take away 10 multiplied by the time elapsed in seconds from the value to move the object towards us.

This is the same function, but moving the object in the same direction as the train:
Code:
TrackFollowerFunction = if[value == 0, value - 1000, if[value > 0, value, value + 10 * delta]]

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 10:59 pm

Cool, thanks! I'll give it a try right now!

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 11:30 pm

Great!

With this script it stops suddenly. Is it possible to make it brake down gradually?

And is it possible to get rid of the "AI" train after it's finished moving?

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by leezer3 on Sat Aug 27, 2016 11:45 pm

Braking smoothly will get complex fast Smile
This is a three speed-stepped formula (1000m to 100m, 100m  to 50m and 50m to 0m)
Code:
TrackFollowerFunction = if[value == 0, value - 1000, if[value > 0, value, if[value > -100,value + 20 * delta,if[value > -50,value + 10 * delta,value + 5 * delta ]]]]
(Untested, but should work, if not, you should be able to read across to see what it's doing)

I'll do some thinking, as I'm sure it's probably possible to do this better, but that's the best I can do off the top of my head.

Getting rid of the AI train is a little more complex.
For this you need a StateFunction
The easiest way to do this, is probably on a simple timer basis:
Code:
StateFunction = if[value + delta > 20, -1,0]
Simple enough- The object will be removed (Set to state -1) after 20 seconds. You'll obviously need to alter the timer to suit the length of the animation.

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sat Aug 27, 2016 11:48 pm

Thanks, Chris.

And another question. Can this script be connected to the distance from the driveable train? I mean, with this script if I go to the F3 view and hop to the position of this "AI train" object using the camera forward (Num 7) button the "AI" train already starts moving. I'd prefer it to wait until the train I'm driving approaches that point.

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by leezer3 on Sun Aug 28, 2016 12:00 am

Sure Smile
Just requires another if condition.

Using the first function for a little clarity, hopefully you should be able to see the difference:
Code:
TrackFollowerFunction = if[value == 0, value + 1000, if[trackdistance > -200,if[value < 0, value, value - 10 * delta], value]]

All this has done is added a check as to whether the train is closer than 200m before doing anything.
Note:
I haven't checked whether Pretrains are considered a train for this.
This will be clarified when I've had time to check the code, and a separate variable provided to check for player trains and AI trains.
https://github.com/leezer3/OpenBVE/issues/91

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sun Aug 28, 2016 12:31 am

I think pretrains are also considered, because a few seconds after the route loads I see the animation happening. Route.Runinterval is 1800, if that matters, but maybe not, I don't know. But, as a matter of fact there are no visible pretrains in the area of the animation, so that's funny.

Another thing: StateFunction = if[value + delta > 20, -1,0] doesn't seem to make the "AI" train disappear. This is what I have in my animated object, maybe I've done something incorrectly:

Code:
[Object]
position = -4,0,0
States = DESIRO1.B3D
StateFunction = if[value + delta > 20, -1,0]
TrackFollowerFunction = if[value == 0, value + 1000, if[trackdistance > -200,if[value < 0, value, value - 10 * delta], value]]

[Object]
position = -4,0,0
States = DESIRO2.B3D
StateFunction = if[value + delta > 20, -1,0]
TrackFollowerFunction = if[value == 0, value + 1000, if[trackdistance > -200,if[value < 0, value, value - 10 * delta], value]]

[Object]
position = -4,0,0
States = DESIROFORGO.B3D
StateFunction = if[value + delta > 20, -1,0]
TrackFollowerFunction = if[value == 0, value + 1000, if[trackdistance > -200,if[value < 0, value, value - 10 * delta], value]]

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by phontanka on Sun Aug 28, 2016 6:36 am

And what would be the best option to add sound to these TrackFollowerFunction trains? .Announce is not the best, because you don't know for sure what track position you meet them at.

phontanka

Posts : 215
Join date : 2012-05-08
Location : Hungary

http://phontanka.wordpress.com

Back to top Go down

Re: Animated Objects: Following The Track

Post by leezer3 on Thu Sep 01, 2016 7:57 pm

Been rather busy, so didn't get back to this.....

I think these two will do what you want, combination of my mistake and the animation formulae system still being rather funky.....
Code:
StateFunction = if[trackdistance > 200, 0 , min[value + 0.5 * delta / 20 , 1]]
TrackFollowerFunction =  if[value == 0, value + 100, if[trackdistance > 200,value,if[value < 0, value, value - 10 * delta]]]

StateFunction:
That was my mistake, I'd forgotten that delta isn't cumulative, and hence you've either got to do some real nastyness with total time or the hack above.
The track position check could be done away with to leave this:
Code:
StateFunction = min[value + 0.5 * delta / 20 , 1]

This will produce odd effects if you reverse with the train still in view (It'll vanish after 20s no matter what), so I wouldn't reccomend this.

TrackFollowerFunction:
There's a bug hiding in the treatment of negative numbers which I haven't got to the bottom of yet which really wasn't helping working properly....

Sounds:
Absolutely no support at present Razz
This will come at some stage, but the sound triggering system is an absolute mess.

Improvise? Twisted Evil

leezer3

Posts : 750
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Re: Animated Objects: Following The Track

Post by Dexter on Fri Sep 02, 2016 11:29 pm

I like this! *thinks about a couple of hours tomorrow evening*

Dexter

Posts : 1780
Join date : 2011-07-08
Age : 31
Location : Brno, Czech republic

http://www.brnobve.eu

Back to top Go down

Re: Animated Objects: Following The Track

Post by Sponsored content Today at 10:25 pm


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum