So i drew a bicycle with 2 ellipse and a few lines in my void body(). I also added a code whereby the bike is reduced size on the left and big on the right. All my code line were written using constant. I used mouseX and mouseY to asign the bike’s location on the sketch and it follows it around. So far it works just fine. But now i want to add a mouseClicked function but i have no clue how. I want the bike to move where i click it on the canvas and stay there until i click somewhere else where it will slowly move towards the new point. I also wanted to keep the code about the size where its small on left, big on right. Can anyone help me? please.
Here is some example code demonstrating the variables and functions required to achieve that effect easily:
float current_x, current_y;
float target_x, target_y;
float lerp_amt = 0;
float d_lerp_amt = 0.02;
void setup() {
size(400, 400);
target_x = width / 2;
target_y = height/2;
current_x = target_x;
current_y = target_y;
rectMode(CENTER);
}
void draw() {
background(0);
if( lerp_amt > 0){
lerp_amt-=d_lerp_amt;
}
current_x = lerp(target_x, current_x, lerp_amt);
current_y = lerp(target_y, current_y, lerp_amt);
pushMatrix();
translate(current_x, current_y);
scale( map(current_x, 0, width, 3,0.1) );
rect(0, 0, 20, 20);
popMatrix();
}
void mousePressed() {
target_x = mouseX;
target_y = mouseY;
lerp_amt = 1;
}
1 Like
Thanks a lot man. It was really helpful.