We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I got this code with oscilating ellipse in with sinewave. I want to every time Im pressing the a key the ellipse shall oscillate in another direction but I cannot get it to work. (Of course in the example below it goes only in one direction since the loc.x and loc.y are the same)
I tried a bunch of things but Im running out of ideas.
Is there any way of changing the direction or do I have to use a different technique for example by using an attractor?
Particle p;
boolean restart = false;
void settings() {
size(1280, 720);
}
void setup() {
p = new Particle(0, 0);
}
void draw() {
background(255);
p.update();
p.display();
}
class Particle {
PVector loc;
int amplitude = 200;
float inc = 0;
Particle(int x, int y) {
loc = new PVector(x, y);
}
void update() {
float posX = sin(inc);
//float posY = cos(inc+10);
loc.x = amplitude * posX;
loc.y = amplitude * posX;
if (restart) {
if (posX < 0.05 && posX > -0.05) {
amplitude = 5;
inc *= 0;
}
} else {
posX = 0;
amplitude = 200;
restart = true;
}
inc += 0.04;
}
void display() {
translate(width/2, height/2);
stroke(0);
strokeWeight(2);
fill(127);
ellipse(loc.x, loc.y, 20, 20);
}
}
void keyReleased() {
restart =! restart;
}
Answers
Using lerp() below.
Kf
Using sin() below.
Kf