I’m trying to figure out how to constrain one end of a sine wave so that the left end of the wave animation is “tied” to the mouse x and mouse y. I’m thinking i need PVectors, but not sure how to implement that. I’ve working off of Shiffman’s sine wave example. I have it “kinda” working with translate, but not really what i want (I want to be able to move the wave around as if it’s attached to the mouse)
int xspacing = 2; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float theta = 0.0; // Start angle at 0
float amplitude;
float x;
float period = 900.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave
float r;//random values
float mx, my;
void setup(){
size(800,600,P3D);
frameRate(30);
background(255);
noCursor();
w = width;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
}
void draw(){
fill(255,100);
rect(0,0,width,height);
mx=mouseX;
my=mouseY;
strokeWeight(1);
stroke(10);
fill(255);
ellipse(mx,my,10,10);
calcWave();
renderWave();
}
void calcWave() {
theta +=.05;
amplitude= random(70,72);
// For every x value, calculate a y value with sine function
x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}
void renderWave() {
noStroke();
fill(10);
r=random(height);
pushMatrix();
translate(mouseX+10,mouseY-200);
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
ellipse(x*xspacing, height/2+yvalues[x], 2, 2);
}
popMatrix();
}