1 --these are functions for doing collision detection
3 --unless one wants to write their own routines, then the
4 -- collide_Object_bounds() and collide_Object_Rects() shouldn't be used
6 -- Instead, please use the check_collisions_*() functions
9 function collide_Object_bounds(a
, b
)
10 --this is provided as a convenience so that one can make objects invisible to collisions
11 if a
.death_anim
== true or b
.death_anim
== true then return false end
13 local x1
, x2
, y1
, y2
, x3
, x4
, y3
, y4
= a
.x
, a
.x
+ a
.w
, a
.y
, a
.y
+ a
.h
, b
.x
, b
.x
+ b
.w
, b
.y
, b
.y
+ b
.h
16 if ((x1
>= x3
and x2
<= x4
) and (y1
>= y3
and y2
<= y4
)) or
17 ((x3
>= x1
and x4
<= x2
) and (y3
>= y1
and y4
<= y2
)) then
22 if ((x1
>= x3
and x1
<= x4
) and (y1
>= y3
and y1
<= y4
)) or
23 ((x3
>= x1
and x3
<= x2
) and (y3
>= y1
and y3
<= y2
)) then
28 if ((x2
>= x3
and x2
<= x4
) and (y1
>= y3
and y1
<= y4
)) or
29 ((x4
>= x1
and x4
<= x2
) and (y3
>= y1
and y3
<= y2
)) then
34 if ((x1
>= x3
and x1
<= x4
) and (y2
>= y3
and y2
<= y4
)) or
35 ((x3
>= x1
and x3
<= x2
) and (y4
>= y1
and y4
<= y2
)) then
40 if ((x1
>= x3
and x1
<= x4
) and (y2
>= y3
and y2
<= y4
)) or
41 ((x3
>= x1
and x3
<= x2
) and (y4
>= y1
and y4
<= y2
)) then
46 if ((x1
<= x3
and x2
>= x4
) and (y1
>= y3
and y1
<= y4
)) or
47 ((x3
<= x1
and x4
>= x2
) and (y3
>= y1
and y3
<= y2
)) then
52 if ((x1
<= x3
and x1
>= x4
) and (y1
>= y3
and y2
<= y4
)) or
53 ((x3
<= x1
and x3
>= x2
) and (y3
>= y1
and y4
<= y2
)) then
58 if ((x2
<= x3
and x2
>= x4
) and (y1
>= y3
and y2
<= y4
)) or
59 ((x4
<= x1
and x4
>= x2
) and (y3
>= y1
and y4
<= y2
)) then
64 if ((x1
<= x3
and x2
>= x4
) and (y2
>= y3
and y2
<= y4
)) or
65 ((x3
<= x1
and x4
>= x2
) and (y4
>= y1
and y4
<= y2
)) then
73 function collide_Object_Rects(a
, b
)
74 if a
.death_anim
or b
.death_anim
then return false end
76 local a_ids
= ObjectList
:new()
77 local b_ids
= ObjectList
:new()
79 for r1
in a
.rects
:iterator() do
80 for r2
in b
.rects
:iterator() do
84 local x1
, x2
, y1
, y2
, x3
, x4
, y3
, y4
= r1
.x
+a
.x
, r1
.x
+a
.x
+a
.w
, r1
.y
+a
.y
, r1
.y
+a
.y
+a
.h
, r2
.x
+b
.x
, r2
.x
+b
.x
+b
.w
, r2
.y
+b
.y
, r2
.y
+b
.y
+b
.h
87 if ((x1
>= x3
and x2
<= x4
) and (y1
>= y3
and y2
<= y4
)) or
88 ((x3
>= x1
and x4
<= x2
) and (y3
>= y1
and y4
<= y2
)) then
90 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
91 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
95 if flag
== false and ((x1
>= x3
and x1
<= x4
) and (y1
>= y3
and y1
<= y4
)) or
96 ((x3
>= x1
and x3
<= x2
) and (y3
>= y1
and y3
<= y2
)) then
98 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
99 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
103 if flag
== false and ((x2
>= x3
and x2
<= x4
) and (y1
>= y3
and y1
<= y4
)) or
104 ((x4
>= x1
and x4
<= x2
) and (y3
>= y1
and y3
<= y2
)) then
106 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
107 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
111 if flag
== false and ((x1
>= x3
and x1
<= x4
) and (y2
>= y3
and y2
<= y4
)) or
112 ((x3
>= x1
and x3
<= x2
) and (y4
>= y1
and y4
<= y2
)) then
114 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
115 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
118 --bottom right corner
119 if flag
== false and ((x1
>= x3
and x1
<= x4
) and (y2
>= y3
and y2
<= y4
)) or
120 ((x3
>= x1
and x3
<= x2
) and (y4
>= y1
and y4
<= y2
)) then
122 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
123 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
127 if flag
== false and ((x1
<= x3
and x2
>= x4
) and (y1
>= y3
and y1
<= y4
)) or
128 ((x3
<= x1
and x4
>= x2
) and (y3
>= y1
and y3
<= y2
)) then
130 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
131 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
135 if flag
== false and ((x1
<= x3
and x1
>= x4
) and (y1
>= y3
and y2
<= y4
)) or
136 ((x3
<= x1
and x3
>= x2
) and (y3
>= y1
and y4
<= y2
)) then
138 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
139 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
143 if flag
== false and ((x2
<= x3
and x2
>= x4
) and (y1
>= y3
and y2
<= y4
)) or
144 ((x4
<= x1
and x4
>= x2
) and (y3
>= y1
and y4
<= y2
)) then
146 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
147 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
151 if flag
== false and ((x1
<= x3
and x2
>= x4
) and (y2
>= y3
and y2
<= y4
)) or
152 ((x3
<= x1
and x4
>= x2
) and (y4
>= y1
and y4
<= y2
)) then
154 if a_ids
:exists(r1
.id
) == false then a_ids
:push_back(r1
.id
) end
155 if b_ids
:exists(r2
.id
) == false then b_ids
:push_back(r2
.id
) end
165 --checks for collisions between two lists and calls the collide() function of each
166 function check_collisions_lists(list1
, list2
)
168 for obj1
in list1
:iterator() do
169 for obj2
in list2
:iterator() do
170 if obj1
~= obj2
and collide_Object_bounds(obj1
, obj2
) then
171 id1
, id2
= collide_Object_Rects(obj1
, obj2
)
172 obj1
:collide(id1
, obj2
.type)
173 obj2
:collide(id2
, obj1
.type)
179 --checks for collisions between an object and a list
180 function check_collisions_obj_list(obj
, list2
)
182 for obj2
in list2
:iterator() do
183 if obj1
~= obj2
and collide_Object_bounds(obj
, obj2
) then
184 id1
, id2
= collide_Object_Rects(obj
, obj2
)
185 obj
:collide(id1
, obj2
.type)
186 obj2
:collide(id2
, obj
.type)
191 --checks for collisions between two objects
192 function check_collisions_obj_obj(obj1
, obj2
)
194 if obj1
~= obj2
and collide_Object_bounds(obj1
, obj2
) then
195 id1
, id2
= collide_Object_Rects(obj1
, obj2
)
196 obj1
:collide(id1
, obj2
.type)
197 obj2
:collide(id2
, obj1
.type)
201 --checks for collisions between an object and a list. returns true or false.
202 function check_collisions_any(obj
, list
)
204 for obj2
in list2
:iterator() do
205 if collide_Object_bounds(obj
, obj2
) then
206 id1
, id2
= collide_Object_Rects(obj
, obj2
)
207 if id1
.size
> 0 and id2
.size
> 0 then