author | unc0rr |
Wed, 02 May 2007 21:37:08 +0000 | |
changeset 504 | 13b6ebc53627 |
parent 498 | 9c8b385dc9a1 |
child 505 | fcba7d7aea0d |
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; |
|
4 | 106 |
begin |
351 | 107 |
x:= hwRound(Gear^.X); |
108 |
if Dir < 0 then x:= x - Gear^.Radius |
|
109 |
else x:= x + Gear^.Radius; |
|
4 | 110 |
if (x and $FFFFF800) = 0 then |
111 |
begin |
|
351 | 112 |
y:= hwRound(Gear^.Y) - Gear^.Radius + 1; |
113 |
i:= y + Gear^.Radius * 2 - 2; |
|
4 | 114 |
repeat |
351 | 115 |
if (y and $FFFFFC00) = 0 then |
116 |
if Land[y, x] <> 0 then exit(true); |
|
4 | 117 |
inc(y) |
351 | 118 |
until (y > i); |
119 |
end; |
|
120 |
TestCollisionXwithGear:= false |
|
4 | 121 |
end; |
122 |
||
498 | 123 |
function TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 124 |
begin |
351 | 125 |
Gear^.X:= Gear^.X + ShiftX; |
498 | 126 |
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY); |
351 | 127 |
TestCollisionXwithXYShift:= TestCollisionXwithGear(Gear, Dir); |
128 |
Gear^.X:= Gear^.X - ShiftX; |
|
498 | 129 |
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY) |
4 | 130 |
end; |
131 |
||
371 | 132 |
function TestCollisionYwithGear(Gear: PGear; Dir: LongInt): boolean; |
133 |
var x, y, i: LongInt; |
|
4 | 134 |
begin |
351 | 135 |
y:= hwRound(Gear^.Y); |
136 |
if Dir < 0 then y:= y - Gear^.Radius |
|
137 |
else y:= y + Gear^.Radius; |
|
4 | 138 |
if (y and $FFFFFC00) = 0 then |
139 |
begin |
|
351 | 140 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
141 |
i:= x + Gear^.Radius * 2 - 2; |
|
4 | 142 |
repeat |
351 | 143 |
if (x and $FFFFF800) = 0 then |
144 |
if Land[y, x] <> 0 then exit(true); |
|
4 | 145 |
inc(x) |
351 | 146 |
until (x > i); |
147 |
end; |
|
148 |
TestCollisionYwithGear:= false |
|
4 | 149 |
end; |
150 |
||
371 | 151 |
function TestCollisionY(Gear: PGear; Dir: LongInt): boolean; |
152 |
var x, y, i: LongInt; |
|
68 | 153 |
begin |
351 | 154 |
y:= hwRound(Gear^.Y); |
155 |
if Dir < 0 then y:= y - Gear^.Radius |
|
156 |
else y:= y + Gear^.Radius; |
|
68 | 157 |
if (y and $FFFFFC00) = 0 then |
158 |
begin |
|
351 | 159 |
x:= hwRound(Gear^.X) - Gear^.Radius + 1; |
160 |
i:= x + Gear^.Radius * 2 - 2; |
|
68 | 161 |
repeat |
351 | 162 |
if (x and $FFFFF800) = 0 then |
163 |
if Land[y, x] = COLOR_LAND then exit(true); |
|
68 | 164 |
inc(x) |
351 | 165 |
until (x > i); |
166 |
end; |
|
167 |
TestCollisionY:= false |
|
68 | 168 |
end; |
169 |
||
371 | 170 |
function TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt): boolean; |
4 | 171 |
begin |
498 | 172 |
Gear^.X:= Gear^.X + int2hwFloat(ShiftX); |
173 |
Gear^.Y:= Gear^.Y + int2hwFloat(ShiftY); |
|
351 | 174 |
TestCollisionYwithXYShift:= TestCollisionYwithGear(Gear, Dir); |
498 | 175 |
Gear^.X:= Gear^.X - int2hwFloat(ShiftX); |
176 |
Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY) |
|
4 | 177 |
end; |
178 |
||
179 |
end. |