float GRAVITY = 0.98; float resistance = 0.01; BALL ball = new BALL(); boolean isChk = false; float lineLength = 100; void setup() { size(400,300); } void draw() { background(255); if(isChk && mousePressed){ float disX = mouseX - ball.x; float disY = mouseY - ball.y; float seta = atan2(disY, disX); float Len = sqrt(disX*disX + disY*disY); float F = Len - lineLength; float Fx = cos(seta) * F; float Fy = sin(seta) * F; if(F > 0){ ball.spX += Fx; ball.spY += Fy; ball.x += Fx; ball.y += Fy; } float late = sin((1-(Len / lineLength)) * PI / 2); noFill(); stroke(255,102,0); // curve(ball.x,ball.y,(ball.x + mouseX)/2,(ball.y + mouseY)/2 - F*late, (ball.x + mouseX)/2,(ball.y + mouseY)/2 , mouseX,mouseY); beginShape(); vertex(ball.x,ball.y); bezierVertex(ball.x,ball.y, (ball.x + mouseX)/2,(ball.y + mouseY)/2 - F*late, mouseX,mouseY); endShape(); } ball.update(); } void mousePressed(){ if((mouseX > ball.x - ball.r/2) && (mouseX < ball.x + ball.r/2) && (mouseY > ball.y - ball.r/2) && (mouseY < ball.y + ball.r/2)){ isChk = true; } } void mouseReleased(){ isChk = false; } public class BALL { //x, y property public float x, y, spX, spY; //elasticity float e = 1.0; //width int r = 50; BALL () { x = 200; y = 0; spX = 0; spY = 0; } public void update() { //add gravity spY += GRAVITY; //add resistance spX -= spX * resistance; spY -= spY * resistance; x += spX; y += spY; int over; if(x > width){ over = int(x - width); x -= over; spX *= -e; } if(x < 0){ over = int(x); x -= over; spX *= -e; } if(y > height - r/2){ over = int(y - height + r/2); y -= over; spY *= -e; } if(y < 0){ over = int(y); y -= over; spY *= -e; } noFill(); stroke(0,0,0); ellipse(x,y,r,r); } }