Search Unity

A* Traffic AI

Discussion in 'Navigation' started by TNec, Oct 25, 2015.

  1. TNec

    TNec

    Joined:
    Oct 25, 2015
    Posts:
    2
    Hi,
    I'm working on a simple traffic AI using A* algorithm for path finding for my "winter hobby project". I got it to work (in terms of pathfinding) but now I'm stuck. I have no idea how to make the path go in the middle of the lane, how to stick to the proper side of the road etc. So far it's just using the shortest way to the target position.
    I was thinking about creating waypoints on each intersection with entry and exit points but I don't know how to make the A* path to use those waypoints. Ideally I'd like to create lanes (maybe using waypoints?) and be able to set some properties like lane direction, allowed speed etc.
    I've googled, I've searched the forum and documentation here and on http://www.arongranberg.com/ (A* project) but I didn't find anything helpful and related to my case (except of couple unanswered questions on topic similar to mine).
    I don't expect complete code examples or instructions step by step, just couple phrases or concepts I should focus on and do some research. I'm new to Unity but not to programming, so I can handle rest on my own (I think ;) ). Of course if someone can provide some code samples or some descriptive ideas I will be very grateful.
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    I assume you are doing a grid based A*. Yould could tag grid points with some layers telling which direction they can go.Say a tile can be used to go from left to right, but not from right to left, or bottom / top, or the combination you want of it. And when searhing through that tile with A*, check if it's direction tag matches the movement direction you want to make, if not, ignore that tile for now, because you can't reach it from where you are, because you cant traverse it through that direction.

    This way you can tag a side of the road with one direction only.
     
  3. TNec

    TNec

    Joined:
    Oct 25, 2015
    Posts:
    2
    Interesting approach, thanks.
    I only don't understand the part "And when searching through that tile with A*". I'm using tutorial scripts for now, so the searching process looks like this:
    seeker.StartPath (transform.position,targetPosition, OnPathComplete);

    I've tried using grid graph, but couple days ago I figured out that it'd probably better to use Point Graph. It seems more intuitive to create "lanes" this way and also takes care of car sticking to the middle of the lane. But still I don't know how to create a path that uses right lane. Do you think this approach may apply also here?

     
  4. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Same approach applies to point graphs. Dont expand point neighbors that you cant go to.

    In your image, from that point right in front of the car, you could either go to the left, or to the green line. The red line would not be a valid neighbor to go from that point. It could even not be on the neighbors list of that point if that would make it any easier.
     
  5. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37