Advertisement
mixster

mixster

Feb 4th, 2010
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.41 KB | None | 0 0
  1. program physicsengine;
  2. uses wincrt, winprocs;
  3.  
  4. type
  5.   TPointE = record
  6.     x, y: real;
  7.   end;
  8.  
  9.   TForce = record
  10.     resist: Boolean;
  11.     val: TPointE;
  12.   end;
  13.  
  14.   TObject = record
  15.     mass: real;
  16.     pos, curFor: TPointE;
  17.     forces: array [0..9] of ^TForce;
  18.   end;
  19.  
  20.   TVisual = record
  21.     obj: TObject;
  22.     ascii: array [0..2] of array [0..2] of char;
  23.   end;
  24.  
  25. var
  26.   screen: array [0..22] of array [0..74] of char;
  27.  
  28. procedure drawScreen;
  29. var
  30.   x, y: integer;
  31. begin
  32.   clrscr;
  33.  
  34.   for y := 0 to 22 do
  35.   begin
  36.     for x := 0 to 74 do
  37.       write (screen[y, x]);
  38.     writeln;
  39.   end;
  40. end;
  41.  
  42. procedure resetScreen;
  43. var
  44.   x, y: integer;
  45. begin
  46.   for y := 0 to 22 do
  47.     for x := 0 to 74 do
  48.       screen[y, x] := ' ';
  49. end;
  50.  
  51. procedure drawVisualObject(vis: TVisual);
  52. var
  53.   sx, sy, x, y, tx, ty: integer;
  54. begin
  55.   sx := round (vis.obj.pos.x);
  56.   sy := round (vis.obj.pos.y);
  57.  
  58.   for y := sy to sy + 2 do
  59.     for x := sx to sx + 2 do
  60.     begin
  61.       tx := x - sx;
  62.       ty := y - sy;
  63.       if ((x >= 0) and (x <= 74)) then
  64.         if ((y >= 0) and (y <= 22)) then
  65.           screen[y, x] := vis.ascii[ty, tx];
  66.     end;
  67. end;
  68.  
  69. procedure parseDisplay(var vis: TVisual; str1, str2, str3: string);
  70. var
  71.   x: integer;
  72. begin
  73.   for x := 0 to 2 do
  74.   begin
  75.     vis.ascii[0, x] := str1[x + 1];
  76.     vis.ascii[1, x] := str2[x + 1];
  77.     vis.ascii[2, x] := str3[x + 1];
  78.   end;
  79. end;
  80.  
  81. procedure handleCollision(var obj: TObject);
  82. begin
  83.   if ((obj.pos.x < 0) or (obj.pos.x > 72)) then
  84.   begin
  85.     obj.curFor.x := -obj.curFor.x * 1;
  86.  
  87.     if (obj.pos.x < 0) then
  88.       obj.pos.x := 0
  89.     else
  90.       obj.pos.x := 72;
  91.  
  92.   end;
  93.  
  94.   if ((obj.pos.y < 0) or (obj.pos.y > 20)) then
  95.   begin
  96.     obj.curFor.y := -obj.curFor.y * 1;
  97.  
  98.     if (obj.pos.y < 0) then
  99.       obj.pos.y := 0
  100.     else
  101.       obj.pos.y := 20;
  102.  
  103.   end;
  104. end;
  105.  
  106. procedure handleForces(var obj: TObject);
  107. var
  108.   i: integer;
  109. begin
  110.   for i := 0 to 9 do
  111.   begin
  112.  
  113.     if (obj.forces[i] <> nil) then
  114.     begin
  115.  
  116.       if (obj.forces[i]^.resist) then
  117.       begin
  118.         obj.curFor.x := obj.curFor.x * obj.forces[i]^.val.x;
  119.         obj.curFor.y := obj.curFor.y * obj.forces[i]^.val.y;
  120.       end
  121.       else
  122.       begin
  123.         obj.curFor.x := obj.curFor.x + obj.forces[i]^.val.x;
  124.         obj.curFor.y := obj.curFor.y + obj.forces[i]^.val.y;
  125.       end;
  126.  
  127.     end;
  128.  
  129.   end;
  130.  
  131.   obj.pos.x := obj.pos.x + obj.curFor.x;
  132.   obj.pos.y := obj.pos.y + obj.curFor.y;
  133.  
  134.   handleCollision(obj);
  135. end;
  136.  
  137. procedure applyForce(var obj: TObject; force: TForce);
  138. begin
  139.   if (force.resist) then
  140.   begin
  141.     obj.curFor.x := obj.curFor.x * force.val.x;
  142.     obj.curFor.y := obj.curFor.y * force.val.y;
  143.   end
  144.   else
  145.   begin
  146.     obj.curFor.x := obj.curFor.x + force.val.x;
  147.     obj.curFor.y := obj.curFor.y + force.val.y;
  148.   end;
  149. end;
  150.  
  151. var
  152.   myobj: TVisual;
  153.   x, y: integer;
  154.   t: longint;
  155.   meh: TForce;
  156.   user: TForce;
  157.   drag: TForce;
  158.   wind: TForce;
  159.   key: char;
  160.  
  161.   run: boolean;
  162.  
  163. begin
  164.   parseDisplay(myobj, '\|/', '-O-', '/|\');
  165.  
  166.   meh.resist := false;
  167.   meh.val.x := 0;
  168.   meh.val.y := 0.5;
  169.  
  170.   user.resist := false;
  171.   user.val.x := 0;
  172.   user.val.y := 0;
  173.  
  174.   drag.resist := true;
  175.   drag.val.x := 0.9;
  176.   drag.val.y := 0.9;
  177.  
  178.   wind.resist := false;
  179.   wind.val.x := 0;
  180.   wind.val.y := 0;
  181.  
  182.   myobj.obj.forces[0] := @meh;
  183.   myobj.obj.forces[1] := @wind;
  184.  
  185.   myobj.obj.forces[9] := @drag;
  186.   run := true;
  187.  
  188.   repeat
  189.       t := getTickCount + 50;
  190.  
  191.       resetScreen;
  192.       handleForces(myobj.obj);
  193.       drawVisualObject(myobj);
  194.       drawScreen;
  195.  
  196.       writeln ('X: ', myobj.obj.curFor.x:2:2, '; Y: ', myobj.obj.curFor.y:2:2);
  197.  
  198.       key := ' ';
  199.       while (t >= getTickCount) do
  200.         if (keyPressed) then
  201.           key := readKey;
  202.  
  203.       if (key <> ' ') then
  204.       begin
  205.         case key of
  206.           'w': user.val.y := user.val.y - 5;
  207.           'a': user.val.x := user.val.x - 5;
  208.           's': user.val.y := user.val.y + 5;
  209.           'd': user.val.x := user.val.x + 5;
  210.           't': wind.val.y := wind.val.y - 0.25;
  211.           'f': wind.val.x := wind.val.x - 0.25;
  212.           'g': wind.val.y := wind.val.y + 0.25;
  213.           'h': wind.val.x := wind.val.x + 0.25;
  214.           'q': run := false;
  215.         end;
  216.  
  217.         if (key in ['w', 'a', 's', 'd']) then
  218.         begin
  219.           applyForce(myobj.obj, user);
  220.           user.val.y := 0;
  221.           user.val.x := 0;
  222.         end;
  223.       end;
  224.   until not run;
  225.  
  226.   donewincrt;
  227. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement