핵심 함수
private function springTo(ballA:Ball, ballB:Ball){
var dx:Number = ballB.x - ballA.x; //거리계산
var dy:Number = ballB.y - ballA.y;
var angle:Number = Math.atan2(dy, dx);
//각도계산
//목표위치계산
var targetX:Number = ballB.x - Math.cos(angle)
* springLength;
var targetY:Number = ballB.y - Math.sin(angle)
* springLength;
ballA.vx += (targetX - ballA.x) * spring;
//속도 계산
ballA.vy += (targetY - ballA.y) * spring;
ballA.vy += gravity; //중력 추가.
ballA.vx *= friction; //마찰 추가
ballA.vy *= friction;
if(Math.abs(ballA.vx) < 0.05){ ballA.vx
= 0;}
//속도가 0.05이하일 경우 정지.
if(Math.abs(ballA.vy) < 0.05){ ballA.vy
= 0;}
ballA.x += ballA.vx; //위치변경
ballA.y += ballA.vy;
}
spring(탄성계수): 0.1, friction( 마찰계수): 0.95,
gravity(중력계수): 0.5, springLength(두 원사이 기본거리): 100
|