Help (Image Collision in an Array)

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Danny769
Posts: 55
Joined: Wed Feb 01, 2006 12:29 pm

Help (Image Collision in an Array)

Post by Danny769 »

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
Danny769
Posts: 55
Joined: Wed Feb 01, 2006 12:29 pm

Post by Danny769 »

Would compairing the images in a double do loop do the job

Code: Select all

for i = 0, 20 do
   for j = 0, 20 do
      -- Colision for Array1.[i]Vs Array2.[j]
   end
end
What you think?
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

you will double check images then

as you do check:
1 -> 1 <-- nonsense always collision
1 -> 2
1 -> 3
1 -> ..
1 -> 20

2 -> 1 <-- already checked in first "pass"
2 -> 2 <-- nonsense always collision
2 -> ..

greets
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Danny769
Posts: 55
Joined: Wed Feb 01, 2006 12:29 pm

Post by Danny769 »

J = 1
I = 2

How would you do it?
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Danny769 wrote:J = 1
I = 2

How would you do it?
A first hack could look like this:

Code: Select all

function isInRectangle&#40;x, y, x0, y0, x1, y1&#41;
	return x >= x0 and x < x1 and y >= y0 and y < y1
end

function overlaps&#40;object1, object2&#41;
	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&#40;o1_x0, o1_y0, o2_x0, o2_y0, o2_x1, o2_y1&#41; or
		isInRectangle&#40;o1_x1, o1_y1, o2_x0, o2_y0, o2_x1, o2_y1&#41; or
		isInRectangle&#40;o2_x0, o2_y0, o1_x0, o1_y0, o1_x1, o1_y1&#41; or
		isInRectangle&#40;o2_x1, o2_y1, o1_x0, o1_y0, o1_x1, o1_y1&#41;
end

objects = &#123;
	&#123; x0=60, y0=20, boundingBoxWidth=50, boundingBoxHeight=30 &#125;,
	&#123; x0=20, y0=20, boundingBoxWidth=50, boundingBoxHeight=30 &#125;&#125;
for i = 1, table.getn&#40;objects&#41; do
	for j = i, table.getn&#40;objects&#41; do
		if i ~= j then
			local object1 = objects&#91;i&#93;
			local object2 = objects&#91;j&#93;
			if overlaps&#40;object1, object2&#41; then
				print&#40;"collision of " .. i .. " and " .. j&#41;
			end
		end
	end
end
Would be better to create classes for rectangles with meta-functions for operators on it.
Danny769
Posts: 55
Joined: Wed Feb 01, 2006 12:29 pm

Post by Danny769 »

Thanks ill try this, seem like alot of extra work though,
Post Reply