 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Danny769
Joined: 01 Feb 2006 Posts: 55
|
Posted: Sun Apr 30, 2006 11:45 am Post subject: Help (Image Collision in an Array) |
|
|
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 |
|
| Back to top |
|
 |
Danny769
Joined: 01 Feb 2006 Posts: 55
|
Posted: Tue May 02, 2006 8:14 pm Post subject: |
|
|
Would compairing the images in a double do loop do the job
| Code: | for i = 0, 20 do
for j = 0, 20 do
-- Colision for Array1.[i]Vs Array2.[j]
end
end |
What you think? |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Wed May 03, 2006 2:02 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Danny769
Joined: 01 Feb 2006 Posts: 55
|
Posted: Wed May 03, 2006 2:19 am Post subject: |
|
|
J = 1
I = 2
How would you do it? |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Wed May 03, 2006 7:21 am Post subject: |
|
|
| Danny769 wrote: | J = 1
I = 2
How would you do it? |
A first hack could look like this:
| Code: |
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
|
Would be better to create classes for rectangles with meta-functions for operators on it. |
|
| Back to top |
|
 |
Danny769
Joined: 01 Feb 2006 Posts: 55
|
Posted: Wed May 03, 2006 10:52 am Post subject: |
|
|
| Thanks ill try this, seem like alot of extra work though, |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|