Rotate shape towards mouse with easing
in
Programming Questions
•
2 years ago
Hi!
I just started learning Processing and probably I should give up on this until I learn more but after trying so many ways without success I would be really happy if someone told me the right way to do this:
make a shape rotate towards the mouse position with easing and not turning around when the angle goes from -PI to PI or viceversa. It's this last part that's really bugging me. Without easing this is invisible but once you see the inbetween positions of the rotation it doesn't work well at the -PI,PI border-crossing
Here's what I've got:
- int foco=20; // width of stream
- int largo=150; // max length of ray
- float angulo; // angle for easing
- void setup(){
- size(640,480);
- smooth();
- noCursor();
- frameRate(24);
- }
- void draw(){
- background(0);
- fill(255);
- stroke(200);
- point(mouseX,mouseY);
- emanator(100,100);
- }
- void emanator(int x,int y){
- ellipse(x,y,random(5,6),random(5,6));
- // easing of the rotation
- int speed=abs((mouseX-pmouseX)+(mouseY-pmouseY));
- float easing=(1-map(speed,0,50,0.05,0.99));
- easing=pow(easing,8);
- float targetAngl=degrees(atan2(mouseY-y,mouseX-x));
- float dangl=targetAngl-angulo;
- if (abs(dangl)>1){
- angulo+=dangl*easing;
- println(angulo);
- }
- for (int i=0; i<=2*foco; i+=3){
- pushMatrix();
- translate(x,y);
- rotate(radians(angulo+i-foco));
- strokeWeight(2);
- stroke(random(170,240));
- line(0,0,random(largo),0);
- popMatrix();
- }
- }
I think there must be a simple way to fix this but I can't find it... Please, help me!
1