author | unc0rr |
Fri, 04 May 2007 19:59:51 +0000 | |
changeset 505 | fcba7d7aea0d |
parent 504 | 13b6ebc53627 |
child 511 | 2b5b9e00419d |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uCollisions; |
|
20 |
interface |
|
351 | 21 |
uses uGears, uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
53 | 23 |
const cMaxGearArrayInd = 255; |
4 | 24 |
|
70 | 25 |
type PGearArray = ^TGearArray; |
53 | 26 |
TGearArray = record |
27 |
ar: array[0..cMaxGearArrayInd] of PGear; |
|
28 |
Count: Longword |
|
29 |
end; |
|
4 | 30 |
|
53 | 31 |
procedure AddGearCI(Gear: PGear); |
32 |
procedure DeleteCI(Gear: PGear); |
|
33 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
|
371 | 34 |
function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean; |
35 |
function TestCollisionYwithGear(Gear: PGear; Dir: LongInt): boolean; |
|
36 |
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean; |
|
498 | 37 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean; |
371 | 38 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 39 |
|
40 |
implementation |
|
57 | 41 |
uses uMisc, uConsts, uLand, uLandGraphics; |
4 | 42 |
|
53 | 43 |
type TCollisionEntry = record |
371 | 44 |
X, Y, Radius: LongInt; |
53 | 45 |
cGear: PGear; |
46 |
end; |
|
351 | 47 |
|
4 | 48 |
const MAXRECTSINDEX = 255; |
49 |
var Count: Longword = 0; |
|
53 | 50 |
cinfos: array[0..MAXRECTSINDEX] of TCollisionEntry; |
51 |
ga: TGearArray; |
|
4 | 52 |
|
53 | 53 |
procedure AddGearCI(Gear: PGear); |
54 |
begin |
|
351 | 55 |
if Gear^.CollIndex < High(Longword) then exit; |
4 | 56 |
TryDo(Count <= MAXRECTSINDEX, 'Collision rects array overflow', true); |
53 | 57 |
with cinfos[Count] do |
4 | 58 |
begin |
351 | 59 |
X:= hwRound(Gear^.X); |
60 |
Y:= hwRound(Gear^.Y); |
|
61 |
Radius:= Gear^.Radius; |
|
504
13b6ebc53627
Fix collision info artifacts in Land array when two objects intersect
unc0rr
parents:
498
diff
changeset
|
62 |
ChangeRoundInLand(X, Y, Radius - 1, +1); |
4 | 63 |
cGear:= Gear |
64 |
end; |
|
351 | 65 |
Gear^.CollIndex:= Count; |
4 | 66 |
inc(Count) |
67 |
end; |
|
68 |
||
53 | 69 |
procedure DeleteCI(Gear: PGear); |
4 | 70 |
begin |
351 | 71 |
if Gear^.CollIndex < Count then |
53 | 72 |
begin |
504
13b6ebc53627
Fix collision info artifacts in Land array when two objects intersect
unc0rr
parents:
498
diff
changeset
|
73 |
with cinfos[Gear^.CollIndex] do |
13b6ebc53627
Fix collision info artifacts in Land array when two objects intersect
unc0rr
parents:
498
diff
changeset
|
74 |
ChangeRoundInLand(X, Y, Radius - 1, -1); |
351 | 75 |
cinfos[Gear^.CollIndex]:= cinfos[Pred(Count)]; |
76 |
cinfos[Gear^.CollIndex].cGear^.CollIndex:= Gear^.CollIndex; |
|
77 |
Gear^.CollIndex:= High(Longword); |
|
53 | 78 |
dec(Count) |
79 |
end; |
|
4 | 80 |
end; |
81 |
||
53 | 82 |
function CheckGearsCollision(Gear: PGear): PGearArray; |
371 | 83 |
var mx, my: LongInt; |
4 | 84 |
i: Longword; |
351 | 85 |
Result: PGearArray; |
4 | 86 |
begin |
53 | 87 |
Result:= @ga; |
88 |
ga.Count:= 0; |
|
12
366adfa1a727
Fix reading out of bounds of the collisions array. This fixes flying hedgehogs and not moving after explosion
unc0rr
parents:
4
diff
changeset
|
89 |
if Count = 0 then exit; |
351 | 90 |
mx:= hwRound(Gear^.X); |
91 |
my:= hwRound(Gear^.Y); |
|
4 | 92 |
|
93 |
for i:= 0 to Pred(Count) do |
|
53 | 94 |
with cinfos[i] do |
95 |
if (Gear <> cGear) and |
|
351 | 96 |
(sqrt(sqr(mx - x) + sqr(my - y)) <= Radius + Gear^.Radius) then |
4 | 97 |
begin |
53 | 98 |
ga.ar[ga.Count]:= cinfos[i].cGear; |
99 |
inc(ga.Count) |
|
4 | 100 |
end; |
351 | 101 |
CheckGearsCollision:= Result |
4 | 102 |
end; |
103 |
||
371 | 104 |
function TestCollisionXwithGear(Gear: PGear; Dir: LongInt): boolean; |
105 |
var x, y, i: LongInt; |
|
505
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
106 |
TestWord: LongWord; |
4 | 107 |
begin |
505
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
108 |
if Gear^.IntersectGear <> nil then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
109 |
with Gear^ do |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
110 |
if (hwRound(IntersectGear^.X) + IntersectGear^.Radius < hwRound(X) - Radius) or |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
111 |
(hwRound(IntersectGear^.X) - IntersectGear^.Radius > hwRound(X) + Radius) then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
112 |
begin |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
113 |
IntersectGear:= nil; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
114 |
TestWord:= 0 |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
115 |
end else |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
116 |
TestWord:= COLOR_LAND - 1 |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
117 |
else TestWord:= 0; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
118 |
|
351 | 119 |
x:= hwRound(Gear^.X); |
120 |
if Dir < 0 then x:= x - Gear^.Radius |
|
121 |
else x:= x + Gear^.Radius; |
|
4 | 122 |
if (x and $FFFFF800) = 0 then |
123 |
begin |
|
351 | 124 |
y:= hwRound(Gear^.Y) - Gear^.Radius + 1; |
125 |
i:= y + Gear^.Radius * 2 - 2; |
|
4 | 126 |
repeat |
351 | 127 |
if (y and $FFFFFC00) = 0 then |
505
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
128 |
if Land[y, x] > TestWord then exit(true); |
4 | 129 |
inc(y) |
351 | 130 |
until (y > i); |
131 |
end; |
|
132 |
TestCollisionXwithGear:= false |
|
4 | 133 |
end; |
134 |
||
505
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
135 |
function TestCollisionYwithGear(Gear: PGear; Dir: LongInt): boolean; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
136 |
var x, y, i: LongInt; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
137 |
TestWord: LongWord; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
138 |
begin |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
139 |
if Gear^.IntersectGear <> nil then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
140 |
with Gear^ do |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
141 |
if (hwRound(IntersectGear^.Y) + IntersectGear^.Radius < hwRound(Y) - Radius) or |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
142 |
(hwRound(IntersectGear^.Y) - IntersectGear^.Radius > hwRound(Y) + Radius) then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
143 |
begin |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
144 |
IntersectGear:= nil; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
145 |
TestWord:= 0 |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
146 |
end else |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
147 |
TestWord:= COLOR_LAND - 1 |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
148 |
else TestWord:= 0; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
149 |
|
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
150 |
y:= hwRound(Gear^.Y); |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
151 |
if Dir < 0 then y:= y - Gear^.Radius |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
152 |
else y:= y + Gear^.Radius; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
153 |
if (y and $FFFFFC00) = 0 then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
154 |
begin |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
155 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
156 |
i:= x + Gear^.Radius * 2 - 2; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
157 |
repeat |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
158 |
if (x and $FFFFF800) = 0 then |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
159 |
if Land[y, x] > TestWord then exit(true); |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
160 |
inc(x) |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
161 |
until (x > i); |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
162 |
end; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
163 |
TestCollisionYwithGear:= false |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
164 |
end; |
fcba7d7aea0d
Fix old bug with grenade(bomd, etc..) not colliding with attacking hedgehog
unc0rr
parents:
504
diff
changeset
|
165 |
|
498 | 166 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 167 |
begin |
351 | 168 |
Gear^.X:= Gear^.X + ShiftX; |
498 | 169 |
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY); |
351 | 170 |
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir); |
171 |
Gear^.X:= Gear^.X - ShiftX; |
|
498 | 172 |
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY) |
4 | 173 |
end; |
174 |
||
371 | 175 |
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean; |
176 |
var x, y, i: LongInt; |
|
68 | 177 |
begin |
351 | 178 |
y:= hwRound(Gear^.Y); |
179 |
if Dir < 0 then y:= y - Gear^.Radius |
|
180 |
else y:= y + Gear^.Radius; |
|
68 | 181 |
if (y and $FFFFFC00) = 0 then |
182 |
begin |
|
351 | 183 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
184 |
i:= x + Gear^.Radius * 2 - 2; |
|
68 | 185 |
repeat |
351 | 186 |
if (x and $FFFFF800) = 0 then |
187 |
if Land[y, x] = COLOR_LAND then exit(true); |
|
68 | 188 |
inc(x) |
351 | 189 |
until (x > i); |
190 |
end; |
|
191 |
TestCollisionY:= false |
|
68 | 192 |
end; |
193 |
||
371 | 194 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 195 |
begin |
498 | 196 |
Gear^.X:= Gear^.X + int2hwFloat(ShiftX); |
197 |
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY); |
|
351 | 198 |
TestCollisionYwithXYShift:= TestCollisionYwithGear(Gear, Dir); |
498 | 199 |
Gear^.X:= Gear^.X - int2hwFloat(ShiftX); |
200 |
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY) |
|
4 | 201 |
end; |
202 |
||
203 |
end. |