How do I Simulate Traffic That Stays in Proper Lane

0 favourites
  • 10 posts
  • I am making a top down game and it has simulated traffic in it. I can get the cars to go to random waypoints just fine using pathfinding, but the problem is they are all over the road and will just drive through each other without blinking an eye. I need to figure out a way to have the cars stay in their own lanes depending on the direction they are heading, and also at least somewhat yield to other cars instead of just running them over.

    Anyone have any ideas?

  • Must be a tough one to crack hmm...

  • This is actually a rather difficult problem...

    I have no idea how to go about it, but I have two ideas for approaches:

    1. Using the built in pathfinding system, create paths with blocks of the same resolution as one lane, and block and unblock specific lanes for each car as necessary. This is probably a Bad idea, as updating the obstacle grid often is costly, and you would probably need to do so individually for each car to have a decent simulation.

    2. Create a custom pathfinding system with nodes at intersections per lane and bullet behaviors. Much work needed.

  • Well this is unfortunate :/ Some things in Construct 2 are easy, and some things are a pain in the bottom.

    So maybe the MoveTo plugin from Rex would be better suited? Then just have the waypoints. Something like one lane is pink waypoints labeled waypoints1, and the second are green labeled waypoints2. If they spawn in the pink make it pick a random pink waypoint and moveto that waypoint. From left to right on the screen it would be Higher to Lower numbers, so that way if the object is moving left or at that angle it would add +1 to the move to if its moving right it would subtract -1 from the moveto so that it keeps moving until it hits its target waypoint number.

    Hope that makes sense..

    Really there isn't an easier way?

  • If it helps this will be hard in any other software as well, unless they handle it specifically.

    Anyways no pathfinding needs to be done at all, unless you need the cars to have a specific destination, otherwise they can just meander.

    Here's one idea:

    https://www.dropbox.com/s/dl5b7om2qwweo ... .capx?dl=1

    /examples34/road_path.capx

    Basically it's a bunch of waypoints connected to make paths. The waypoints can have multiple outgoing paths, so the cars just pick a random one. I made an in game editor so creating the connections is easier. I then can copy the result to a variable and remove the editor if i want to.

    Next step is to make the cars move. I suppose the moveto behavior could have been useful, but I opted to just use events to move from one waypoint to the other. Pretty much the only requirement I had was to specify when to move.

    The car to car awareness proved slightly complicated. It was simple enough to make the car's angle face the direction it was going. Then i could see if another car was ahead with the overlapping at offset condition. That isn't enough for intersections, which would bind up with just that rule. My solution was to only let one car travel through the intersection at one time. It's random so maybe actually making traffic laws would help.

    Overall it works pretty well, however the logic for intersections fails in a few places near intersections, so that would need more work.

    There may be other simpler ways. Standby for a sightly different idea.

  • Thank you much. I am not able to open the project because it says it was saved in release 230 and I am only on 227 however I am not able to download anything newer lol....thats confusing.

  • Nevermind I found it in beta releases haha. I look forward to seeing if you have other ideas. I am going to check this one out.

  • Here's the other slightly differecnt method:

    https://www.dropbox.com/s/df8jtstjva45m ... .capx?dl=1

    /examples34/road2.capx

    Cars track to the line defined by the road's position and angle. The result is some smoother turning.

    The intersections are done by picking a random outgoing road in roughly the direction the car is facing.

    The car car interactions is slightly different too. If there's another car ahead it stops, but if it's clear further ahead it keeps going. This works well for the most part but it fails pretty bad if cars cross paths in an intersection so they drive through each other.

  • Easiest solution.... make a dummy sprite that covers both lanes. Pin the car to one side of the dummysprite. Move dummysprite instead. Car will always stay on right side, in crossings, roundabouts, when turning. Not harder than that.

    U can even have the car u-turn by just rotating the dummy sprite,

    Edit: you can also make sure it can't turn if it's overlapping another sprite, or opposite other cars nearby stop while it's turning.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Easiest solution.... make a dummy sprite that covers both lanes. Pin the car to one side of the dummysprite. Move dummysprite instead. Car will always stay on right side, in crossings, roundabouts, when turning. Not harder than that.

    U can even have the car u-turn by just rotating the dummy sprite,

    Edit: you can also make sure it can't turn if it's overlapping another sprite, or opposite other cars nearby stop while it's turning.

    That actually sounds like it would be a solid solution. If I get some time I am going to test it and reply back to this thread, but I really see no reason why it wouldnt work. Thank you.

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