вот набросок для классов физики

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
unit Physics;
interface
uses Variable, Math, Classes, Contnrs,
dialogs, sysutils;// это чтоб работало inttostr и showmessage . Just for testing...
function Normalize(Angle: Single): Single;
function Arctg(arcX1, arcY1, arcX2, arcY2: Single): Single;
procedure init;
type
TParticle = class(TObject) //а что делать? зато так удобнее!
private
x_, y_, dx_, dy_, vx_, vy_: Single;
public
property x: single read x_ write x_;
property y: single read y_ write y_;
property dx: single read dx_ write dx_;
property dy: single read dy_ write dy_;
property vx: single read vx_ write vx_;
property vy: single read vy_ write vy_;
constructor Create(_x, _y, _dx, _dy, _vx, _vy: single);
end;
TJoint = class(TObject)
private
a_, b_: integer;
Length_: Single;
public
property A: Integer read a_ write a_;
property B: Integer read b_ write b_;
property Length: Single read Length_ write Length_;
constructor Create(FirstNode, SecondNode: integer; len: single);
end;
TParticleList = class(TObjectList)
private
function GetItem(Index: Integer): TParticle;
public
function Add(AObject: TParticle): Integer;
property Items[Index: Integer]: TParticle read GetItem; default;
end;
TJointList = class(TObjectList)
private
function GetItem(Index: Integer): TJoint;
public
function Add(AObject: TJoint): Integer;
property Items[Index: Integer]: TJoint read GetItem; default;
end;
TWorld = class(TObject) //а вот это наше всё!
private
JointList_: TJointList;
ParticleList_: TParticleList;
public
property JointList: TJointList read JointList_ write JointList_;
property ParticleList: TParticleList read ParticleList_ write ParticleList_;
constructor Create;
end;
implementation
function Arctg(arcX1, arcY1, arcX2, arcY2: Single): Single;
begin
if arcX1 = arcX2 then
begin
if arcY1 >= arcY2 then Result := 0 else Result := 180;
end else
begin
Result := 90 + arctan((arcY1 - arcY2) / (arcX1 - arcX2)) * 180 / pi;
if arcX1 < arcX2 then Result := Result - 180;
end;
end;
function CrossedLine(xa1, ya1, xa2, ya2, xb1, yb1, xb2, yb2: Single; var xCros, yCros: Single): boolean;
var kCros1, bCros1, kCros2, bCros2: Single;
begin
Result := true;
if xb1 <> xb2 then kCros1 := (yb2 - yb1) / (xb2 - xb1) else kCros1 := yb2 - yb1 / 0.0001;
bCros1 := yb1 - kCros1 * xb1;
if not ((ya1 > kCros1 * xa1 + bCros1) xor (ya2 > kCros1 * xa2 + bCros1)) then Result := false;
if xa1 <> xa2 then kCros2 := (ya2 - ya1) / (xa2 - xa1) else kCros2 := ya2 - ya1 / 0.0001;
bCros2 := ya1 - kCros2 * xa1;
if not ((yb1 > kCros2 * xb1 + bCros2) xor (yb2 > kCros2 * xb2 + bCros2)) then Result := false;
if Result then
begin
xCros := (bCros2 - bCros1) / (kCros1 - kCros2);
yCros := kCros1 * xCros + bCros1;
end;
end;
function Normalize(angle: Single): Single;
begin
Result := angle;
while Result > 180 do Result := Result - 360;
while Result < -180 do Result := Result + 360;
end;
procedure init;
var JointList: TJointList;
begin
JointList := TJointList.Create;
JointList.Add(TJoint.Create(1, 2, 10));
JointList.Add(TJoint.Create(3, 4, 20));
showmessage(floattostr(jointlist.Items[0].length)); //ну это чтоб убедиццо, что все работает.. (:
end;
{ TParticleList }
function TParticleList.Add(AObject: TParticle): Integer;
begin
Result := inherited Add(AObject);
end;
function TParticleList.GetItem(Index: Integer): TParticle;
begin
Result := TParticle(inherited GetItem(Index));
end;
{ TJointList }
function TJointList.Add(AObject: TJoint): Integer;
begin
Result := inherited Add(AObject);
end;
function TJointList.GetItem(Index: Integer): TJoint;
begin
Result := TJoint(inherited GetItem(Index));
end;
{ TJoint }
constructor TJoint.Create(FirstNode, SecondNode: integer; len: single);
begin
inherited create; //хз, может инхеритед и не надо, без него все работает, ибо у предка Create пустой
A := FirstNode;
B := SecondNode;
Length := len;
end;
{ TParticle }
constructor TParticle.Create(_x, _y, _dx, _dy, _vx, _vy: single);
begin
inherited create; //см. коммент к TJoint
x := _x;
y := _y;
dx := _dx;
dy := _dy;
vx := _vx;
vy := _vy;
end;
{ TWorld }
constructor TWorld.Create; // это я пока еще не тестил, надо будет к нему collide приделать
begin
inherited create;
JointList_ := TJointList.Create;
ParticleList_ := TParticleList.Create;
end;
end.