Improve this top down car traffic example/code?

0 favourites
  • 8 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Hey all!

    I found an 'old' car traffic example by R0J0hound;

    construct.net/out

    Which I'm really grateful for!

    But I noticed, as well as by reading an old topic of mine where the link was posted, that it wasn't completely finished.

    Main problem is that there is congestion at the junction, when too many cars want to go in different directions. Problem: They get stuck. They do move tiny bits, but through eachother. I'd love to see it solved, I'm sure many others would get great value from this.

    So what I was thinking is, if there are three or more cars entering the junction, and they are all waiting for eachother (and the code 'knows' this in advance) have a car take another turn, instead of the turn that would cause trouble between the cars at the junction.

    I couldn't figure out what lines to adapt/add and where, partly because I don't understand what everything 'does'.

    P.S. I posted this in C3 section because I used the code in a C3 project.

    Tagged:

  • Anyone?

    I tried adding and changing parts of the code in the link (you can still open the .capx in C3 ;)) but after hours of fiddling I gave up. I was hoping someone with more knowledge could help out!

  • When I posted that example I didn’t have a good idea how to fix that issue and decided it still was good enough to post as it mostly worked.

    To fix I’d say the whole thing would need to be redesigned with more logic at intersections.

    As is it just moves along the roads, turns randomly at intersections, and stops if a car is ahead.

    Probably would need the cars to not enter the intersection unless it’s path doesn’t cross another one already in it. Not just look right in front of itself. Probably would do well with some kind of signal logic to keep things fair for cars who can’t enter.

  • Thanks for your reply.

    If I wanted to change the code so that the cars only turn right, (so they circle a block, or possibly around four blocks) what would I need to change? I intend to use it for enemy cars, so they won't Wrap, and the enemy cars will drive around a block on the map.

    I also want to use the same code so that cars only go straight ahead (This one does use the Wrap behavior) and will give way to cars coming from the right. This code will be used for all other, regular traffic then.

    What would I need to change?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That particular event looks at all the outgoing roads except for the one the car came on and just picks a random one.

    To only turn right I guess you could pick roads whose angle is car.angle+90 or something.

    It’s honestly been long enough that I don’t like the events I wrote and would rather just redesign the whole thing and maybe reusing some of the concepts used here. I’ll probably have a go at that the next few days with better intersection logic. Maybe the cars could have preferences where they’d like to go for like pathfinding and such.

    In my experience when I make something and then change what I want it to do it’s not often a simple tweak. Besides rewriting it often yields a better design anyways.

  • That'd be awesome. Can't wait! Thanks in advance.

  • R0J0hound Hey, I was just wondering if you were able to come up with that new traffic 'logic'?

    The code you wrote most recently (road2.capx) was easy to integrate in my own project, because you just put the number of vehicles you want to have on the layout, and put down the roads where you want them. That way you can exactly decide where you want to allow the AI to drive. And the random car starting locations code works great for me personally.

    Do you think it's doable? Or would it take a lot of your time.

    I'd do it myself but my coding skills are still primarily stuck at 'novice', when I attempted to write something myself a few times every time I just couldn't make it work.

  • Sorry. No ideas yet. Haven’t been on the computer.

    Any idea is probably feasible. I’d need to dissect that capx to see what I did to comment on any ways to modify it.

    On face value all the cars need to do is move along the roads, queue up at intersections, and have some logic to let them go though intersections in an orderly fashion.

    I’d have to work it out on paper, then find a way to make it into events. Depending on how I decide to do it some quirks of the event system need to be taken into account.

    Sub-problems to solve include moving the cars smoothly, handling different speeds, testing for edge cases and such.

    I’ll post if I get anything working.

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