-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuCollisions.pas
133 lines (111 loc) · 4.47 KB
/
uCollisions.pas
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
//* Àâòîð [Xakep] *//
unit uCollisions;
interface
uses
GLScene, Classes;
function Collision(CollisionsList: TList; Obj1: TGLBaseSceneObject; Tag: Integer): Boolean; overload;
function Collision(Obj1: TGLBaseSceneObject; Obj2: TGLBaseSceneObject): Boolean; overload;
function Collision(CollisionsList: TList; Obj1: TGLBaseSceneObject; Tag: Integer; w1,h1,w2,h2: Single): Boolean; overload;
function Collision(Obj1: TGLBaseSceneObject; Obj2: TGLBaseSceneObject; w1,h1,w2,h2: Single): Boolean; overload;
implementation
function Collision(CollisionsList: TList; Obj1: TGLBaseSceneObject; Tag: Integer): Boolean;
Var
i: Integer;
P,Obj2: TGLBaseSceneObject;
t: array[1..4] of Integer;
begin
Result:= False;
for i:= 0 to CollisionsList.Count - 1 do
begin
P:= CollisionsList.Items[i];
if Assigned(P) then
if (P.Tag = Tag) and (P.Visible) then
begin
Obj2:= P;
if Obj1.Scale.X < 0 then t[1]:= -1 else t[1]:= 1;
if Obj1.Scale.Y < 0 then t[2]:= -1 else t[2]:= 1;
if Obj2.Scale.X < 0 then t[3]:= -1 else t[3]:= 1;
if Obj2.Scale.Y < 0 then t[4]:= -1 else t[4]:= 1;
if ((Obj1.Position.X + 0.1 + ((Obj1.Scale.X / 2) * t[1]) >= (Obj2.Position.X - ((Obj2.Scale.X / 2) * t[3]))) and
(Obj1.Position.X - 0.1 - ((Obj1.Scale.X / 2) * t[1]) <= (Obj2.Position.X + ((Obj2.Scale.X / 2) * t[3])))) and
((Obj1.Position.Y + 0.1 + ((Obj1.Scale.Y / 2) * t[2]) >= (Obj2.Position.Y - ((Obj2.Scale.Y / 2) * t[4]))) and
(Obj1.Position.Y - 0.1 - ((Obj1.Scale.Y / 2) * t[2]) <= (Obj2.Position.Y + ((Obj2.Scale.Y / 2) * t[4])))) then
begin
Obj1.TagObject:= Obj2;
Obj2.TagObject:= Obj1;
Result:= True;
Exit;
end;
end;
end;
end;
function Collision(Obj1: TGLBaseSceneObject; Obj2: TGLBaseSceneObject): Boolean;
Var
t: array[1..4] of Integer;
begin
Result:= False;
if Obj1.Scale.X < 0 then t[1]:= -1 else t[1]:= 1;
if Obj1.Scale.Y < 0 then t[2]:= -1 else t[2]:= 1;
if Obj2.Scale.X < 0 then t[3]:= -1 else t[3]:= 1;
if Obj2.Scale.Y < 0 then t[4]:= -1 else t[4]:= 1;
if ((Obj1.Position.X + 0.1 + ((Obj1.Scale.X / 2) * t[1]) >= (Obj2.Position.X - ((Obj2.Scale.X / 2) * t[3]))) and
(Obj1.Position.X - 0.1 - ((Obj1.Scale.X / 2) * t[1]) <= (Obj2.Position.X + ((Obj2.Scale.X / 2) * t[3])))) and
((Obj1.Position.Y + 0.1 + ((Obj1.Scale.Y / 2) * t[2]) >= (Obj2.Position.Y - ((Obj2.Scale.Y / 2) * t[4]))) and
(Obj1.Position.Y - 0.1 - ((Obj1.Scale.Y / 2) * t[2]) <= (Obj2.Position.Y + ((Obj2.Scale.Y / 2) * t[4])))) then
begin
Obj1.TagObject:= Obj2;
Obj2.TagObject:= Obj1;
Result:= True;
end;
end;
function Collision(CollisionsList: TList; Obj1: TGLBaseSceneObject; Tag: Integer; w1,h1,w2,h2: Single): Boolean;
Var
i: Integer;
P,Obj2: TGLBaseSceneObject;
t: array[1..4] of Integer;
begin
Result:= False;
for i:= 0 to CollisionsList.Count - 1 do
begin
P:= CollisionsList.Items[i];
if Assigned(P) then
if (P.Tag = Tag) and (P.Visible) then
begin
Obj2:= P;
if w1 < 0 then t[1]:= -1 else t[1]:= 1;
if h1 < 0 then t[2]:= -1 else t[2]:= 1;
if w2 < 0 then t[3]:= -1 else t[3]:= 1;
if h2 < 0 then t[4]:= -1 else t[4]:= 1;
if ((Obj1.Position.X + 0.1 + ((w1 / 2) * t[1]) >= (Obj2.Position.X - ((w2 / 2) * t[3]))) and
(Obj1.Position.X - 0.1 - ((w1 / 2) * t[1]) <= (Obj2.Position.X + ((w2 / 2) * t[3])))) and
((Obj1.Position.Y + 0.1 + ((h1 / 2) * t[2]) >= (Obj2.Position.Y - ((h2 / 2) * t[4]))) and
(Obj1.Position.Y - 0.1 - ((h1 / 2) * t[2]) <= (Obj2.Position.Y + ((h2 / 2) * t[4])))) then
begin
Obj1.TagObject:= Obj2;
Obj2.TagObject:= Obj1;
Result:= True;
Exit;
end;
end;
end;
end;
function Collision(Obj1: TGLBaseSceneObject; Obj2: TGLBaseSceneObject; w1,h1,w2,h2: Single): Boolean;
Var
t: array[1..4] of Integer;
begin
Result:= False;
if w1 < 0 then t[1]:= -1 else t[1]:= 1;
if h1 < 0 then t[2]:= -1 else t[2]:= 1;
if w2 < 0 then t[3]:= -1 else t[3]:= 1;
if h2 < 0 then t[4]:= -1 else t[4]:= 1;
if ((Obj1.Position.X + 0.1 + ((w1 / 2) * t[1]) >= (Obj2.Position.X - ((w2 / 2) * t[3]))) and
(Obj1.Position.X - 0.1 - ((w1 / 2) * t[1]) <= (Obj2.Position.X + ((w2 / 2) * t[3])))) and
((Obj1.Position.Y + 0.1 + ((h1 / 2) * t[2]) >= (Obj2.Position.Y - ((h2 / 2) * t[4]))) and
(Obj1.Position.Y - 0.1 - ((h1 / 2) * t[2]) <= (Obj2.Position.Y + ((h2 / 2) * t[4])))) then
begin
Obj1.TagObject:= Obj2;
Obj2.TagObject:= Obj1;
Result:= True;
end;
end;
end.