BVE WorldWide
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Animation code - assistance needed

Go down

Animation code - assistance needed Empty Animation code - assistance needed

Post by mrknowitall Fri Feb 07, 2020 10:17 am

Hi All,

Im wondering if anyone can help me with a bit of coding?  Me, Steve and Mick are still working hard on the airedale line, but theres something i am struggling with, and thats the coding for "moving objects".

I have the code for a object to move from A to B, but im wanting to know the code for "when player train is X meters away" i have tried changing numbers and figures but im having no luck, it may be something i have overlooked, or maybe something i am yet to try, but im hoping to get to the bottom of it before i can move along to the next stage of the route.

For the class 195 thats arriving at BFS, i am using this code.

Code:
StateFunction = if[value + delta > 100, -1,0]
TrackFollowerFunction = if[value == 0, value + 180, if[trackdistance > -150,if[value < 0, value, value - 5 * delta], value]]

can someone who knows animation coding, please explain to me which bit would be the "when player train is X meters away" part?



I know a while back someone made a level crossing that lifted / lowered upon certain distance the player train was, but i am scratching my head at who created that bit of code.


Last edited by Quork on Sat Feb 08, 2020 5:52 pm; edited 1 time in total (Reason for editing : admin edit - more meaningful topic title)
mrknowitall
mrknowitall

Posts : 822
Join date : 2011-07-09
Age : 29
Location : W. Yorkshire

https://www.pioneertrains.co.uk/

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by graymac Fri Feb 07, 2020 11:27 am

Some time back I suggested that perhaps a "cook book" with a few animated code examples might be useful? My understanding is too limited to produce such a thing, unfortunately.
I really struggle with animated coding, not really understanding the mechanics or syntax of the whole thing. And don't even start on boolean functions. Shocked

I made a both a lifting crossing barrier and a swing-gated crossing which open and shut when the player train is at a specified distance from the gates. I freely admit I "borrowed" the function line after studying another dev's item (could've been one of Dexter's?) to see how it was done.
Good luck with that, Ben, I'm sure someone can help, I've had help myself in the past from Chris, if he doesn't know nobody does!
graymac
graymac

Posts : 2127
Join date : 2011-08-28
Location : Co Mayo, Eire

http://www.celtictrainsim.com

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by leezer3 Fri Feb 07, 2020 12:17 pm

Who, me Razz
Let's take this function step by step.

We first evaluate the following clause:
if[value == 0

Simple really- If the *current* value is zero, then return the first option, if not, return the second.
When our object is first shown by the renderer, this value will always be zero.

The first option is again simple:
value + 180


This adds 180 to the value. Now, the next time the funciton is evaluated (in other words, the next frame so immediately), it'll return the second option, which is the following:

if[trackdistance > -150,if[value < 0, value, value - 5 * delta], value]


First, note that here we've got another nested if clause, not just a simple value.
This will be evaluated in exactly the same way as the first above, so let's go onwards:


if[ trackdistance > -150



If our trackdistance (The distance to the player train) is greater than -150m, it will return the first option. Otherwise it'll return the second.
Remember that the track distance in this case is a signed number; When the train is approaching the object, it will return a negative number, and once passed it will return a positive number.

When the object is first shown by the renderer, trackdistance will be approximately your viewing distance, and as the train approaches this number will go downwards Smile
This means it'll return the second option until your train is within the 150m distance:

value

This just returns the previous value (180)

When however we get within 150m, it evaluates yet another nested if statement:

if[value < 0, value, value - 5 * delta]

Again, relatively simple:

if[value < 0


If our value is less than 0, return the second option. Otherwise, return the first.
As we've just moved the value to 180, it will return the second.

Our second option is fortunately simple:

value - 5 * delta


Going back to secondary school maths, the MULTIPLICATION is done before the SUBTRACTION.

We therefore take the delta, which is the time elapsed for the previous frame in seconds (likely to be ~0.03 or less) and multiply it by 5 to give the total distance moved by the object this frame, so something about 10cm or so.
This is then subtracted from the previous value to give the new position of the object.

The final part of this is simplicity itself, and is returned when our value evaluates to less than zero-

value

The last value- Our object stops moving!



Confused yet?

leezer3

Posts : 1785
Join date : 2011-08-23

http://www.bvecornwall.co.uk

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by Phonteus Nevolius Fri Feb 07, 2020 11:23 pm

I wish there was a like button here, I really appreciate such explanations of .animated code.
Phonteus Nevolius
Phonteus Nevolius

Posts : 111
Join date : 2017-10-05
Location : Hungary

https://phonteusnevolius.wordpress.com

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by mrknowitall Mon Feb 10, 2020 5:10 pm

Thank you for the reply! very helpful, and very appreciated.
mrknowitall
mrknowitall

Posts : 822
Join date : 2011-07-09
Age : 29
Location : W. Yorkshire

https://www.pioneertrains.co.uk/

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by Quork Tue Feb 11, 2020 12:29 am

Phonteus Nevolius wrote:I wish there was a like button here, I really appreciate such explanations of .animated code.
But there is! =D On the right hand side of the post you can see a tiny "+" and a tiny "-". The "+" is a like. It's a rather basic system, but that's how the reputation value you can see under the avatar of every poster is calculated.
Quork
Quork

Posts : 1388
Join date : 2012-05-05
Age : 29
Location : Hofheim a.T., Hessen (Hesse), European Union

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by Phonteus Nevolius Tue Feb 11, 2020 3:24 am

Oh, never noticed that... Anyway, I've only been using this forum for 8 years... Smile Thanks!
Phonteus Nevolius
Phonteus Nevolius

Posts : 111
Join date : 2017-10-05
Location : Hungary

https://phonteusnevolius.wordpress.com

Back to top Go down

Animation code - assistance needed Empty Re: Animation code - assistance needed

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


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