• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need Help with my Traffic AI, Please.

S

speedysilwady

Guest
Hi everyone, I am having trouble making my traffic AI work. Was wondering if anybody had any suggestions as to what I did wrong.

Its a top down driving game and all the vehicles spawn above the room and drive downwards through four lanes. Basically this code determines if there is another traffic object above or below or to the right or left of the object as well as any road barriers, and how the object should behave. I wanted the object to switch lanes if it was below another car. If there are no lanes available I wanted it to maintain a distance between the car below it.

If it is below a car I want the traffic object to slow down and maintain a distance as well.

Code:
// STEP EVENT
  
 if !instance_place(self.x,y-160,oTraffic) || !instance_place (self.x,y+64, oTraffic) {slowing=0;self.switching=0;}//no traffic above or below, then keep moving
     if (instance_place(self.x,self.y-160,oTraffic)) {self.switching=1;} //If theres traffic above me self.switching = 1
     if (instance_place (self.x,self.y+64, oTraffic)) {self.switching=2;} //If theres traffic below me self.switching = 2

  
   switch (self.switching) {
     case 1: 
     {
     if !instance_place(self.x+1,self.y-160,oBarrier) && !instance_place(self.x-65,self.y-160,oBarrier)
     {//if traffic is above me then i need switch to another lane
            if !instance_place (self.x+32,self.y-160,oTraffic) && !instance_place (self.x-96,self.y-160,oTraffic) 
              {hspeed+=choose(20,-20);image_blend =c_red;} //free lane to the left and right of me, pick a lane
            if instance_place (self.x+32,self.y-160,oTraffic) && !instance_place (self.x-96,self.y-160,oTraffic)
                {hspeed-=20;image_blend = c_lime;} // free lane to the left of me, move left
            if instance_place (self.x-96,self.y-160,oTraffic)  && !instance_place (self.x+32,self.y-160,oTraffic)   
                 {hspeed+=20;image_blend = c_lime;} // free lane to the right of me, move right
          else {swiching=2;}
          }
     }
          
     case 2:{
        hspeed=0;vspeed=5;slowing=1;image_blend=c_black;}
     case 0: {
     if !(instance_place (self.x,self.y+64, oTraffic)){
         if abs(oPlayer.vspeed<8 && oPlayer.vspeed>0){self.vspeed=oPlayer.vspeed*tmult1;hspeed=0;image_blend=c_blue;}
        else {self.vspeed=tmult2;hspeed=0;}
     }
    else switching=2;
    }
}
 
S

speedysilwady

Guest
You do not need self. everywhere. Write just x, y, vspeed etc. I think instance_place do not work in this way. You need to set varible, but you should use place meeting or collision point. Check here and read it. https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_place.html
Yeah the self thing was just me getting desperate trying to troubleshoot lol.

I was previously using instance_position but it was not working for some reason. I can try place meeting and collision point to see if either works. Thank You

After changing to place_meeting, everything is working better, switching gets set to the appropriate number depending on the situation. But the other traffic object seems to not want to switch lanes even if there is an open lane which is what I was trying to achieve. Not sure what else I am missing..
 
Last edited by a moderator:
W

weiner

Guest
Instance_position need variable too, because it is returning id. Maybe there is problem in another code. Blending is working? Maybe you should try put else between checking left+right / left / right.
 
S

speedysilwady

Guest
I tried to clean it up a bit. I assigned instance_position to a variable and it just ignores everything when I do...I tried replacing with position_meeting and it would cause the cars to brake a bit but still collide with the car below and not obey any of the rules.

Some additional info:

The traffic sprite for now is a placeholder. vertical rectangle 64 pixels wide by 96 pixels tall.The origin of the sprite is at 64 and 96 which is the bottom right side of the sprite.

There are four - 128px wide lanes and each car is centered perfectly 64 pixels apart on each of the four lanes.

Code:
if global.started=1{
     if thealth>0{
     rlanef = !instance_position (x+64,y,oTraffic) && !instance_position(x+1,y,oBarrier) && !instance_position(x-65,y,oBarrier)  // right lane free 
     llanef = !instance_position (x-128,y,oTraffic) && !instance_position(x+1,y,oBarrier) && !instance_position(x-65,y,oBarrier)// left lane free
 if !instance_position(x,y-160,oTraffic) || !instance_position (x,y+64, oTraffic) {slowing=0;switching=0;}//no traffic above or below, then keep moving
     if (instance_position(x,y-160,oTraffic)) {switching=1;} //If theres traffic above me switching = 1
     if (instance_position (x,y+64, oTraffic)) {switching=2;} //If theres traffic below me switching = 2

 
   switch (switching) {
     case 1: //if traffic is above me then i need switch to another lane
     {
               
            if llanef=1 && rlanef=1 {hspeed+=choose(20,-20);} //free lane to the left and right of me, pick a lane
            else if llanef=1 && rlanef=0    {hspeed-=20;} // free lane to the left of me, move left
            else if rlanef=1 && llanef = 0  {hspeed+=20;} // free lane to the right of me, move right
            else if llanef=0 && rlanef=0 {swiching=2;}
 
     }
         
     case 2:{ // if traffic is below me (they should switch lanes, and I should brake
                hspeed=0;vspeed=5;slowing=1;
            }
     case 0: { // if the road is free continue to accelerate downwards
   
                 if !(instance_position (x,y+64, oTraffic)){
                 if abs(oPlayer.vspeed<8 && oPlayer.vspeed>0){vspeed=oPlayer.vspeed*tmult1;hspeed=0;image_blend=c_blue;}
                    else {vspeed=tmult2;hspeed=0;}
                        }
                        else switching=2;
              }
}
}
if thealth<=0 {instance_destroy();}
if slowing=1 {image_index=2;}
if hspeed=0 && slowing==0 {image_index=0;}
if hspeed<0 {image_index=1;}
if hspeed>0 {image_index=3;}
}
 
W

weiner

Guest
Change (l,r)lanef=1 to lanef!=noone and lanef=0 to lanef = noone. It is returning ID of object. So it returns somethink like this: llanef=100012 maybe. And in case 0 you are using instance position again, where it should not be.
 
Top