Gravity and Inverse Kinematics - from a book

Alright, so i got a book called Foundation Actionscript 3 Animation: Making Things Move, and i was reading the inverse kinematics section. i got all the code down, and i was playing with the code trying to add gravity to it. i tried attaching a ball to the last peice of tha chain/rope but that didn’t work. for that to work it have to stay in a circular area with a set diameter…

anyway, this is the code (it’s free to get off the author’s website):

Segment.as


package
{
 import flash.display.Sprite;
 import flash.geom.Point;
 public class Segment extends Sprite
 {
  private var color:uint;
  private var segmentWidth:Number;
  private var segmentHeight:Number;
  
  public var vx:Number = 0;
  public var vy:Number = 0;
  
  public function Segment(segmentWidth:Number, segmentHeight:Number, color:uint = 0xffffff)
  {
   this.segmentWidth = segmentWidth;
   this.segmentHeight = segmentHeight;
   this.color = color;
   init();
  }
  
  public function init():void
  {
   // draw the segment itself
   graphics.lineStyle(0);
   graphics.beginFill(color);
   graphics.drawRoundRect(-segmentHeight / 2, 
           -segmentHeight / 2,
           segmentWidth + segmentHeight,
           segmentHeight,
           segmentHeight,
           segmentHeight);
   graphics.endFill();
   
   // draw the two "pins"
   graphics.drawCircle(0, 0, 2);
   graphics.drawCircle(segmentWidth, 0, 2);
  }
  
  public function getPin():Point
  {
   var angle:Number = rotation * Math.PI / 180;
   var xPos:Number = x + Math.cos(angle) * segmentWidth;
   var yPos:Number = y + Math.sin(angle) * segmentWidth;
   return new Point(xPos, yPos);
  }
 }
}

MultiSegmentReach.as


package {
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.geom.Point;
 public class MultiSegmentReach extends Sprite
 {
  private var segments:Array;
  private var numSegments:uint = 6;
  
  public function MultiSegmentReach()
  {
   init();
  }
  
  private function init():void
  {
   stage.align = StageAlign.TOP_LEFT;
   stage.scaleMode = StageScaleMode.NO_SCALE;
   segments = new Array();
   for(var i:uint = 0; i < numSegments; i++)
   {
    var segment:Segment = new Segment(50, 10);
    addChild(segment);
    segments.push(segment);
   }
   segment.x = stage.stageWidth / 2;
   segment.y = stage.stageHeight / 2;
   addEventListener(Event.ENTER_FRAME, onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   var target:Point = reach(segments[0], mouseX, mouseY);
   for(var i:uint = 1; i < numSegments; i++)
   {
    var segment:Segment = segments*;
    target = reach(segment, target.x, target.y);
   }
   for(i = numSegments - 1; i > 0; i--)
   {
    var segmentA:Segment = segments*;
    var segmentB:Segment = segments[i - 1];
    position(segmentB, segmentA);
   }
  }
  
  private function reach(segment:Segment, xpos:Number, ypos:Number):Point
  {
   var dx:Number = xpos - segment.x;
   var dy:Number = ypos - segment.y;
   var angle:Number = Math.atan2(dy, dx);
   segment.rotation = angle * 180 / Math.PI;
   
   var w:Number = segment.getPin().x - segment.x;
   var h:Number = segment.getPin().y - segment.y;
   var tx:Number = xpos - w;
   var ty:Number = ypos - h;
   return new Point(tx, ty);
  }
  
  private function position(segmentA:Segment, segmentB:Segment):void
  {
   segmentA.x = segmentB.getPin().x;
   segmentA.y = segmentB.getPin().y;
  }
 }
}

i’m just not sure what to do to add some gravity in this.

ALSO, this will probably go into a game when the character can walk into this rope/chain, and it just moves and slides over them… i haven’t exactly got around to that part yet, but i though i might share…