Randomized AI cars + functional traffic signals: Doable?

0 favourites
  • 4 posts
From the Asset Store
The official Scirra Multiplayer Signalling Server for helping peers find and connect to each other
  • Dear all:

    I am part of a non-profit program for kids' safety. One of my pet projects is to bring a safe walking game to the program, based around real life scenarios. Enter Construct 2 and my willingness to learn a program I've never worked with before. That said, I've worked with simple code in Microsoft Train Simulator and PHP for Wordpress before, so the theories behind code principle and event hierarchy are not entirely foreign to me, and Construct 2's learning curve hasn't been too difficult up to now.

    However, I've come to a bit of a standstill with AI control; hence, before I try to go any further with the game project, I thought I'd ask if my goals are even remotely possible.

    At present, the game places a player (with 8-position control) on the sidewalk on a top-down map, with single-lane streets and bike lanes in either direction. The street, bike lane, and sidewalk are all individual objects. Each crosswalk signal has two animations (walk/don't walk, alternating between each direction), and each traffic signal has three (green/yellow/red), and the animations are timed together so that the automobile traffic light is red when the crosswalk is safe to cross. The walker/player is at the left, on the sidewalk:

    The green lines are solids, there to assist me when trying pathfinding experiments.

    The behaviors applied to each BlueCar sprite are: Solid / Car / LineOfSight. I also have max/minimum speeds listed as instance variables.

    The above is the rough map of where I've been able to get after a few days of fiddling with the program, but I've been having nothing but problems trying to work out AI.

    Following is my laundry list of questions, which I sincerely hope can be answered before I spend ages trying to attempt something that may be impossible - and before I tell my administrators to buy the full version of Construct 2:

    • Pathfinding isn't working for me. I've experimented by placing solid lines against the edge of the road terminating at the intersection, but when I can get a car to move to an X,Y coordinate, it'd rather bump up against the solid line all the way to the intersection rather than avoid it.
    • Is there a function to turn on the visualization of cell obstacles, as shown in the Pathfinding tutorial? I can't find it. I'm suspecting that there are not enough cell obstacles surrounding the car to make it reliable, but I can't verify that without being able to see the buffer zone.
    • I've tried copying Pode's brilliant CarAI.capx with targets within waypoints. In doing so, I was able to get a car to travel down a street in a straight line, and make a right turn. However, I'm not really sure if its possible (with this method) to randomize (or alternate) whether a car turns left, right, or continues straight ahead. I tried using animations to change the state of each target, but I probably borked up the conditionals. Should I continue trying this?
    • Million dollar question: Is it possible to randomize whether a car continues ANY path reliably with ANY method mentioned above?
    • Two million dollar question: If any of the above is actually possible, can a code be written to stop cars at an intersection stop line sprite, such as the one below...
      ...when the traffic light for that direction is red? I know I can tie in an event to the animation frame of the red traffic signal, but - so far - I have not been able to figure out how to write a conditional stating code to the effect of "BlueCar speed = 0 only if touching the stop line sprite when trafficsignal001horizontal animation frame = 1" though I'd assume this is how it would be done (am I completely off track here?)
    • Any automatically generated cars (e.g.: System, Every 5 Seconds | Create object BlueCar on Layer 3 at (X,Y)) ignore any and all events before them. Besides being a pain, it has limited the scope of my experiments. I think I read something about automated system events not taking on prior events before them, but I never did find an obvious solution for this in the forums (though I seem to remember a thread about using the "System: On every tick" event to solve it). What have I missed?
    • Because of this limitation with the auto-generated cars, I can't get any automated car to adhere to line-of-sight rules. Thus, I'll get a big pileup of cars smashing and pushing behind the one car generated on the layout - provided that layout-based sprite stops.
    • This also brings up another thought: I don't mind making the cyclists follow straight paths exclusively; thus, I can generate them traveling in fixed directions from their spawn point. That's easy enough, and whatever code can stop the cars at the intersection can also stop cyclists. Thing is: Can I apply line-of-sight to an auto-generated object (the cars) to stop them in the middle of left and right turns on green to allow a cyclist to travel straight through the intersection?
    • I'd like to have about 8 or 9 intersections in this map before I'm done, allowing a player to travel from "home" to "school," along with a semi-city/neighborhood environment between the two. Thus, ANY of the code mentioned above reliable and simple enough (relatively speaking) that it can be coded to work with said nine intersections.
    • I'd like to spawn the cars off map, but - near as I can figure it - I can't do that if the car also has the Destroy Outside Layout behavior enabled as well, correct?
    • Bonus points if the cars can be made to pull into and out of parking lots and spaces. Yes, I know I'm asking too much.

    Let me know if I'm way off track, or whether there is hope for this project yet.

    -Kurt

  • Should all be possible, with a bit of trial and error..

    You could make the line of sight behaviour spot the intersection stop line sprite and when the light is red decellarate the car to stop before it..

    On collision with the intersection stop line sprite you could choose which direction the car will turn and add the events to do so (which would probably be a bit different depending on the car-speed)

    Adding events with conditions for when the bicycle is within the path of the car should also be possible..

    It will take a lot of work though, especially using the car behaviour for the movement..

    You might be better of creating your own movement with the custom movement behaviour..

  • Should all be possible, with a bit of trial and error..

    You could make the line of sight behaviour spot the intersection stop line sprite and when the light is red decellarate the car to stop before it..

    On collision with the intersection stop line sprite you could choose which direction the car will turn and add the events to do so (which would probably be a bit different depending on the car-speed)

    Adding events with conditions for when the bicycle is within the path of the car should also be possible..

    It will take a lot of work though, especially using the car behaviour for the movement..

    You might be better of creating your own movement with the custom movement behaviour..

    I gave it a try again, and I was able to get it to work - as a car (custom movement seemed to be a bit potentially overcomplex) - with a number variable running off the animation frame and LineOfSight:

    I also placed a sprite in the intersection (target3 above) to turn the car, but "Simulate Car pressing Steer Right" refuses to work, no matter what I've set. As you can see in the events above, I checked to see that the target was even functioning by testing it with the Car Speed and Rotate 90 degrees (the latter too jarring a turn to be a substitute for the steer function) - those two functions DO work, but Steer Right doesn't.

    EDIT: LineOfSight is giving trouble as well. Even after the car clears the two stop lines, it will decelerate again for the next red light cycle. I have the cone of view set to 20 degrees, so it shouldn't do that at all.

    Same seems to apply for gradual acceleration - it doesn't work, hence the fixed set speed.

    Any ideas?

    -Kurt

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, somehow I was able to get it to work simply by re-ordering the events. I'll post a photo of the revised order of events later today or this week.

    However, I have a new problem: The first two cars on the map (one already on the map, the other auto-generated) work fine. The remainder of the auto generated cars (#3 and up) do not reflect the Car Speed = 350 event or StopBarHorizontal, and simply roll to a stop in the middle of the layout. Strangely enough though, these cars WILL follow the steer commands if they overlap target3 on animation frame #1.

    Again, I'm stumped. Any ideas?

    -Kurt

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