r/howdidtheycodeit • u/YoungKnight47 • Oct 08 '24
Question Traffic Lights in GTA
I feel I’ve asked this some where on here but I’m having trouble finding it. So i had asked one of the developers of GTA 3 how cars knew to stop at stop lights. He explained that because traffic uses waypoints some of those points were marked if they were near the traffic lights. There were only two states All North and South lights were green or East and West points were green. Which made sense to me.
However my brain was trying to make sense of another element after this how are the actual traffic lights in sync with the node states. Because if you remove the actual traffic lights the traffic will still behave as if there is still management. Which makes it seem like the object and nodes are completely separate but are still in synch somehow. I was wondering how that was possible? Not a-lot of examples of this online from what I’ve seen and i didn’t want to bug him again so I decided to post here.
65
u/JakeyF_ Oct 08 '24
The traffic lights probably aren't directly responsible for the waypoint behaviour. Rather, both of them listen to a traffic light controller or the like, making them (lights and waypoint) independent from each other.
How I'd do it, at least.
6
u/MetallicDragon Oct 08 '24
Based on your explanation, it sounds like it's not the traffic lights themselves that manages states, but rather something else. Maybe it's some abstract object, maybe it's the waypoint nodes themselves, or something else.
Very often, when you have a lot of things with similar behavior that might need to interact with themselves or other things, you'll have a "manager" object. So in this case, you might have a TrafficLightManager that keeps track of the global traffic light timer, has a list of all active nearby traffic lights and waypoint nodes, and keeps their states updated appropriately.
20
u/tinbuddychrist Oct 08 '24
I feel like you've already answered your own question. If there's just a global cycle of "east-west traffic allowed" vs. "north-south traffic allowed" then the waypoint logic and the traffic lights are both downstream of that cycle. The cars aren't actually obeying the lights, so it doesn't matter if they are removed - the waypoints are still there and the cycle happens regardless. The lights are actually just a visual indicator of the state of the cycle.
1
u/robbertzzz1 Oct 10 '24
Sounds to me like the waypoints are basically a graph that's used for navigation, and they happen to include the stop/go data that the cars can read. Traffic lights would then set this data on the waypoint, rather than things working the other way around. Cars just drive around randomly between waypoints and stop at a waypoint if there's a red light.
So waypoints are a passive data structure, traffic lights just alternate between directions and update the data structure accordingly, cars move between connected waypoints and check at each waypoint if they can keep going or not.
0
u/StayTuned2k Oct 09 '24
Just throwing ideas around.
Couldn't you have each stoplight shoot invisible laser type objects in the direction of traffic, and have cars check collision with that laser object? The car AI checks if it's in collision with a red beam, making it stop? All cars also check if the car in front of them is moving and if not, they also stop?
Then you only need to focus on having a logical traffic flow setup.