Hey, I'm making a simple moving car at night with its headlight bulbs and I'm using PROCESSING software, not java. What I tried to make is when the car touches the right border, the headlight bulb of the right side of the car should be changed to the left side of the same car. It doesn't work with this code below. Any suggestions?

float moveX, moveY, speed = 3;
float dia;

void setup() {
  size(600, 400);
  background(0);
  moveX = width/2;
  moveY = height/2;
  dia = 50;
}

void draw() {
  background(0);
  rectMode(CENTER);
  noStroke();
  fill(255);
  rect(moveX, moveY, dia, 20);
  moveX += speed;
  if (moveX <= 25 || moveX >= width-25)
    speed *= -1;

  fill(255, 255, 0, 150);                               // BELOW THIS POINT, IT DOESN'T WORK!
  if (moveX >= 0 && moveX <= width) {
    triangle(moveX+80, height/2+10, moveX+80, height/2, moveX+20, height/2+5);
    triangle(moveX+80, height/2-10, moveX+80, height/2, moveX+20, height/2-5);
  }
  else if (moveX <= 0 && moveX >= width) {
    triangle(moveX-80, height/2+10, moveX-80, height/2, moveX-20, height/2+5);
    triangle(moveX-80, height/2-10, moveX-80, height/2, moveX-20, height/2-5);
  }
}

Dani AI

Generated

The bug is a logic/condition issue. is right: your second compound test can never be true, and your first headlight test (moveX >= 0 && moveX <= width) will almost always be true, so the left-headlight branch never runs. That makes the drawing code for the opposite side unreachable.

Two simple, robust fixes:

  • Base the headlight side on direction (recommended). When the car reverses, check the sign of speed and draw the cone on the side the car is moving toward. This avoids exact-position checks and floating-point equality issues.
  • Or base it on the car bounds using the car half-width (use dia/2) rather than magic numbers. Test moveX <= dia/2 and moveX >= width - dia/2 and clamp moveX after reversing to avoid overshoot.

A concise Processing example (direction-based approach):

float half = dia/2;
fill(255,255,0,150);
if (speed > 0) {               // moving right -> right headlight
  triangle(moveX+half, moveY-6, moveX+half+60, moveY, moveX+half, moveY+6);
  triangle(moveX+half, moveY-18, moveX+half+60, moveY-9, moveX+half, moveY-9);
} else {                       // moving left -> left headlight
  triangle(moveX-half, moveY-6, moveX-half-60, moveY, moveX-half, moveY+6);
  triangle(moveX-half, moveY-18, moveX-half-60, moveY-9, moveX-half, moveY-9);
}

Practical tips: compute offsets from dia (no hard-coded 80/20), clamp the X when you invert speed to prevent tunneling, and avoid == checks on floats. Using pushStyle()/popStyle() keeps fill/stroke changes local. These changes make the headlight switch predictable and keep the visuals aligned with the car body.

if (moveX <= 0 && moveX >= width) {

This is only going to test true if moveX is exactly width (and width is zero) or width is negative. I'm going to assume that width is never negative since widths usually aren't negative. You shouldn't ever depend on a floating point number to be exactly some value, because floating point arithmetic is not exact. You should be aware that it can happen sometimes and make sure your program behaves appropriately when it does, such as protecting against division by zero, but you should also be aware that it may never happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.