aabb collision need help...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
dalmuti
Prole
Posts: 4
Joined: Fri Feb 12, 2016 5:36 am

aabb collision need help...

Post by dalmuti »

obj = {}
function obj_create()
-- issential: y x,y,w,h that im using in aabb to detect collision when the rectangle is created.
table.insert(obj, {x=0, y=0, width=32, height=32, xspeed=0, yspeed=true})
end

i have a timer set to create an obj every so many secands, and that is added to the table of obj = {}
the issue im having is iterating through the objects and telling the program what all the different variable are needed and that they are different.

function aabb(x1,y1,w1,h1, x2,y2,w2,h2) -- generic equation to check of to objects overlap then return true/false
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end


function collision(v1, v2, dt) --issue is here. differentiating between the v1, and v2, (the two tables im getting from obj = {})
if aabb(v1.x, v1.y, v1.width, v1.height, v2.x, v2.y, v2.width, v2.height)then
if (variable to adjust were the collision changes, i got them, not needed here atm) then
yspeed = 0 -- block are spawned at top of screen, when they collide sets speed to 0
v1.y = v2.y - v1.height -- placment of said blocks
end
end
end

for k,v in pairs(obj)do-- so something like 1:table(that has x,y,w,h) , 2:table... and so forth for every rectangle obj inside the table
collision(v1, v2, dt)-- how do i specify what 2 table to to compare????
end
Last edited by dalmuti on Fri Feb 12, 2016 7:02 pm, edited 1 time in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: aabb collision with metatables? need help

Post by Roland_Yonaba »

Let us say obj is an array list holding the objects you are trying to perform collision checks upon. SInce they are ordered, you can basically go with nested for loops.

Code: Select all

for i in ipairs(obj) do
  for j in ipairs(obj) do
    if i ~= j then -- This is just to avoid testing an object against itself, as it will obviously return a true.
      collision(obj[i], obj[j], dt)
      end
  end
end 
This one is fairly simple to get started with. But...

Bear in mind one thing though...the approach is very "gross" and it is not performance wise. Let us say obj contains a list of four objects, named 1,2, 3 and 4. With the previous code, you will be performing the following pair of tests.

(1 vs 2)
(1 vs 3)
(1 vs 4)
(2 vs 1) -- this is a repetition of (1 vs 2), hence unneeded
(2 vs 3)
(2 vs 4)
(3 vs 1) -- this is a repetition of (1 vs 3), hence unneeded
(3 vs 2) -- this is a repetition of (2 vs 3), hence unneeded
(3 vs 4)
(4 vs 1) -- this is a repetition of (1 vs 4), hence unneeded
(4 vs 2) -- this is a repetition of (2 vs 4), hence unneeded
(4 vs 3) -- this is a repetition of (3 vs 4), hence unneeded

With n objects, you will perform (n-1)*n checks, where (n-1)*n/2 (the half, actually) would be useless.
Assuming n = 4, you will have 12 tests, where 6 test are unneeded.
For n = 50, you will have 2450 checks in total, where 1225 tests are repetitive ones.
In case n = 100, it will be 9900 checks in total, where 4950 tests would be useless.

This is just because collision check is commutative. I.e, if objectA collides with objectB, then objectB also collides with objectA. Taking advantage of that, one can reduce the number of collision checks in the following way:

Code: Select all

local n = #obj -- Get the number of objects listed in table obj
for i = 1, n-1 do
  for j = i + 1, n do
    collisionTest(obj[i], obj[j])
  end
end
In that case, with the previous example (table of 4 objects) you will perform the exact number of required tests.

Iteration 1 : i = 1 and j = 2..4
(1 vs 2)
(1 vs 3)
(1 vs 4)
Iteration 2: i = 2 and j = 3..4
(2 vs 3)
(2 vs 4)
Iteration 3 : i = 3 and j = 4
(3 vs 4)

Hence, only 6 tests in total.

Sure, there might be some other optimizations, but this is enough to get started with most use-cases.
Hope all of this helps.

PS/ You mentionned metatables in your question... I don't know where you get that word, but there are no metatables involved here in any portion of your code, nor in my answer. Metatables are a specific feature of Lua that most people consider to be an advanced topic. Feel free to read about that in PiL. But do not feel any urge to learn to use them unless really necessary.
dalmuti
Prole
Posts: 4
Joined: Fri Feb 12, 2016 5:36 am

Re: aabb collision with metatables? need help

Post by dalmuti »

the first method works, but I see how that is repetitive,
so trying to avoid it and go with the second method.
I most likely missing something, but aren't the i and j values just pointing to a position in the table?
You would then you would call the table with obj,
wouldn't properly work unless I change obj and obj[j] positions, whys that?

Code: Select all

local n = #obj
for i = n-1 do
	for j = i+1, n do
		collision(obj[i], obj[j] dt)
	end
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests