How do I make cars drive on roads

0 favourites
  • 11 posts
From the Asset Store
10 looping royalty free retro driving inspired tracks.
  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • Hmmm ok well what kind of algorithm could do that? is there anyway to measure that distance?

  • 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.

  • 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)

  • Is there any way to just make the pathfinding only move at 90 degree angles? disabling diagonals doesn't actuallly do that

  • 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.

  • 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

  • No I'm trying to figure out how to make them stop when there's another car in front of them

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)