ID:2081498
 
I want to make a train that travels across the map to different train stations on different Z maps in real time. My main question is would it be less CPU heavy to have a control panel that players can use to initiate the train's movement than to have a timer that determines how long the train stays docked at each station before moving to the next one?

Details of this design are that: The train has multiple cars; the train moves along tracks (so if a player wanted to make a new station during runtime they can put track tiles down leading to said new station); players can enter and exit the train while its moving; the interior of the train will be rooms players are teleported to when entering (so technically they won't be moving with the train).

I'm not a great programmer so I'm not too positive on how to make all this happen. But I'm determined, if that amounts to anything. All ideas are welcomed. Thanks. :)
Honestly, neither approach should be too CPU heavy, so that fact is basically irrelevant.
In response to Popisfizzy
Popisfizzy wrote:
Honestly, neither approach should be too CPU heavy, so that fact is basically irrelevant.

Having a multi-car train moving across maps and into other z levels wouldn't lag a game down? I assume it would since (I assume) it would use a while or for loop the entire time.
In response to Oleic
My main question is would it be less CPU heavy to have a control panel that players can use to initiate the train's movement than to have a timer that determines how long the train stays docked at each station before moving to the next one?

I was referring to this in particular. Deciding how to get it to move is not gonna have any effect; all that really matters is how intensive it is when moving.

Anyways, in practice it would not be any more inefficient than having a bunch of constantly-moving NPCs. The graphical effects involved are not very intensive, and the use of loops here is fairly inconsequential since you'll be having sleep or spawn statements in them. The only time loops are really bad as far as CPU goes is when there is not a delay, e.g. when you want to try and finish the while loop as fast as possible. Loops alone are not enough to bring a game down.
Alright I see what you mean. Thanks a lot man.
For the train pieces you'd obviously cut down on resource usage by tying them directly to the engine car and moving them as it moves in a loop, preferably over a small list containing just the cars connected to that specific engine. You could do the literal train method of having each car only connected to the one behind it and moving them progressively as each car moves forward, or delay a loop over the cars in the list so each car moves only after the one ahead of it has moved. The second method saves a ton of proc overhead since it's not recursion but iteration.

It's basically the same principal you'd apply to something like a beam projectile where the head object contains most of the details about the children and they behave correspondent to that.
The more interesting question to my mind is how you'd handle curves on this track, and adjusting the train's transform to rotate cars along it.
In response to Lummox JR
I had been thinking about this, and Beziér curves could work pretty well to describe the motion. Turns could easily be computed by determining the angle between two points on the curve.

The difficulty would be getting this to line up nicely with in-world tracks visually. It might be easiest to decide the curve first and place the tracks second.
In response to Lummox JR
Lummox JR wrote:
The more interesting question to my mind is how you'd handle curves on this track, and adjusting the train's transform to rotate cars along it.

I got you covered fam. This video explains it perfectly

https://www.youtube.com/watch?v=uaqZoph265U
In response to Popisfizzy
Popisfizzy wrote:
I had been thinking about this, and Beziér curves could work pretty well to describe the motion. Turns could easily be computed by determining the angle between two points on the curve.

The difficulty would be getting this to line up nicely with in-world tracks visually. It might be easiest to decide the curve first and place the tracks second.

I was thinking that since simple quarter-turn tracks with specific radii are probably easiest to do graphically, maybe the track's length could be rounded and then the distance of travel along the arc could be calculated to determine the current position and angle.
In response to Lummox JR
Those would definitely be the easiest, graphics-wise, but a train always doing quarter turns is kind of ugly. I mean, it's a game so realism doesn't have to be the #1 goal, but still.

If someone wanted to do it with the Beziér curve method, you could easily use the curve itself as the guideline on drawing the tracks, and then use the normals of the curve as specified intervals for the rail ties. I'm thinking that adding a way of getting the normal vector at a given point along a Beziér curve might be a good feature to add to pif_BezierCurve, now that I think about it.

Regardless, your suggestion is definitely easier. I just think it might be a bit ugly.
My first thought was to implement a really crude Kinematics solver. So the first car moves forward by however much, and then the next car's transform matrix can be determined from two points: the point where it connects to the previous car, and the intersection of a circle (with radius equal to the length of the train car) and the curve representing the track. In most cases, the curve representing the track can just be a collection of connected line segments, so computing the circle intersection should be pretty straightforward.

But maybe this method is a bit too involved :P
Lol TheMagicMan. Thanks guys for your suggestions. I understand what you all suggest, but I simply don't know how to even code this. I've tried making a proc for having a train obj move along traintrack objs along the ground for a day and more, but the shit is difficult. I can't yet understand the syntax and how different things work together to make it happen.