# How do I let cars drive on a street (AI)

Just started using Construct 2? Post your questions here

### » Wed Aug 13, 2014 11:07 pm

Hi Guys,

i'm just getting into path finding with construct 2 and I have logical question when build my AI for cars which should be able to drive through a city.

See the picture attached (cars_street.JPG). I have some checkpoints (red rectangles) to find the path and move my car along to it. I used to tutorial https://www.scirra.com/tutorials/1118/page-1 to achieve this goal. The car is driving from one checkpoint to another (top corner then left corner etc.).

This is quite a cool feature but I want more, but have no idea how to solve it with construct 2.

- I want to let cars drive in a random way (not only from checkpoint 1 to checkpoint 2 to checkpoint 3 and so on). I want to let the cars find there own way: lets say on each cross-way the car changes its direction or if that is not possible just drive on the street to a random checkpoint.
--> Let my car drive to random checkpoints is a little bit buggy because sometimes the car drives not on the street but on the road curb - how to let my cars drive ONLY on the street, which is shown on cars_street.JPG?
- For performance reasons: just move cars which are in the layout and I'm able to see in my current resolution.

Any tips on how to achieve my new goals?

Thanks!
You do not have the required permissions to view the files attached to this post.
B
5
Posts: 5
Reputation: 235

### » Thu Aug 14, 2014 1:39 am

You can randomize targets easily with System: Pick Path_Car where Path_Car.path = random(1, max_path)

For choosing a path at a crossroad, if you have 3 directions, just choose(1,2,3) or similar (say, 1 left, 2 straight, 3 right). You could store in each Path_Car, its available destination Path_Car(s).

Also rather than check for overlapping with Path_Car once the car arrives, I'd set it when you first choose the target (suggestion).

For performance, C2 won't render what's not onscreen, and you can't pathfind to something offscreen anyway.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

### » Thu Aug 14, 2014 9:56 am

Hi codah,

if I randomize my new target with random() [Pick Path_Car where Path_Car.path = random(1, max_path)] the car just stays at the current checkpoint and does not move forward. If i set it to 4 the car drives to this checkpoint. Also it seems that the car is ignoring my defined obstacles like it's shown in the picture attached.

You do not have the required permissions to view the files attached to this post.
B
5
Posts: 5
Reputation: 235

### » Thu Aug 14, 2014 10:03 am

I fixed random movement with the screenshot attached (using now Pick random instance) - movement is not very realistic.

But the obstacle is ignored.
You do not have the required permissions to view the files attached to this post.
B
5
Posts: 5
Reputation: 235

### » Thu Aug 14, 2014 10:53 am

nilzyo wrote:I fixed random movement with the screenshot attached (using now Pick random instance) - movement is not very realistic.

But the obstacle is ignored.

soo.. what's the current status?
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

### » Thu Aug 14, 2014 3:09 pm

Hi,

everything works fine BUT the car is moving over the road curb (its Sprite2) to its checkpoint, although I have definied sprite2 as obstacle (I guess when I define somethin as obstacle then my car should NOT drive over it to drive to its checkpoint?)

You can see this behaviour in the attached image.
- Car drives to Checkpoint (Path_Car) (which is marked as "1" here)
- Arrived at checkpoint 1 the new destination checkpoint 4 (marked with "4" in the image) is choosen and the car begins to drive over the road to the checkpoint
- the blue line shows the movement of the car (the car ignores the defined obstacle?!) and drives over Sprite2
You do not have the required permissions to view the files attached to this post.
B
5
Posts: 5
Reputation: 235

### » Thu Aug 14, 2014 11:21 pm

In the pathfinding tutorual, grab the piece of code under 'show grid' and trigger it in your code. This will show you where it thinks the obstacles are.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596