1

I made a program of interactive traffic lights, basically there is an intersection between two roads and there are 4 traffic lights and 4 cars, one of them going from right to left, another from left to right, another from top to bottom, and another from bottom to top. I have already programmed the functions but when I run the program in the first seconds everything looks fine but then I notice that the cars that are going from top to bottom and bottom to top are moving forward in relation to the X-axis for some reason that I can't explain. I will leave images for you to understand. After seeing this happen I thought that it is probably the way I am moving the objects around but I am not sure. I know the code doesn't follow the conventions but my teacher asked for it this way. I'm sorry because I think this should be basic but I started processing a few days ago and I have been searching and seeing other posts and it didn't help at all...

First tab:

final color WHITE = color(255,255,255);
final color GREEN = color(0,255,0);
final color DARK_GREEN = color(0,55,0);
final color RED = color(255,0,0);
final color YELLOW = color(255,255,0);
final color DARK_GREY = color(100,100,100);
final color GREY = color(200,200,200);
final color BLACK = color(0,0,0);
final int W_WIDTH = 1000;
final int W_HEIGHT = 700;

carro c1 = new carro(W_WIDTH/2 - 150,W_HEIGHT/2,150,10,0,RED);
carro c2 = new carro(W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,3,270,GREY);
carro c3 = new carro(W_WIDTH/2 + 75,W_HEIGHT/2 - 150,150,5,90,WHITE);
carro c4 = new carro(W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,5,180,GREEN);
carro carros[] = {c1,c2,c3,c4};
lane l1 = new lane(350,W_HEIGHT/2 + 150,150,150,150,0,2,2,2,GREEN,W_WIDTH/2 - 150,W_HEIGHT/2,150,10,0,RED);
lane l2 = new lane(650,W_HEIGHT/2 + 300,150,150,150,270,2,2,2,RED,W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,3,270,GREY);
lane l3 = new lane(500,W_HEIGHT/2 - 150,150,150,150,90,2,2,2,RED,W_WIDTH/2 + 75,W_HEIGHT/2 - 150,150,5,90,WHITE);
lane l4 = new lane(800,W_HEIGHT/2,150,150,150,180,2,2,2,GREEN,W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,5,180,GREEN);
lane lanes[] = {l1,l2,l3,l4};

void settings()
{
  size(W_WIDTH,W_HEIGHT);
}

void setup(){}

void update()
{
  for(int i = 0; i < lanes.length;i++)
  {
    lanes[i].update();
  }
}

void draw()
{
  update();
  noStroke();
  background(DARK_GREEN);
  fill(DARK_GREY);
  rect(0,W_HEIGHT/2,1000,150);
  fill(DARK_GREY);
  rect(W_WIDTH/2,0,150,700);
  for(int i = 0; i < carros.length; i++)
  {
    carros[i].draw();
  }
  for(int i = 0; i < lanes.length; i++)
  {
    lanes[i].draw();
  }
}

Second tab:

class carro
{
  float x;
  float y;
  float velocidade_max;
  float velocidade_atual;
  float angle;
  color c;
  
  carro(float x,float y,float velocidade_max,float velocidade_atual,float angle,color c)
  {
    this.x = x;
    this.y = y;
    this.velocidade_max = velocidade_max;
    this.velocidade_atual = velocidade_atual;
    this.angle = angle;
    this.c = c;
  }
      
  void wrapAroundScreenx() {
    if (x > width) {
      x = 0;
    } else if (x < 0) {
      x = width;
    }
    
    if (y > height) {
      y = 0;
    } else if (y < 0) {
      y = height;
    }
  }
  
  void draw()
  {
  pushMatrix();
  translate(x,y);
  rotate(radians(angle));
  fill(c);
  rect(0, 10, 100, 50);
  fill(BLACK);
  fill(c);
  rect(101,15,25,40);
  fill(BLACK);
  rect(70,60,20,5);
  fill(BLACK);
  rect(15,60,20,5);
  fill(BLACK);
  rect(70,5,20,5);
  fill(BLACK);
  rect(15,5,20,5);
  popMatrix();
  }
}

