Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Simple BoxNormalCollision function — Gideros Forum

Simple BoxNormalCollision function

Maybe someone find it useful, I used it in my game that I never finished ..

function BoxNormalCollision(ObjA,ObjB)
    local x, y = 0,0 
    local a = {x= ObjA:getX(), y= ObjA:getY(), w= ObjA:getWidth(), h= ObjA:getHeight()}
    local b = {x= ObjB:getX(), y= ObjB:getY(), w= ObjB:getWidth(), h= ObjB:getHeight()}
 
    if -- first test for any collision
    (
        a.x < b.x+b.w and
        b.x < a.x+a.w and
        a.y < b.y+b.h and
        b.y < a.y+a.h
    ) 
    then -- test from where does collision come
        local ax0 = a.x + a.w /2
        local ay0 = a.y + a.h /2
        local bx0 = b.x + b.w /2
        local by0 = b.y + b.h /2
        --desno dolje
        if bx0 > ax0 and by0 > ay0 then
            if (a.x+a.w) - b.x > (a.y+a.h) - b.y then
                y = -1
                ObjA:setY(b.y-a.h)
            else
                x = -1
                ObjA:setX(b.x-a.w)
            end		
            return x,y
        --desno gore
        elseif bx0 > ax0 and by0 < ay0 then
            if (a.x+a.w) - b.x > (b.y+b.h) - a.y then
                y = 1
                ObjA:setY(b.y+b.h)
            else
                x = -1
                ObjA:setX(b.x-a.w)
            end		
            return x,y
        --lijevo dolje
        elseif bx0 < ax0 and by0 > ay0 then
            if (b.x+b.w) - a.x > (a.y+a.h) - b.y then
                y = -1
                ObjA:setY(b.y-a.h)
            else
                x = 1
                ObjA:setX(b.x+b.w)
            end
            return x,y
        --lijevo gore
        elseif bx0 < ax0 and by0 < ay0 then
            if (b.x+b.w ) - a.x > (b.y+b.h) - a.y then
                y = 1
                ObjA:setY(b.y+b.h)
            else
                x = 1
                ObjA:setX(b.x+b.w)
            end
            return x,y
        --
        end  
    end
    return x,y
end



-demo proj is attached
zip
zip
ncol_demo.zip
3K

Likes: Apollo14

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • Gideros has a plugin for a C version of Bump - it does this sort of collision detection (and more) and may be faster - you should take a look.

    http://wiki.giderosmobile.com/index.php/Special:MyLanguage/bump

    Here is the official Bump documentation:
    https://github.com/kikito/bump.lua

    Likes: oleg

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • olegoleg Member
    edited April 2019
    Another option for checking the collision
    function Sprite:collision(sprite2)
            -- self bottom < other sprite top
            if self:getY() + self:getHeight()/3 < sprite2:getY() then
                    return false
            end
            -- self top > other sprite bottom
            if self:getY() > sprite2:getY() + sprite2:getHeight() then
                    return false
            end
            -- self left > other sprite right
            if self:getX() > sprite2:getX() + sprite2:getWidth() then
                    return false
            end
            -- self right < other sprite left
            if self:getX() + self:getWidth()/3 < sprite2:getX() then
                    return false
            end
                    return true
    end
     
    -- class of object
     
    if self:collision(sprite5) then
     
    --actions in a collision
    end

    Likes: MoKaLux

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 )Share on Facebook
  • Yes, that works great too. I usually use this method, but inline. Bump has a few advantages as it calculates the rebound and the actual collision point - even if an object is travelling faster than the size of the collision square.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • antixantix Member
    Bump is basically a spatial hash with extra features (collision). You can query it with rectangles, points or segments. It is a fantastically robust addition to Gideros.
  • olegoleg Member
    I think we should be able to check the collision by various methods
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • Can someone convert my example project to cbump, I really would love to try it but don't have time to study it.

    Bump has a few advantages as it calculates the rebound and the actual collision point - even if an object is travelling faster than the size of the collision square.

    btw. I had problem in my game similar to this example I attached, If i put spacing between blocks a bit larger and the ball was hitting from right side, for example, lands on a block, and hits just about the corner of the previous brick causing ball to switch unwanted direction



    I know this happen cause it tests collision in 1,2,3 .. order from objs table, but for me it was weird enough to have this same issue with box2d


    rect2207.png
    600 x 523 - 13K
  • antixantix Member
    @gemboy100 head over to https://github.com/kikito/bump.lua and have a read. Gideros bump is a native (6 or so times faster) of that :)
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.