Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //these are just the rendering controls, using the tickbox, turn them on and off how you want
- muscles = false;
- skeleton = false;
- outline = true;
- //this turns wave muscles on or off
- wave = false;
- //verlet integration 2d
- //how many points you have on the stage; points should have instances ball1, ball2, ball3 etc.
- point_no = 17;
- //this creates an array containing x, y, oldx, and oldy posiitions for each point
- points = new Array();
- for(i=1;i<point_no+1;i++){
- points[i] = {x:_root["ball"+i]._x, y:_root["ball"+i]._y, x2:_root["ball"+i]._x, y2:_root["ball"+i]._y};
- }
- //iterations is how many times calculations are repeated per frame, should be >4 for best results but depends on what kind of
- //object it is
- iterations = 4;
- //universal gravity
- gravity = 0.4
- //on enter frame call each function
- function onEnterFrame(){
- //clear drawing after frame
- _root.clear();
- // carry out main functions;
- verlet();
- render();
- constraints();
- ai();
- keys();
- }
- //the verlet function pretty much moves the points by the difference of the previous two points
- verlet = function(){
- //for each particle
- for(i=1;i<point_no+1;i++){
- //store initial point
- tempx = points[i].x
- tempy = points[i].y
- //verlet integration
- points[i].x += points[i].x - points[i].x2
- points[i].y += points[i].y - points[i].y2 + gravity
- //store previous point
- points[i].x2 = tempx;
- points[i].y2 = tempy;
- }
- }
- //this controls where each particle is allowed to go
- constraints = function(){
- //for each particle carry out distance and boundary functions
- for(it=0;it<iterations;it++){
- distance();
- boundaries();
- }
- }
- //this function connects each particle/point with either a solid line (constrain) or a spring (muscle);
- distance = function(){
- //torso
- muscle(1, 2, 68);
- constrain(2, 3, 60);
- //chest triangle
- constrain(4, 7, 65);
- constrain(4, 3, 95);
- constrain(7, 3, 95);
- //hip triangle
- constrain(10, 13, 65);
- constrain(10, 2, 95);
- constrain(13, 2, 95);
- //waist muscles;
- muscle(4, 10, 100);
- muscle(7, 13, 100);
- //neck muscles;
- muscle(4, 1, 70);
- muscle(7, 1, 70);
- muscle(3, 1, 128);
- muscle(13, 1, 200);
- muscle(10, 1, 200);
- //left arm;
- constrain(4, 5, 60);
- constrain(5, 6, 50);
- muscle(1, 5, 130);
- muscle(4, 6, 110);
- //right arm;
- constrain(7, 8, 60);
- constrain(8, 9, 50);
- muscle(1, 8, 130);
- muscle(7, 9, 110);
- //left leg;
- constrain(10, 11, 85);
- constrain(11, 12, 80);
- muscle(10, 12, 165);
- muscle(3, 10, 40);
- //right leg;
- constrain(13, 14, 85);
- constrain(14, 15, 80);
- muscle(13, 15, 165);
- muscle(3, 13, 40);
- //leg width;
- muscle(12, 15, 90);
- muscle(11, 14, 100);
- muscle(2, 15, 280);
- muscle(2, 12, 280);
- //feet;
- constrain(12, 16, 30);
- constrain(15, 17, 30);
- muscle(16, 11, 100);
- muscle(17, 14, 100);
- muscle(17, 16, 170);
- //arms up;, the wave function only occurs on clicks
- if(wave == true){
- muscle(9, 13, 220);
- muscle(6, 10, 220);
- muscle(15, 9, 350);
- muscle(12, 6, 350);
- }
- }
- //edge of the world and friction
- boundaries = function(){
- for(i=1;i<point_no+1;i++){
- if(points[i].x > 540) (points[i].x = 540)
- if(points[i].x < 0) (points[i].x = 0)
- if(points[i].y > 400){
- //friction depends on the normal force ie penetration depth...
- depth = points[i].y-400
- points[i].x -= depth*(points[i].x-points[i].x2)/10
- points[i].y = 400;
- }
- //move the balls to the positions
- _root["ball"+i]._x = points[i].x
- _root["ball"+i]._y = points[i].y
- }
- //position head movie clip (you'll need one of these on the stage);
- head._x = ball1._x
- head._y = ball1._y
- }
- //the constrain function
- constrain = function(point, target, distance){
- r = distance;
- dx = points[point].x-points[target].x
- dy = points[point].y-points[target].y
- d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
- d2 = 0.5*(d1-r)/d1
- dx = dx*d2;
- dy = dy*d2;
- points[target].x += dx; points[target].y += dy;
- points[point].x -= dx; points[point].y -= dy
- if(skeleton==true){
- _root.lineStyle(2, 0xFF0000);
- _root.moveTo(points[target].x, points[target].y);
- _root.lineTo(points[point].x, points[point].y);
- }
- }
- //the muscle function;
- muscle = function(point, target, distance){
- r = distance;
- dx = points[point].x-points[target].x
- dy = points[point].y-points[target].y
- d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
- d2 = 0.05*(d1-r)/d1
- dx = dx*d2;
- dy = dy*d2;
- points[target].x += dx; points[target].y += dy;
- points[point].x -= dx; points[point].y -= dy;
- if(muscles==true){
- _root.lineStyle(2, 0x0000FF);
- _root.moveTo(points[target].x, points[target].y);
- _root.lineTo(points[point].x, points[point].y);
- }
- }
- //control with the keys
- keys = function(){
- for(i=1;i<point_no+1;i++){
- if(Key.isDown(Key.RIGHT)) points[i].x += gravity*2
- if(Key.isDown(Key.LEFT)) points[i].x -= gravity*2
- if(Key.isDown(Key.UP)) points[i].y -= gravity*2
- if(Key.isDown(Key.DOWN)) points[i].y += gravity*2
- }
- }
- //render it
- render = function(){
- if(outline==true){
- _root.lineStyle(10, 0x000000, 100);
- _root.moveTo(ball1._x, ball1._y);
- _root.lineTo((ball4._x+ball7._x)/2, (ball4._y+ball7._y)/2);
- _root.moveTo(ball4._x, ball4._y);
- _root.beginFill(0x000000, 100);
- _root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, (ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
- _root.curveTo((ball10._x+ball13._x)/2, (ball10._y+ball13._y)/2, (ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
- _root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, ball7._x, ball7._y);
- _root.lineTo(ball4._x, ball4._y);
- _root.endFill();
- _root.lineTo(ball5._x, ball5._y);
- _root.lineTo(ball6._x, ball6._y);
- _root.moveTo(ball7._x, ball7._y);
- _root.lineTo(ball8._x, ball8._y);
- _root.lineTo(ball9._x, ball9._y);
- _root.moveTo((ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
- _root.lineTo(ball11._x, ball11._y);
- _root.lineTo(ball12._x, ball12._y);
- _root.lineTo(ball16._x, ball16._y);
- _root.moveTo((ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
- _root.lineTo(ball14._x, ball14._y);
- _root.lineTo(ball15._x, ball15._y);
- _root.lineTo(ball17._x, ball17._y);
- }
- }
- //control the wave function;
- _root.onMouseDown = function(){
- wave = true;
- }
- _root.onMouseUp = function(){
- wave = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement