How do I Simulate Traffic That Stays in Proper Lane

Get help using Construct 2

Post » Sun Jul 17, 2016 5:32 am

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?
B
9
S
3
Posts: 50
Reputation: 804

Post » Mon Jul 18, 2016 5:50 pm

Must be a tough one to crack hmm...
B
9
S
3
Posts: 50
Reputation: 804

Post » Mon Jul 18, 2016 5:57 pm

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.
Mistakes were made.
B
51
S
25
G
107
Posts: 1,581
Reputation: 60,458

Post » Mon Jul 18, 2016 7:20 pm

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?
B
9
S
3
Posts: 50
Reputation: 804

Post » Tue Jul 19, 2016 12:11 am

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://dl.dropboxusercontent.com/u/542 ... _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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Tue Jul 19, 2016 6:30 pm

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.
B
9
S
3
Posts: 50
Reputation: 804

Post » Tue Jul 19, 2016 6:31 pm

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.
B
9
S
3
Posts: 50
Reputation: 804

Post » Thu Jul 21, 2016 5:18 pm

Here's the other slightly differecnt method:
https://dl.dropboxusercontent.com/u/542 ... 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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Thu Jul 21, 2016 5:30 pm

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.
Follow my progress on Twitter
or in this thread Archer Devlog
B
37
S
15
G
17
Posts: 949
Reputation: 12,295

Post » Thu Jul 21, 2016 7:19 pm

tunepunk wrote: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.
B
9
S
3
Posts: 50
Reputation: 804


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 19 guests