How Do I do an image Collision check for X,Y Controled Objects In an Array?
Yes i know leave it for me to come up with the fun problems
Help (Image Collision in an Array)
Moderators: Shine, Insert_witty_name
Would compairing the images in a double do loop do the job
What you think?
Code: Select all
for i = 0, 20 do
for j = 0, 20 do
-- Colision for Array1.[i]Vs Array2.[j]
end
end
A first hack could look like this:Danny769 wrote:J = 1
I = 2
How would you do it?
Code: Select all
function isInRectangle(x, y, x0, y0, x1, y1)
return x >= x0 and x < x1 and y >= y0 and y < y1
end
function overlaps(object1, object2)
local o1_x0 = object1.x0
local o1_y0 = object1.y0
local o1_x1 = o1_x0 + object1.boundingBoxWidth
local o1_y1 = o1_y0 + object1.boundingBoxHeight
local o2_x0 = object2.x0
local o2_y0 = object2.y0
local o2_x1 = o2_x0 + object2.boundingBoxWidth
local o2_y1 = o2_y0 + object2.boundingBoxHeight
return isInRectangle(o1_x0, o1_y0, o2_x0, o2_y0, o2_x1, o2_y1) or
isInRectangle(o1_x1, o1_y1, o2_x0, o2_y0, o2_x1, o2_y1) or
isInRectangle(o2_x0, o2_y0, o1_x0, o1_y0, o1_x1, o1_y1) or
isInRectangle(o2_x1, o2_y1, o1_x0, o1_y0, o1_x1, o1_y1)
end
objects = {
{ x0=60, y0=20, boundingBoxWidth=50, boundingBoxHeight=30 },
{ x0=20, y0=20, boundingBoxWidth=50, boundingBoxHeight=30 }}
for i = 1, table.getn(objects) do
for j = i, table.getn(objects) do
if i ~= j then
local object1 = objects[i]
local object2 = objects[j]
if overlaps(object1, object2) then
print("collision of " .. i .. " and " .. j)
end
end
end
end