Based on the basis of last recitation, I improved my project by using functions like “mouseX/Y”, “atan2”,polar equation of oval and “lerp”
my code for moving eye ball( the other parts are written in the recitation5):
int x0 = width/2;
int y0 = 267;
float angle = atan2(mouseY-y0, mouseX-x0);
float ballX = x0+50*cos(angle);
float ballY =y0+11*sin(angle);
tempX = lerp(tempX, ballX, 0.1);
tempY = lerp(tempY, ballY, 0.1);
ellipse(tempX, tempY, 50, 50);
- (x0,y0) is the center of the eye
- By using atan2, I can know the angle of the mouse and the center.
- I used the polar equation to restrain the eye ball into an oval, so that the eye ball will not leave the eye socket. It needs the angle of step2.
- function lerp is used to let the eye move more smoothly. If the mouse leave the picture and appear at another point, the eyeball will move smoothly to the new angle instead of move to it in a sudden.