Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program physicsengine;
- uses wincrt, winprocs;
- type
- TPointE = record
- x, y: real;
- end;
- TForce = record
- resist: Boolean;
- val: TPointE;
- end;
- TObject = record
- mass: real;
- pos, curFor: TPointE;
- forces: array [0..9] of ^TForce;
- end;
- TVisual = record
- obj: TObject;
- ascii: array [0..2] of array [0..2] of char;
- end;
- var
- screen: array [0..22] of array [0..74] of char;
- procedure drawScreen;
- var
- x, y: integer;
- begin
- clrscr;
- for y := 0 to 22 do
- begin
- for x := 0 to 74 do
- write (screen[y, x]);
- writeln;
- end;
- end;
- procedure resetScreen;
- var
- x, y: integer;
- begin
- for y := 0 to 22 do
- for x := 0 to 74 do
- screen[y, x] := ' ';
- end;
- procedure drawVisualObject(vis: TVisual);
- var
- sx, sy, x, y, tx, ty: integer;
- begin
- sx := round (vis.obj.pos.x);
- sy := round (vis.obj.pos.y);
- for y := sy to sy + 2 do
- for x := sx to sx + 2 do
- begin
- tx := x - sx;
- ty := y - sy;
- if ((x >= 0) and (x <= 74)) then
- if ((y >= 0) and (y <= 22)) then
- screen[y, x] := vis.ascii[ty, tx];
- end;
- end;
- procedure parseDisplay(var vis: TVisual; str1, str2, str3: string);
- var
- x: integer;
- begin
- for x := 0 to 2 do
- begin
- vis.ascii[0, x] := str1[x + 1];
- vis.ascii[1, x] := str2[x + 1];
- vis.ascii[2, x] := str3[x + 1];
- end;
- end;
- procedure handleCollision(var obj: TObject);
- begin
- if ((obj.pos.x < 0) or (obj.pos.x > 72)) then
- begin
- obj.curFor.x := -obj.curFor.x * 1;
- if (obj.pos.x < 0) then
- obj.pos.x := 0
- else
- obj.pos.x := 72;
- end;
- if ((obj.pos.y < 0) or (obj.pos.y > 20)) then
- begin
- obj.curFor.y := -obj.curFor.y * 1;
- if (obj.pos.y < 0) then
- obj.pos.y := 0
- else
- obj.pos.y := 20;
- end;
- end;
- procedure handleForces(var obj: TObject);
- var
- i: integer;
- begin
- for i := 0 to 9 do
- begin
- if (obj.forces[i] <> nil) then
- begin
- if (obj.forces[i]^.resist) then
- begin
- obj.curFor.x := obj.curFor.x * obj.forces[i]^.val.x;
- obj.curFor.y := obj.curFor.y * obj.forces[i]^.val.y;
- end
- else
- begin
- obj.curFor.x := obj.curFor.x + obj.forces[i]^.val.x;
- obj.curFor.y := obj.curFor.y + obj.forces[i]^.val.y;
- end;
- end;
- end;
- obj.pos.x := obj.pos.x + obj.curFor.x;
- obj.pos.y := obj.pos.y + obj.curFor.y;
- handleCollision(obj);
- end;
- procedure applyForce(var obj: TObject; force: TForce);
- begin
- if (force.resist) then
- begin
- obj.curFor.x := obj.curFor.x * force.val.x;
- obj.curFor.y := obj.curFor.y * force.val.y;
- end
- else
- begin
- obj.curFor.x := obj.curFor.x + force.val.x;
- obj.curFor.y := obj.curFor.y + force.val.y;
- end;
- end;
- var
- myobj: TVisual;
- x, y: integer;
- t: longint;
- meh: TForce;
- user: TForce;
- drag: TForce;
- wind: TForce;
- key: char;
- run: boolean;
- begin
- parseDisplay(myobj, '\|/', '-O-', '/|\');
- meh.resist := false;
- meh.val.x := 0;
- meh.val.y := 0.5;
- user.resist := false;
- user.val.x := 0;
- user.val.y := 0;
- drag.resist := true;
- drag.val.x := 0.9;
- drag.val.y := 0.9;
- wind.resist := false;
- wind.val.x := 0;
- wind.val.y := 0;
- myobj.obj.forces[0] := @meh;
- myobj.obj.forces[1] := @wind;
- myobj.obj.forces[9] := @drag;
- run := true;
- repeat
- t := getTickCount + 50;
- resetScreen;
- handleForces(myobj.obj);
- drawVisualObject(myobj);
- drawScreen;
- writeln ('X: ', myobj.obj.curFor.x:2:2, '; Y: ', myobj.obj.curFor.y:2:2);
- key := ' ';
- while (t >= getTickCount) do
- if (keyPressed) then
- key := readKey;
- if (key <> ' ') then
- begin
- case key of
- 'w': user.val.y := user.val.y - 5;
- 'a': user.val.x := user.val.x - 5;
- 's': user.val.y := user.val.y + 5;
- 'd': user.val.x := user.val.x + 5;
- 't': wind.val.y := wind.val.y - 0.25;
- 'f': wind.val.x := wind.val.x - 0.25;
- 'g': wind.val.y := wind.val.y + 0.25;
- 'h': wind.val.x := wind.val.x + 0.25;
- 'q': run := false;
- end;
- if (key in ['w', 'a', 's', 'd']) then
- begin
- applyForce(myobj.obj, user);
- user.val.y := 0;
- user.val.x := 0;
- end;
- end;
- until not run;
- donewincrt;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement