Mike
Mike

Reputation: 963

I don't know how the assignment operator works [processing]

I'm writing a program that simulates a snake, and the values of each section of the snake are stored in an array. In order to simulate the snake physics, I need to assign the subsequent section to the previous one, which is where I run into a problem.

int bar;
int foo;

void setup() {}

void draw() {
  foo = bar;
  bar = mouseX;
  println(foo);
  println(bar);
}

In this example, shouldn't foo output bar's previous mouse position? Bar is being assigned to mouseX after foo is being assigned to bar. Does the assignment operator make it so that if the variable that is assigned changes the variable being assigned to changes with it? This is very frustrating and I think the solution should be simple. How do I assign one variable to another disregarding future changes to the var being referenced?

EDIT: Here is an example of the snake simulator that should work but doesn't for the same reason, all the segments end up being the same at the end of each for loop:

int segments = int(random(3, 10));
float springing[] = new float[segments];
float damping[] = new float[segments];
PVector accel[] = new PVector[segments];
PVector[] joints = new PVector[segments];
PVector[] delta = new PVector[segments];
PVector food = new PVector(0,0,0);

void setup() {
  size(500, 500);
  stroke(255);
  for(int n = 0; n < joints.length; n++) {
    if (n == 0) joints[0] = new PVector(random(width), random(height));
    else joints[n] = joints[0];
    delta[n] = new PVector(0,0,0);
    accel[n] = new PVector(0,0,0);
    springing[n] = .05*(.07*(n+1)); 
    damping[n] = .95-(.02*n);
  }
}

void draw() {
  background(0);
  food.x =  mouseX;
  food.y = mouseY;
  for(int n = 0; n < segments; n++) {
    if (n == 0) {
     delta[0] = PVector.sub(food, joints[0]); 
     joints[0].add(delta[0]);
    }
    else {
     delta[n] = PVector.sub(joints[n-1], joints[n]);
     delta[n].mult(springing[n]);
     accel[n].add(delta[n]);
     joints[n].add(accel[n]);
    }
    point(joints[n].x, joints[n].y);
    accel[n].mult(damping[n]);
  }
}

Upvotes: 1

Views: 570

Answers (2)

Robert Stewart
Robert Stewart

Reputation: 3330

The draw() loop gets run many times per second. As soon as it runs again, foo gets overwritten with the value of bar, which has the mouse x position from the previous execution of draw(). I think you can see it more clearly if you change the println statements to:

println("foo: " + foo + " bar: " + bar);

Then if you run your sketch while moving the mouse you will see entries like:

foo: 76 bar: 84
foo: 84 bar: 91
foo: 91 bar: 97
foo: 97 bar: 97

As expected, each time draw() executes, foo takes on the value that bar had (i.e., mouseX) during the last time draw() executed, because your draw() loop keeps setting foo to the current value of bar.

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

In your example, foo and bar are primitive types, so the results should match your expectations. If they were references, however, then what you have written would be incomplete. In such a case, a constructor would help you preserve the old value of the variable.

Have you tried inspecting the value of bar when you enter your draw method and seeing what it actually is before and after the assignments?

Upvotes: 2

Related Questions