home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / GrafSys 2.0 / Demos / RobotArm ƒ / robotarm.p next >
Encoding:
Text File  |  1993-06-29  |  3.6 KB  |  175 lines  |  [TEXT/PJMM]

  1. program RobotArm;
  2.  
  3. {demonstrates inheritance of transformation on a 2D robot hand }
  4.  
  5.     uses
  6.         Matrix, Transformations, GrafSysCore, GrafSysScreen, GrafSysObject, Resources;
  7.  
  8.     const
  9.         degrees = 0.01745329; (* π/180 *)
  10.         cTheWindow = 400;
  11.  
  12.  
  13.     type
  14.         robot = object
  15.                 Arm: TSObject3D;
  16.                 Hand: TSObject3D;
  17.                 joint: TMatrixList; (* used to translate hand to end of arm *)
  18.                 jointRect: Rect;
  19.                 jointRadius: integer;
  20.                 procedure Init;
  21.                 procedure draw;
  22.             end;
  23.  
  24.     var
  25.         theRobot: robot;
  26.         EyeLoc: Vector4;
  27.         theWindow: WindowPtr;
  28.  
  29.  
  30.     procedure BuildArm (Obj: TSObject3D);
  31.  
  32.         var
  33.             OK: longint;
  34.  
  35.     begin
  36.         OK := Obj.AddPoint(0, 15, 0);
  37.         OK := Obj.AddPoint(70, 15, 0);
  38.         OK := Obj.AddPoint(70, -15, 0);
  39.         OK := Obj.AddPoint(0, -15, 0);
  40.         OK := Obj.AddLine(1, 2);
  41.         OK := Obj.AddLine(2, 3);
  42.         OK := Obj.AddLine(3, 4);
  43.         OK := Obj.AddLine(4, 1);
  44.  
  45.         OK := Obj.AddPoint(85, 0, 0); (* joint is circle with this center  *)
  46.         OK := Obj.AddPoint(70, 0, 0); (* joint radius calculated with this *)
  47.  
  48.     end;
  49.  
  50.     procedure BuildHand (Obj: TSObject3D);
  51.         var
  52.             OK: longint;
  53.  
  54.     begin
  55.         OK := Obj.AddPoint(15, 15, 0);
  56.         OK := Obj.AddPoint(45, 15, 0);
  57.         OK := Obj.AddPoint(35, 5, 0);
  58.         OK := Obj.AddPoint(25, 5, 0);
  59.         OK := Obj.AddPoint(25, -5, 0);
  60.         OK := Obj.AddPoint(35, -5, 0);
  61.         OK := Obj.AddPoint(45, -15, 0);
  62.         OK := Obj.AddPoint(15, -15, 0);
  63.         OK := Obj.AddLine(1, 2);
  64.         OK := Obj.AddLine(2, 3);
  65.         OK := Obj.AddLine(3, 4);
  66.         OK := Obj.AddLine(4, 5);
  67.         OK := Obj.AddLine(5, 6);
  68.         OK := Obj.AddLine(6, 7);
  69.         OK := Obj.AddLine(7, 8);
  70.         OK := Obj.AddLine(8, 1);
  71.     end;
  72.  
  73.     procedure robot.Init;
  74.         var
  75.             tmp: TMatrixPass;
  76.             h1, v1, h, v: integer;
  77.  
  78.     begin
  79.         New(Arm);
  80.         Arm.Init;
  81.         BuildArm(Arm);
  82.         Arm.SetUseBounds(TRUE);
  83.         New(Hand);
  84.         Hand.Init;
  85.         BuildHand(Hand);
  86.         Hand.SetUseBounds(TRUE);
  87.         joint := Hand.FFNewPostConcat;
  88.         tmp := Arm.FFPassOn;
  89.         Hand.FFInherit(tmp);
  90.         joint.TMTranslate(85, 0, 0);
  91.         ProjectPoint(Arm.transformedPoint(5), h, v);
  92.         ProjectPoint(Arm.transformedPoint(6), h1, v1);
  93.         jointRadius := abs(h1 - h);
  94.     end;
  95.  
  96.     procedure robot.draw;
  97.  
  98.         var
  99.             thePoint: Vector4;
  100.             dummy, z: real;
  101.  
  102.     begin
  103. (* erase old image *)
  104.         EraseRect(Arm.Bounds);
  105.         EraseRect(Hand.Bounds);
  106.         EraseRect(jointRect);
  107.         Arm.fDraw;
  108.         Hand.fDraw; (* now draw the joint *)
  109.         thePoint := Arm.transformedPoint(5);
  110.         GetVector4(thePoint, dummy, dummy, z);
  111.         if z >= 0 then
  112.             begin
  113.                 ProjectPoint(thePoint, jointRect.right, jointRect.bottom);
  114.                 jointRect.left := jointRect.right;
  115.                 jointRect.top := jointRect.bottom;
  116.                 InsetRect(jointRect, -jointRadius, -jointRadius);
  117.                 FrameOval(jointRect);
  118.             end;
  119.     end;
  120.  
  121.     const
  122.         leftArrow = $7B;
  123.         rightArrow = $7C;
  124.         upArrow = $7E;
  125.         downArrow = $7D;
  126.  
  127.     var
  128.         dummyLong: longint;
  129.         dummyBool: Boolean;
  130.         theEvent: EventRecord;
  131.         theKeys: KeyMap;
  132.         update: Boolean;
  133.  
  134. begin
  135.     InitGrafSys;
  136.     theWindow := GetNew3DWindow(cTheWindow, pointer(-1));
  137.     SetVector4(EyeLoc, 0, 0, -1);
  138.     SetEye(TRUE, EyeLoc, 0, 0, 0, 90 * degrees, arithmetic);
  139.     New(theRobot);
  140.     theRobot.Init;
  141.     theRobot.Draw;
  142.     repeat
  143.         GetKeys(theKeys);
  144.         if theKeys[leftArrow] then (* rotate hand counter-clockwise *)
  145.             begin
  146.                 theRobot.Hand.Rotate(0, 0, 2.5 * degrees);
  147.                 update := TRUE;
  148.             end;
  149.  
  150.         if theKeys[rightArrow] then (* rotate hand clockwise *)
  151.             begin
  152.                 theRobot.Hand.Rotate(0, 0, -2.5 * degrees);
  153.                 update := true;
  154.             end;
  155.  
  156.         if theKeys[upArrow] then (* rotate arm counter-clockwise *)
  157.             begin
  158.                 theRobot.Arm.Rotate(0, 1 * degrees, 2.5 * degrees);
  159.                 update := true;
  160.             end;
  161.  
  162.         if theKeys[downArrow] then
  163.             begin
  164.                 theRobot.Arm.Rotate(0, 0, -2.5 * degrees);
  165.                 update := true;
  166.             end;
  167.  
  168.         if update then
  169.             theRobot.Draw;
  170.         update := False;
  171.  
  172.         dummyBool := GetNextEvent(everyevent, theEvent);
  173.         SystemTask;
  174.     until button;
  175. end.