# How do I make cars drive on roads

Get help using Construct 2

### » Sat Feb 20, 2016 3:49 am

So I want a car to drive along a road from point A to point B, how can I get them to move only on the roads and turn the direction they need to at intersections? It seems like path finding won't work for this so I'm not sure what to do
B
27
S
12
Posts: 222
Reputation: 2,624

### » Sat Feb 20, 2016 3:57 am

This can be a simple fix or a very very advanced problem depending on what exactly you need. You'll need to be more specific, or post a capx. Are your roads straight? How is your car being controlled? Are there multiple paths to the destination?

This is a quick trick - make an invisible sprite at each intersection. You can use these invisible sprites to check for collision with the car and do an action when the car reaches the intersection.
B
60
S
30
G
134
Posts: 1,949
Reputation: 75,375

### » Sat Feb 20, 2016 4:14 am

Well the main issue is having the car decide which way to turn because in the program players place down roads so I can't just have it manually programmed in for a car to turn left or right, I need some way for the car to find the shortest route to it's destination with the roads available
B
27
S
12
Posts: 222
Reputation: 2,624

### » Sat Feb 20, 2016 4:26 am

I think you're in the very advanced problem zone =P

How are your roads placed? Are there constraints like do they need to be connected at ends only, is there a limit to how many roads per intersection?

When you create an intersection, you'll need some information there including which direction each option is in, and how long that road segment is.

Basically every time you place a road, you will need to add information at the intersection it was placed on.

You'll also need to create your own pathfinding - A "simple" pathfinding algorithm would find every possible path to the destination, and compare the total values (length?) of each segment of road to pick the path with the lowest total number. This algorithm should record which road to take at each intersection in a variable or array, which the car then references to make its drive.

Given that your roads are straight, you can have the car angled at the same angle as the chosen road and have it move forward until it hits another intersection.
B
60
S
30
G
134
Posts: 1,949
Reputation: 75,375

### » Sat Feb 20, 2016 4:36 am

Hmmm ok well what kind of algorithm could do that? is there anyway to measure that distance?
B
27
S
12
Posts: 222
Reputation: 2,624

### » Sat Feb 20, 2016 4:44 am

Again, how are your roads being created/placed? For example if it is like tetris where a player gets a random piece of road to put down, that distance could be already associated with that piece of road as an instance variable. Or you can use sprite width/height. Or you can calculate pixel distance with imagepoints. Depends on your project.

Edit/Disclaimer: I'm no expert on pathfinding, so if anyone knows better please chime in

You can search online for pathfinding algorithms, it is a VERY common problem in the history of programming and well documented as well.
B
60
S
30
G
134
Posts: 1,949
Reputation: 75,375

### » Sat Feb 20, 2016 3:50 pm

The player can place down a piece of road wherever they want and the road automatically connects to the pieces next to it (not diagonally)
B
27
S
12
Posts: 222
Reputation: 2,624

### » Sat Feb 20, 2016 4:54 pm

Is there any way to just make the pathfinding only move at 90 degree angles? disabling diagonals doesn't actuallly do that
B
27
S
12
Posts: 222
Reputation: 2,624

### » Sat Feb 20, 2016 5:04 pm

very good question.

I know you can set borders that are 90 degrees but event then it still likes to cut corners some times.

it all comes down to cell size and cell spacing.
B
29
S
14
G
10
Posts: 357
Reputation: 8,406

### » Sat Feb 20, 2016 10:12 pm

I've got it more or less figured out, they don't drive totally straight but by doing what you suggested with the cell size I managed to get them to stay on the roads
B
27
S
12
Posts: 222
Reputation: 2,624

Next