AABB’s for complex objects in lua.

I touched on complex objects a bit in my previous collision detection post, and now I plan to talk more about them.  Specifically, creating AABB‘s for complex objects, and drawing complex objects.  You can find the example löve file here.

For the purposes of this post, I will define a complex object as a set of points that are connected in a specific order to create a shape.  What do I mean by that?  Something along the lines of this:

obj1 = {{10, 50},{20, 30},{30, 70},{70, 90},{20, 10},{70, 30}}

That would create this object:

Image broken, use link untill I fix it.

I have created 2 useful functions when dealing with complex objects.  A function to draw a complex object, and a function to calculate the AABB of a complex object.

To draw a complex object, use this:

function drawcompobj(obj)
    local ppx = obj[#obj][1]
    local ppy = obj[#obj][2]
    for i=1, #obj, 1 do
        love.graphics.line(ppx, ppy, obj[i][1], obj[i][2])
        ppx = obj[i][1]
        ppy = obj[i][2]
    end
end

Line by line, here’s what it does:

    local ppx = obj[#obj][1]
    local ppy = obj[#obj][2]

This sets ppx and ppy to the last coordinates in the array.  these will be used as the starting point for each line it draws.

for i=1, #obj, 1 do

This loops through all of the coordinates in the array “obj.”  We will draw a line for each one of these points.

love.graphics.line(ppx, ppy, obj[i][1], obj[i][2])

This draws a line from the previous point to the current point.

ppx = obj[i][1]
ppy = obj[i][2]

This sets the previous points to the current points.

I also have a way to get an AABB for a complex object.  All this does is loops through all of the points, and gets the biggest and smallest X’s and Y’s.  Here is the code:

function makeaabb(obj)
    local minx, maxx, miny, maxy = obj[1][1], obj[1][1], obj[1][2], obj[1][2]

    for i=1, #obj, 1 do
        if obj[i][1] > maxx then maxx = obj[i][1] end
        if obj[i][1] < minx then minx = obj[i][1] end
        if obj[i][2] > maxy then maxy = obj[i][2] end
        if obj[i][2] < miny then miny = obj[i][2] end
    end

    return {minx, miny, (maxx - minx), (maxy - miny)}
end

Here’s what it does, line by line:

local minx, maxx, miny, maxy = obj[1][1], obj[1][1], obj[1][2], obj[1][2]

This sets the default minimum and maximum values for X and Y.

for i=1, #obj, 1 do

This loops through all of the points in the complex object.

if obj[i][1] > maxx then maxx = obj[i][1] end
--Other similar stuff.

This sets the current highest x value to reflect it being compared to the previous highest x value.  The other 3 lines do basically the same thing.

return {minx, miny, (maxx - minx), (maxy - miny)}

This returns the x, y, width and height of the complex object.

I hope you find these functions useful, or at least that you enjoyed reading about them.

Once again, you can find the example löve file here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s