How do I keep the object (train) on the curve (rail)?

1 favourites
  • 11 posts
From the Asset Store
Create a game inspired by great arcade classics as Operation Wolf
  • Hi there, I need to keep my object on the curve, I will move the object forward and backward with the keys (up and down arrows). Could you help me, please? Thx, Rene.

  • You can add imagepoints belong your curve and have a variable Destination to set your object move to that point.

    Then you press Up and add 1 to Destination and Down to subtract 1 from Destination.

    Example: follow-curve.netlify.com

  • Reneczech

    are you doing a top view or side view? and how long is the train? it would get a little tricky making all the cars getting pulled stay on the track, but I am sure it could be done.

    it would help if you posted a sample that had some track and parts of the train for us to experiment with.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yes, it is view from above. Train will have two coaches. Sample is atacged, you can find there curved line, locomotive and two coaches. Thanks for your help

    drive.google.com/open

  • Reneczech

    well, I had a lot of fun playing with that, and have a good start... I borrowed ideas from a bunch of places - a line following example ROJOhound made a couple years ago, smooth curves for trucks, a wiggly worm demo, etc.

    I made two track segments (straight, and a 90 degree curve) that can snap together to make the layout you wanted. It would be easy to make other types of track segments - just have the end line up, and create image points.

    if you turn on snap to grid with a grid size of 44x44 pixels it is real easy to place the tracks. You have to make sure to get the arrows on the segments all pointing the same direction - otherwise the waypoints wont get created in the proper order at runtime... the waypoints are created based on image points on the track segments. You also have to assign a sequence number to each section of track so the program knows what order to process them in, when the waypoints are created. (There is a family instance number called SegmentNumber for this).

    I changed the layout a little to make a loop so the trains can keep going around and around. Then I thought it should be possible to have multiple tracks... it uses the track name to know which track a waypoint belongs to.

    I also made it so that trains can accelerate and slow down and stop - but there are still problems with spacing the wagons. I used a method I helped someone else with (who was making a worm), it keeps a history of where the head has been to position the parts behind it. this works well for keeping the wagons on the tracks around corners. But, I am not sure it is the best way to do this... I might try some other options later this week.

    going around curves is not as smooth as I would like either... adding more image points to the tracks might help, but that might add other problems too.

    So, hopefully this will give you something to work with.

    https://www.rieperts.com/games/forum/traintracks.capx

  • ok, just did a major update on the traintracks.capx

    I removed the code that was trying to manually space the wagons using an array of positions from the train head, and gave the wagons the bullet behavior instead. This works MUCH better - creating the train, starting, stopping are all greatly improved. And the CPU utilization is a lot lower as well - and simplified the code (they are all in the same family now).

    next thing I want to add is switches - so you can click to make the train change tracks! Another thing I would like to add is a control panel - click a train to bring up controls to start/stop/accelerate/slow down, zoom in/out to follow a train.

  • AllanR

    thanks a lot for your advice, it was really helpful. I just started and it is exactly I needed. I just have 2 questions. When I stop the train, sometimes the train goes back and then jumps to right position. I understand that probably image points controls this (when train is closer to point which is behind the train train goes to this point. Any idea how to amend this? And the second question is: If collision with other object (sinus behavior) on the rail occur I need train to stop immediately, now the train goes ahead.

    Thanks a lot again,

    R

  • Reneczech

    I have a much more advanced version that I was playing with - the trains start and stop much better, and can go in reverse, I added switches - you click on a switch to change its setting.

    currently switches don't work when the trains are going in reverse. and collisions will take some thought, and will probably require adding independent train controls (they are still just random speed, and they all start, stop and accelerate together).

    I might have a little time later tonight... and will post the updated version when I can. (I will post again to say a new download is available).

  • Cool examples AllenR! Looks like you had a lot of fun making them.

    I'm a bit late to the party, but I finally was able to try out an idea to make motion on the curves smoother. It also lets you place cars at exact distances on the tracks so spacing stays good too. Other than that it's pretty bare bones with only one track.

    Anyways here's the capx.

    dropbox.com/s/quzp15smcjkljd4/train_wip2.capx

  • I'm a bit late to the party, but I finally was able to try out an idea to make motion on the curves smoother. It also lets you place cars at exact distances on the tracks so spacing stays good too. Other than that it's pretty bare bones with only one track.

    Impressive, I had 16 carriages at 100 acceleration, and 2000 speed in debug on laptop, maintaining 60 fps, and 9 % cpu.

    changing to a 30 or 15 degree arc, would give a lot more track design variety, food for thought.

    Anyway, brilliant as usual.

  • [quote]changing to a 30 or 15 degree arc, would give a lot more track design variety, food for thought.[/quote]

    As it is now you can place tracks on a 64x64 grid, which makes making tracks pretty easy in the layout editor. Other arcs wouldn't line up to a grid as readily. I mean an in game track editor is another option, but saving and loading workflows are a bit awkward in construct games.

    Anyways this was meant to be more of a simpler proof of concept.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)