본문 바로가기
데모

[processing] 볼팅기기

by hansoo.labs 한수댁 2020. 9. 25.

 

Processing Code

float GRAVITY = 0.02;
float ballX[] = new float[100]; 
float ballY[] = new float[100];
float speedX[] = new float[100]; 
float speedY[] = new float[100];
float prevX = 0;
float prevY = 0;

void setup() {
  size(400,600);  
  background(255);
  for (int i=0; i<100; i++) {
    ballX[i] = 200;     
    ballY[i] = 0;     
    speedX[i] = random(1);    
    speedY[i] = random(1);
  }
}
void draw() {
  background(255);
  for (int i=0; i<100; i++) {
    speedY[i] = speedY[i] + GRAVITY;
    ballY[i] = ballY[i] + speedY[i];
    ballX[i] = ballX[i] + speedX[i];

    if (ballY[i]>height) {
      speedY[i] = speedY[i] * -0.8;
      ballY[i] = height;
    }
    if (ballX[i]>width) {
      speedX[i] = speedX[i] * -0.9;
      ballX[i] = width;
    } 
    else if (ballX[i]<0) {
      speedX[i] = speedX[i] * -0.9;
      ballX[i] = 0;
    } 
    
    ellipse(mouseX,mouseY,30,30);
    
    if (abs(mouseX-ballX[i])<20 && abs(mouseY-ballY[i])<20) {
      
        int dx = mouseX - ballX[i];
        int dy = mouseY - ballY[i];
        float dab = abs(sqrt(dx*dx + dy*dy)) ;    
        float sinTheta = dy / dab;
        float cosTheta = dx / dab;
        float e = 1;
        float a_m = 100;
        float b_m = 50;
        float a_vx = mouseX - prevX;
        float a_vy = mouseY - prevY;
      
        float vxAp = (a_m - e*b_m)/(a_m + b_m)*(a_vx*cosTheta + a_vy*sinTheta) +
                    (b_m + e*b_m)/(a_m + b_m)*(speedX[i]*cosTheta + speedY[i]*sinTheta);
        float vxBp = (a_m + e*a_m)/(a_m + b_m)*(a_vx*cosTheta + a_vy*sinTheta) + 
                    (b_m - e*a_m)/(a_m + b_m)*(speedX[i]*cosTheta + speedY[i]*sinTheta);
        float vyAp = a_vx*(-sinTheta) + a_vy*cosTheta;
        float vyBp = speedX[i]*(-sinTheta) + speedY[i]*cosTheta;
        //a_vx = vxAp*cosTheta + vyAp*(-sinTheta);
        //a_vy = vxAp*sinTheta + vyAp*cosTheta;
        speedX[i] = vxBp*cosTheta + vyBp*(-sinTheta);
        speedY[i] = vxBp*sinTheta + vyBp*cosTheta;
        /////////겹치는 경우 조정
        float angleBA = atan2(-dy, -dx);
        float moveToDistance = abs(15 + 10) - dab;
        ballX[i] = ballX[i] + moveToDistance * Math.cos(angleBA);
        fill(255,0,0,255);
        ellipse(ballX[i],ballY[i],10,10);
    } 
    else {
        fill(255,255,255);
        ellipse(ballX[i],ballY[i],10,10); 
    }
  }
  
  prevX = mouseX;
  prevY = mouseY;
}

댓글0