Traffic Simulation

edited February 2018 in Questions about Code

Hi, I am new in processing, I want to create car simulation at intersection but i am confused to handle moving object to turn left/right with rotate function. I wrote simple code but it didn't run well. May be you can help me to solve the problem I really appreciate it. Thanks

my simple code

float x, y = 50, w = 40, h = 30;
float angle1=0.0;

void setup() {
  size(1000, 700);
  rectMode(CENTER);

}//func

void draw() {
  background(0);

  fill(255); 

  //display
    translate(x, y);
    rotate(angle1); 
    println("frameCount=" +frameCount +" angle="+angle1 + " x="+x + " y=" +y);

    fill(200, 0, 0);//red
    rect(0, 0, w, h);

  //move
  if (frameCount<300) {
      x++; 
      //y++;
      //angle1 += 0.01;
    } else if (frameCount<=460) {
      x+=1; 
      y++;
      angle1 += 0.01;
    } else if(frameCount<900){
      w = 30;
      h = 40;

      x = 460;
      y++;
      angle1 = 0.0;
    }
}

Answers

  • void setup(){
      size(400,400);
      noStroke();
      fill(200,0,0);
      rectMode(CENTER);
    }
    
    void draw(){
      background(0);
      translate(200,200);
      rotate(map(millis()%4000,0,4000,0,TWO_PI));
      rect(100,0,30,40);
    }
    
  • int state = 0;
    int time = -1;
    
    void setup() {
      size(400, 400);
      noStroke();
      fill(200, 0, 0);
      rectMode(CENTER);
    }
    
    void draw() {
      background(0);
      translate(200, 200);
      if ( state == 1) {    
        if ( millis() - time >= 1000 ) {
          state = 2;
        } else {
          pushMatrix();
          rotate(map((millis()-time)%1000, 0, 1000, 0, HALF_PI));
          rect(100, 0, 30, 40);
          popMatrix();
        }
      }
      if ( state == 0 ) {
        rect(100, 0, 30, 40);
      }
      if ( state == 2 ) {
        rect(0, 100, 40, 30);
      }
    }
    
    void mousePressed() {
      if ( state == 0 ) {
        time = millis();
        state = 1;
      }
    }
    
  • Answer ✓
    final int DUR = 1500;
    
    int state = 0;
    int time = -1;
    
    
    int px, py, dx, dy;
    
    void setup() {
      size(400, 400);
      noStroke();
      fill(200, 0, 0);
      rectMode(CENTER);
      px = 100;
      py = -240;
      dx = 0;
      dy = 1;
    }
    
    void draw() {
      background(0);
      translate(200, 200);
      rotate(-HALF_PI);
      px+=dx;
      py+=dy;
      if ( state == 0 && py >= 0 ) {
        state = 1;
        time = millis();
      }
      if ( state == 1) {    
        if ( millis() - time >= DUR ) {
          state = 2;
          px = 0;
          py = 100;
          dx = -1;
          dy = 0;
        } else {
          pushMatrix();
          rotate(map((millis()-time)%DUR, 0, DUR, 0, HALF_PI));
          rect(100, 0, 30, 40);
          popMatrix();
        }
      }
      if ( state == 0 ) {
        rect(px, py, 30, 40);
      }
      if ( state == 2 ) {
        rect(px, py, 40, 30);
      }
    }
    
  • Thanks for your answer, it helped me a lot :)>-

  • Hi @TfGuy44 I'm sorry to bother you again. I still do not understand how the object can move to the left .. I tried many times but it always does not work ..Thank a lot

  • I tried many times but it always does not work

    In this situations the best thing to do is to provided an MCVE demonstrating your problem.

    Kf

  • When you set dx to negative it moves left

    Because you add dx to px

    But show your entire code

  • Actually I am a bit confused to do the rotation .. How to rotate 90 degrees but to the left.

  • Answer ✓

    here is another approach

    it holds an event table that tells after how many milliseconds the car has to turn or drive etc.

    ArrayList<Event> events = new ArrayList(); 
    
    final int DUR = 1500;
    
    int time = -1;
    
    // car pos and velocity
    int px=100, py=70, 
      dx=1, dy=0;
    
    int centerX, centerY;
    String remark="";
    int current_i; 
    boolean lightsOn=false; 
    
    // the orientation of the car 
    final int north = 0;
    final int east  = 1; 
    final int south = 2; 
    final int west  = 3;
    int orientation = east; 
    
    float angleFrom, angleTo;
    
    int myMillis;
    
    color backgroundColor=255; 
    boolean itsNight=false;
    
    // --------------------------------------------------------
    
    void setup() {
    
      size(1500, 1000);
    
      noStroke();
      fill(200, 0, 0);
      rectMode(CENTER);
    
      events.add(new Event(0, "background255")); // event starts at zero and has a duration of 30
      events.add(new Event(60, "goRight"));       // event starts at 30 and has a duration of 2220
      events.add(new Event(2220, "turnRight"));    // event starts at 2220 and has a duration of DUR (every rotation has that duration)
      events.add(new Event(2220+DUR, "go"));
      events.add(new Event(2220+DUR+2000, "turnLeft"));
      events.add(new Event(2220+DUR+2000+DUR, "goRight"));
      events.add(new Event(2220+DUR+2000+DUR+811, "turnRight"));
      events.add(new Event(2220+DUR+2000+DUR+811+DUR, "go"));
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811, "turnRight"));
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR, "goLeft") );
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR+5522, "background0")); 
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR+5552, "turnRight") );
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR+5552+DUR, "goUp") );
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR+5552+DUR+5555, "turnRight") );
      events.add(new Event(2220+DUR+2000+DUR+811+DUR+811+DUR+5552+DUR+5555+DUR, "reset") );
    
      myMillis=millis();
    }
    
    void draw() {
    
      background(backgroundColor);
    
      int myTime = millis() - myMillis; 
    
      fill(255, 0, 0); 
      text("Millis: "+myTime+", "+remark+" ("+current_i+")", 19, 19);
    
      if (orientation>3) 
        orientation = 0;
    
      for (int i=events.size()-1; i>=0; i--) {
        Event e = events.get(i); 
        if (myTime >= e.from) {
          if (i!=current_i) {
            execute1("init_"+e.doCmd);
          }
          current_i=i;
          remark=e.doCmd;
          execute1(e.doCmd);
          break;
        }//if
      }//for
      //
    } //draw
    
    //---------------------------------------------------------------------------
    
    void execute1(String doCmd1) {
      //
      switch(doCmd1) {
    
      case "reset":
        myMillis=millis();
        break; 
    
      case "goRight":
        px+=dx;
        py+=dy;
        orientation=east; 
        fill(200, 0, 0);//red
        rect(px, py, 40, 30);
        fill(255);
        //rect(px+20, py-14, 2, 2);
        //rect(px+20, py+14, 2, 2);
        break;
    
      case "goLeft":
        px+=dx;
        py+=dy;
        orientation=west; 
        fill(200, 0, 0);//red
        rect(px, py, 40, 30);
        fill(255);
        //rect(px+20, py-14, 2, 2);
        //rect(px+20, py+14, 2, 2);
        break; 
    
      case "init_background0":
      case "init_background255":
      case "init_reset":
      case "init_go":
      case "init_goRight":
      case "init_goLeft":
      case "init_goUp":
        //ignore
        break;
    
      case "background0":
        backgroundColor=0;
        itsNight=true; 
        break;
    
      case "background255":
        backgroundColor=255;
        itsNight=false; 
        break;
    
      case "goUp":
        px+=dx;
        py+=dy;
        orientation=north; 
        fill(200, 0, 0);//red
        rect(px, py, 30, 40);
        fill(255);
        break; 
    
      case "go":
        px+=dx;
        py+=dy;
        fill(200, 0, 0);//red
        rect(px, py, 30, 40);
        fill(255);
        break;
    
      case "init_turnRight":
        if (orientation==east) {
          centerX=px-0;
          centerY=py+100;
          angleFrom=  -HALF_PI; 
          angleTo=  0;
          dx=0;
          dy=1;
        } else if (orientation==south) {
          centerX=px-100;
          centerY=py+0;
          angleFrom=  0; 
          angleTo=  HALF_PI;
          println("south");
          dx=-1;
          dy=0;
        } else if (orientation==west) {
          centerX=px-0;
          centerY=py-100;
          angleFrom=  HALF_PI; 
          angleTo=  HALF_PI+HALF_PI;
          println("west");
          dx=0;
          dy=-1;
        } else if (orientation==north) {
          centerX=px+100;
          centerY=py-0;
          angleFrom=  HALF_PI+HALF_PI; 
          angleTo= HALF_PI+HALF_PI+HALF_PI;
          println("north");
          dx=1;
          dy=0;
        } 
        time=millis(); 
        orientation++;
        break; 
    
      case "init_turnLeft":
        centerX=px+100;
        centerY=py-0;
        time=millis(); 
        orientation--;
        break; 
    
      case "turnRight":
        translate (centerX, centerY);
        rotate(map((millis()-time)%DUR, 
          0, DUR, 
          angleFrom, angleTo));
        fill(200, 0, 0);//red
        rect(100, 0, 30, 40); //  
        px=int(screenX(100, 0));
        py=int(screenY(100, 0));
        break;
    
      case "turnLeft":
        // facing south
        dx=1;
        dy=0; 
        translate (centerX, centerY);
        rotate(map((millis()-time)%DUR, 
          0, DUR, 
          HALF_PI+HALF_PI, HALF_PI));
        fill(200, 0, 0);//red
        rect(100, 0, 30, 40); // 
        px=int(screenX(100, 0));
        py=int(screenY(100, 0));
        break;
    
      case "lightsOn":
        lightsOn=true; 
        break; 
    
      case "lightsOff":
        lightsOn=false; 
        break;
    
      default:
        println("not found "
          +doCmd1);
        exit(); 
        break;
      }//switch
    }//func 
    
    //=========================================
    
    class Event {
    
      int from; 
      String doCmd;
    
      //constr
      Event(int millis1_, String cmd_) {
        from  = millis1_; 
        doCmd = cmd_;
      }//constr
    }//class
    
    //=========================================
    
  • thank you very much for all your help

  • you're welcome!

    The turn left option doesn't work in all situations (only when the car is going south) - look at "init_turnRight" in switch to see how it's done.

Sign In or Register to comment.