Third tab:

class lane extends carro
{
  float positionx;
  float positiony;
  float alphav;
  float alphaa;
  float alphaver;
  float angleS;
  float timev; //tempo que cada luz fica aberta
  float timea;
  float timever;
  float startTime;
  float activeLight;
  float cor_inicio;
  
  lane(float positionx,float positiony,float alphav,float alphaa,float alphaver,float angleS,float timev,float timea,float timever,float cor_inicio,float x,float y,float velocidade_max,float velocidade_atual,float angle,color c)
  {
    super(x,y,velocidade_max,velocidade_atual,angle,c);
    this.positionx = positionx;
    this.positiony = positiony;
    this.alphav = alphav;
    this.alphaa = alphaa;
    this.alphaver = alphaver;
    this.angleS = angleS;
    this.timev = timev;
    this.timea = timea;
    this.timever = timever;
    this.cor_inicio = cor_inicio;
  }

void update()
{
  float elapsedTime = millis() - this.startTime; // calcula o tempo decorrido
  float currentSecond = elapsedTime / 1000; // converte para segundos

  float cycleTime = timev + timea + timever; // tempo total do ciclo
  float lightIndex = currentSecond % cycleTime; // calcula o índice da luz acesa

  float distanciax = W_WIDTH/2 - 120;
  float distanciay = W_HEIGHT/2 + 300;
  if (cor_inicio == GREEN) {
    if (lightIndex < this.timever) {
      // vermelho
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
      this.activeLight = 0;
      carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
      carros[0].wrapAroundScreenx();
      carros[3].x -= velocidade_atual;
      carros[3].wrapAroundScreenx();
    } else if (lightIndex < this.timever + this.timea) {
      // amarelo
      this.alphav = 150;
      this.alphaa = 500;
      this.alphaver = 150;
      this.activeLight = 1;
  if (abs(carros[0].x - distanciax) > 10) {
    carros[0].x += velocidade_atual;
    carros[0].wrapAroundScreenx();
    carros[3].x -= velocidade_atual;
    carros[3].wrapAroundScreenx();
  }
    } else {
      // verde
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
      this.activeLight = 2;
      if(abs(carros[0].x - distanciax) < 10)
      {
      carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
      carros[0].wrapAroundScreenx();
      carros[3].x += velocidade_atual;
      carros[3].wrapAroundScreenx();
      }
    }
  } else if (cor_inicio == RED) {
    if (lightIndex < this.timev) {
      // vermelho
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
      this.activeLight = 0;
      if(abs(carros[0].x - distanciax) < 10)
      {
      carros[1].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
      carros[1].wrapAroundScreenx();
      carros[2].x += velocidade_atual;
      carros[2].wrapAroundScreenx();
      }
    } else if (lightIndex < this.timev + this.timea) {
      // amarelo
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
      this.activeLight = 1;
      carros[1].y -= velocidade_atual;
      carros[1].wrapAroundScreenx();
      carros[2].y += velocidade_atual;
      carros[2].wrapAroundScreenx();
    } else {
      // verde
      this.alphav = 150;
      this.alphaa = 500;
      this.alphaver = 150;
      this.activeLight = 2;
  if (abs(carros[1].y - distanciay) > 10) {
    carros[1].y -= velocidade_atual;
    carros[1].wrapAroundScreenx();
    carros[2].y += velocidade_atual;
    carros[2].wrapAroundScreenx();
  }
    }
  }
}
 
  void draw()
  {
    pushMatrix();
    translate(positionx,positiony);
    rotate(radians(angleS));
    fill(BLACK);
    rect(0, 0, 150, 50);
    fill(RED,alphav);
    circle(120, 25, 40);
    fill(YELLOW,alphaa);
    circle(75, 25, 40);
    fill(GREEN,alphaver);
    circle(30, 25, 40);
    fill(BLACK);
    rect(-40, 14, 40, 20);
    popMatrix();
  }
}

"Normal program" After a few seconds

0 Answers0