Vectors give me same data

Hello everyone, i’m new here
I want to make a slingshot that throws projectiles but i’m confronted to a problem with vector.
The objectiv is to have a vector that stores the datas of the throw. “v3”

Can you tell me why my vectors are the same after the first throw ?

My code with vectors
PVector v1, v2;

void setup () {
  v1 = new PVector();
  v2 = new PVector();
  size(400, 400);
}

void draw() {
  background(255);

  if (mousePressed) line(v1.x, v1.y, mouseX, mouseY);
}

void mousePressed() {
  v1.set(mouseX, mouseY);
}


void mouseReleased() {
  v2.set(mouseX, mouseY);

  print("v1  ", v1.x, ",", v1.y);
  print("    v2  ", v2.x, ",", v2.y);
  v1 = v2.sub(v1);
  println("    v3  ", v1.x, ",", v1.y);
}
The first three throws of the slingshot

v1 132.0 , 185.0 v2 195.0 , 233.0 v3 63.0 , 48.0
v1 159.0 , 225.0 v2 159.0 , 225.0 v3 0.0 , 0.0
v1 245.0 , 205.0 v2 245.0 , 205.0 v3 0.0 , 0.0

PVector.sub(v2, v1, v1);

I don’t understand what you wrote but after rereading the reference I noticed my error.

The corrected form is v2.sub(v1); whitout the assignation

Thanks for the help :wink:

But then you’d have to println() v2 instead of v1:
println(" v3 ", v1.x, ",", v1.y); —> println(" v3 ", v2.x, ",", v2.y);

PVector.sub(v2, v1, v1); means subtract v2 - v1 then store the result in v1.

Yes I figured it out

Ho ok i needed more details to understand. thank you

2 Likes