AI Traffic System

Does anyone know how I can make an AI Traffic system? I’ve dealt with AI before, I have my own AI system actually, but its only with pedestrians, I’m not sure how I would go about it. I tried many things that didn’t really work, so if someone could help me out a bit, It would be greatly appreciated!

5 Likes

You could try using pathfinding service.

2 Likes

I’m not sure if that would work if you want the cars to be able to follow traffic laws, I could be wrong tho.

4 Likes

Oh, you meant like NPCs driving cars… Yikes, that’s a tough one. I may have a few solutions though.

You could have cars CFrame to areas right in-front of traffic lights/stop signs and you can tween cars toward them, once completed check if the stop light is green, if it is, do another tween, if not, wait until it is green, same goes for stop signs.

I am not certain on this next one, but RocketPropulsion may be feasible if you modify it heavily.

Or you could use a mix of both pathfinding, tweenservice, etc. I recommend doing a little more research upon it before deciding though.

4 Likes

Nope, highly inflexible

You should use Steering Behaviors to simulate traffic.

Steering behaviors are used to move autonomous characters/vehicles in a natural manner. Wander steering and object avoidance steering behaviors are perfect! It takes a bit of math and physics though.

I have made a tutorial for the same. I also recommend to watch a few videos and read the official research paper, found here:

8 Likes

This would take a lot of work to turn into an AI Traffic system, I don’t anything too crazy. Just something simple for the time being because I need something asap.

2 Likes

Well, AI itself isn’t just what schools teach you, “Artificial intelligence”. So its probably going to take you months to achieve what you want. Good luck :+1:

5 Likes

Using steering behaviors on Roblox’ endorsed vehicles (because of their constraints & it’s built up with parts, instead of one mesh), I achieved into making pursuing cars & tactics within a few months. This can be changed to a traffic system with nodes too.

It needs motivation and experience, but it is definitely achievable on Roblox.

7 Likes

It’s not really that hard, because I (an intermediate scripter) managed to make them. I used TweenService as the fundamental part of the system.
The basis of the system is that cars drive from intersection to intersection. Cars also are assigned random IDs when they spawn, so that scripts can communicate to one specific car. (for example to permit it to proceed at an intersection).
At the intersection shown in the video (a 4-way stop), I used a table to determine which car should go first.
There was no pathfinding involved, instead I just randomised the direction they’ll take at the next intersection. To make the cars turn, I generated a Bezier Curve between 2 nodes, and made them tween along the curve with a loop. Here’s a video of my prototype system (which is currently being rewritten for better optimization). ROBLOX | AI traffic | 4-way stop demo #1 - YouTube

7 Likes

Questions:

  1. How did you generate a bezier curve?

  2. Do you ever plan on using pathfinding?

2 Likes
  1. I looked up a bezier curve function on this forum and modified it for my use.
    Since I do not plan on using that anymore in my current system, here it is:
--Functions
function lerp(a, b, c)
	return a + (b - a) * c
end

function quadBezier(t, p0, p1, p2)
	local l1 = lerp(p0, p1, t)
	local l2 = lerp(p1, p2, t)
	local quad = lerp(l1, l2, t)
	return quad
end
--Curve generation script
for t = 0, 1, 0.011 do
local pos = quadBezier(t, workspace.Placeholders.SouthStart.Position,workspace.Placeholders.BottomRightTurnCenter.Position, workspace.Placeholders.SouthTurn.Position)
local newpoint = Instance.new("Part")
newpoint.Anchored = true
newpoint.Parent = script.Parent.Points
newpoint.Position = pos
script.Parent.OrientationValue.Value = script.Parent.OrientationValue.Value - Vector3.new(0,1,0)
newpoint.Orientation = script.Parent.OrientationValue.Value
newpoint.Transparency = 1
newpoint.CanCollide = false
end			
  1. Not really, the AI traffic I am working on is purely to make the game more lively. The direction they will take at the next intersection is randomised on spawn and per each intersection they pass through.
2 Likes

Pathfinding is unnecessary here, the only information you need to have are:

  • Traffic nodes
  • Their connections (as otherwise they could take an ‘illegal’ turn)

You can completely randomize which connection should be taken (if not behind), and then calculate the steering angle to the next waypoint.

Using this information, the end result is the following:

Yes, it’s actually that simple

8 Likes

wow, now thats more like it thanks! Question tho, I already studied how roblox’s endorsed model car chassis thanks to your police AI post, i learnt how the car’s steering and propulsion functions work. I just wanna know this:

How do you calculate the steering angle?

To me this is useful if i want to make a free roam game. Roblox games need more ais than usual. But right now its just a project i wanna do.

4 Likes

In my current system, at curves, I merely set the car’s orientation to the curve part’s orientation, with Vector3:Dot() to make sure the car points in the right direction. It’s not the best implementation (as it “teleports” between orientations), but is a lot better than overcomplicating it with sensors at every curve.
At junctions, I have sensors with an “end angle” value in them. When a car hits that sensor, it’ll keep steering until the desired angle is reached.

2 Likes

Ohh thanks! Ill make sure to follow those methods

2 Likes

is it like this

SteeringAngle = math.acos(math.clamp(Vector1.Unit:Dot(Vector2.Unit), -1, 1))
2 Likes

Not really. I only used the Dot product to determine whether to use a negative degree or a positive degree for the orientation. You don’t actually need to use it in your system, it’s up to you how you design it.

2 Likes

Are you a gangster or something?
Why would you want more than one car in a 4-way-stop intersection?!

2 Likes

Because why not? It’s actually legal in America atleast to proceed at the same time as someone else if you’re not cutting across their path. You are referring to my older video, right?

2 Likes

Instead of the NPC controlling the car just make the car an NPC

2 Likes