Hi,
I have a sketch where a mover moves along a sinusoidal path. From that mover two vectors emerge.
Vector A rotating in the direction of the path. Vector B pointing at a position at the bottom center of the canvas.
No I try to get the vector addition of A+B but somehow the result is not what I expect.
This is how it currently looks:
The result I get is way to long and when you let the sketch run it seems to be out of place too. Shouldn’t the resulting Vector of A+B always be positioned between A and B?
Here is how I expect it should look if executed correctly:
I guess I screwed something with the order or the fact that some parts are rotating and some not…
I’d appreciate any help or feedback on this!
Here is my code:
let anglex = 0;
let angley = 0;
let y = 0;
let x = 0;
let mover;
function setup() {
createCanvas(600, 400);
// angleMode(DEGREES);
angley = 0;
anglex = 0;
angley1 = 0;
anglex1 = 0;
}
function draw() {
background(0);
mover = new Mover(x, y);
fill(252, 238, 33);
stroke(252, 238, 33);
beginShape();
y = map(sin(angley), -1, 1, 0, 200);
x = map(sin(anglex), -1, 1, 0, 600);
strokeWeight(4);
angley += 0.1;
anglex += 0.01;
endShape();
mover.update();
mover.display();
}
class Mover {
constructor(x, y) {
this.position = createVector();
this.velocity = createVector(x, y);
this.r = 20;
this.speed = 50;
}
update() {
this.position.add(this.velocity);
}
display() {
//Fixed Listener Position
let listener = createVector(300, 400);
fill(0, 255, 255);
noStroke();
ellipse(listener.x, listener.y, 20);
//Mover
stroke(50, 200, 50);
noFill();
ellipse(this.position.x, this.position.y, this.r);
push();
translate(this.position.x, this.position.y);
//Vector Mover to Listener (Air Speed)
let airspeed = p5.Vector.sub(listener, this.position);
airspeed.setMag(100);
stroke(0, 255, 255);
line(0, 0, airspeed.x, airspeed.y);
//Rotating Speed Vector
let direction = map(sin(angley), -1, 1, PI, PI / 2);
rotate(direction);
let speedmag = createVector(50, 50);
speedmag.setMag(100);
let speedvector = p5.Vector.sub(this.position, speedmag);
stroke(255, 0, 255);
line(0, 0, speedmag.x, speedmag.y);
//??Final Speed Vector (How to get a correct Vector addition here?)
stroke(0, 255, 0);
strokeWeight(1);
let finalspeed = p5.Vector.add(speedvector, airspeed);
line(0, 0, finalspeed.x, finalspeed.y);
pop();
}
}