Creating top-down car traffic (AI)

Get help using Construct 2

Post » Tue Aug 08, 2017 11:07 am

Hello!

I was wondering if it's possible to make some simple car traffic.

Now I *think* I grasp the basics of it, but I strongly feel like it can be done in a much better and less time consuming way.

So basically, I have 18 different cars (most same models but different color) and I want them to stop and give way when a car is coming from the right.

What I have now is like this:

Image

As you can see that can be much more efficient, I would have to copy that group and change it 17 times for each car group.


I was also wondering how to handle things when four cars are arriving at the crossing around the same time, with LOS they might just end up giving way forever to the one on the right. Now I did have some stop lines added all over the map, basically the horizontal lanes are supposed to be given way. Green and grey car are supposed to give way as you can see so that might solve the problem a bit, but I don't understand yet how to efficiently code that?

Image

Also I notice that with LOS the cars only stop when they are (almost) hitting the other car, regardless of how much LOS sight I give them. This happens when the car from the bottom has to give way to a car from the left for example.

So I thought about using families. But I couldn't figure out how to get the one that has line of sight with a car, to get that specific car to stop instead of the whole family. If any could shed any light on this traffic issue.. I'd be really grateful.

P.S. Here's a .capx sample of the situation: http://mediaconceptfactory.com/images/Traffic.capx

- R
B
13
S
7
G
4
Posts: 143
Reputation: 3,581

Post » Sun Aug 13, 2017 6:15 pm

Hi @Rudi55

I give it a quick go to try and this is my results, I'm not sure if this is what you looking for and I'm sure is not the best but it might help you to give some ideas.
Note I didn't have much time to test it as I done it quick, so test it properly because you may find some bugs if you need help let me know I will have a look.
you can customize it a lot more and improve it if you know how.
(The Middle roads = Horizontal) cars have a preference which is the Right like you asked

For fast Driving and Animation Change Variety >>>>>>>activate the group >>Extras

Capx: https://www.dropbox.com/s/sgrr2of0okr2s9e/GTA%20San%20Andreas%20Light%20%29.capx?dl=0
B
38
S
22
G
63
Posts: 235
Reputation: 35,558

Post » Sun Aug 13, 2017 9:48 pm

Thanks, I will take a look as soon as I got some other issues solved for another game!!
B
13
S
7
G
4
Posts: 143
Reputation: 3,581

Post » Mon Aug 14, 2017 4:33 pm

Hey tarek2, thanks so much! That traffic is actually pretty darn well done. The only thing left to improve could be to stop the cars from driving in 'pairs' since at one time, they'll end up waiting for eachother when giving way and they drive in pairs forever. Probably the issue is the wrap behavior, because it causes the two cars (pair) to continue as they were after having to give way.

Did you originally create this capx one time for yourself?

It might take some time for me to figure out how it's all done though, lol, I guess I'm still a beginner.

Anyway, thanks again!!
B
13
S
7
G
4
Posts: 143
Reputation: 3,581

Post » Mon Aug 14, 2017 5:59 pm

Here's and old capx experiment of mine that may give some ideas.
https://www.dropbox.com/s/df8jtstjva45m ... .capx?dl=1

The cars don't really follow any traffic laws, they just go while trying to not overlap each other. It can fail when multiple cars enter an intersection at the same time, so some right of way is needed. The hack fix was to make the stop temporary and they'll eventually just drive over each other to keep things going.
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Mon Aug 14, 2017 6:17 pm

@Rudi55

Thank you, sir, I'm glad you like it :D

yea I created from scratch I didn't have much time to do it that's why I did it pretty basic so you can follow easily how it was done and from there improve it on the top of it with your ideas, I didn't want to make it to complicated other ways you will find hard to follow it. I tried to simplify it as much as I can.

is true I had the problem with driving in pairs and to avoid that I came up with the idea with the Speed boost but you have to activate the group "Extras Only " did you try that? it is not active by default, it improves the driving on pairs a little bit not much, but much better than when they are all driving at the exact speed. I tried many different things to avoid the driving on pairs but the problem I saw at the end is that are many stops in very short roads and doesn't matter how I put one car faster and the other slower they will catch up again in the stops because the faster one comes back from behind and has to wait and drive at the same speed of the slower one that's in front of him blocking him.

I will give it another go when I have time and will see how can it be improved, does the map has to be exactly that way or can it be changed?? what is your goal for this game ?? the more info you can give me the better to take it into account for the logic, you need it slow or fast driving game?
B
38
S
22
G
63
Posts: 235
Reputation: 35,558

Post » Mon Aug 14, 2017 6:45 pm

No problem.

You are very helpful. No I forgot to activate the 'extras' group, haha. The idea I had for this game was to play as a police car arresting bad guys. You'd have to drive through the traffic, avoiding cars and pedestrians while being in time to catch the criminals.

To be honest with you though, I left the project/idea for what it was as it took some time before someone came with a solution for the traffic + I once again had a too large project scope! I always have loads of ideas that I want to implement and the scope of the game becomes too big. I pretty much finished the map, and a handful of cars and the pedestrians (all top down).

Looking at the code from your example it might take some time for me to understand it all. I do want to get better at programming but I think I'll need to start making games that are less complicated and go from there.

Oh yeah, I also had issues with the car movement (for the player controlled car) I just couldn't figure out how to program it so it would steer normally at higher speeds and it the car would always drive really fast backwards, simulating car behavior is tricky. So yeah lots of reasons for me to start with something else, something smaller.
B
13
S
7
G
4
Posts: 143
Reputation: 3,581

Post » Mon Aug 14, 2017 10:10 pm

Nice one @R0J0hound :D

Runs really smooth



@Rudi55 Cool
B
38
S
22
G
63
Posts: 235
Reputation: 35,558


Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 8 guests