lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, May 29, 2006 at 06:57:58PM +0000, Jose Marin wrote:
> I don't have to check collisions twice, only one time
> for each pair of objects.
> 
> In pseudocode:
> 
> for obj1 = list.first to list.end do
>    for obj2 = list.first to list.end do
>     
>       if obj1.collideWith(obj2) then 
>         obj1.DoSomething(obj2)
>       end
>    end
> end

More of an algorithm question than Lua, but you probably want (using your
pseudocode):

for obj1 = list.first to list.end do
   for obj2 = obj1+1 to list.end do
      if obj1.collideWith(obj2) then 
        obj1.DoSomething(obj2)
      end
   end
end

or in Lua:

list = { "a", "b", "c", "d", "e" }
for k, v in next, list, nil do    -- same as pairs(list)
  for k2, v2 in next, list, k do  -- like pairs(list), but start at next(k)
    print(v .. ", " .. v2);
  end
end

-- 
Glenn Maynard