Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
more complex collision detectors — Gideros Forum

more complex collision detectors

aimoiaimoi Member
edited August 2015 in Game & application design
Imagine I have a circle shape in .png format and a maze also in .png format. I have stored them inside two sprites. The circle can move inside the maze. I want to know if circle has touched the maze walls. What should I do?
apparently simple collision tests wont help me here (because the maze is not a simple rectangle shape). something like pixel-based collision tests seem to work there. I have searched a lot and finally concluded I should learn box2d to handle such a test. I started that and there I found a "b2TestOverLap" function which I didn't find replace for it in gideros. the only thing that I have found in gideros is b2Contact. but I didnt understand how to use it. The documentations of the gideros are not clear enough for me in this case and I didnt find any other explanation for b2contact. so help me. how can i use b2contact to know if two sprites have touched each other? Or is there a better way to find such a collision?

Comments

  • piepie Member
    edited August 2015 Accepted Answer
    Hi @aimoi ,
    everything I know to check collisions uses "objects/shapes" (every wall of your maze should be a "single object" so that its boundaries are clearly defined).

    You can check:
    http://giderosmobile.com/forum/discussion/2286/v2-a-new-tool-tiled-as-a-level-and-box2d-world-physics-editor-for-gideros/p1
    for box2d and tiled map editor (should be easy to draw a maze with tiled, but maybe using box2d just for collisions is a bit too much)

    tnt collision engine - really fast plugin for gideros by gregBug
    http://www.tntparticlesengine.com/?cat=15

    this seems to be promising, full lua, but I never tried it
    https://github.com/kikito/bump.lua

    or you can check between circle shape and maze walls (not walls as one big sprite, but each wall as a single sprite) with Sprite:collidesWith() by gregBug / ar2rsawseen (I lost who wrote it :) )
    function Sprite:collidesWith(sprite2)
    	local x,y,w,h = self:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
     
    	return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
    end
     
    --usage like
    if sprite1:collidesWith(sprite2) then
        --sprite2 is hit
    end
    [edit] I forgot, maybe you can also check using hitTestPoint()



    Likes: aimoi

    +1 -1 (+1 / -0 )Share on Facebook
  • @pie
    Thank you for your meek answer. (:
    In fact I thought there would be other than "objects/shapes" ways for detecting collisions. In fact my maze is a big sprite with walls inside it. look at attachments (every black line is a wall and the ball shall not be passed through them). but according to the circular shapes of the walls, I don't know how to break my wall to little objects to use common collision tests with.

    Also about using hitTestPoint(), is there a way to find all the points that a sprite has covered? So then I can loop over them and see if user has touched one of them or not.
    level1.png
    1080 x 1920 - 17K
  • piepie Member
    @aimoi
    that's tricky :)

    I think that you may use some snippet from this discussion:
    http://giderosmobile.com/forum/discussion/218/how-to-get-spritehittestpoint-to-ignore-transparent-areas-of-image/p1

    if you draw a raw shape over the maze (keep it invisible, just to check on less vertices than using "real" circles), theoretically you can check if it is "overlapping" with another shape; maybe to keep the collisions to a higher definition it's better to split the maze in some smaller shapes and check only the one you have to deal with (using the distance from the center of the maze?).

    I don't know if it is the best approach, just guessing :)

    Likes: aimoi

    +1 -1 (+1 / -0 )Share on Facebook
  • aimoiaimoi Member
    edited August 2015
    so for last conclusion, there is no clear approach to detect any collision between my maze (concern that it is not some lines drowned on a white background. In fact it's the lines themselves. there is nothing inside the circular structures. check the attachments.) and my circle shape. I first thought if I make a sprite by this maze structure and a sprite by a circle shape, I can detect collisions between the shape and the lines inside the maze. but seems like I should use another way for it.
    Untitled.png
    317 x 382 - 31K
  • @aimoi - well I thought of one solution but it would be interesting to implement. If you used bump.lua (I love that) then each pixel in your maze could be mapped as a rectangle in the bump world. It might be tricky and processor intensive checking the pixels of your circle against all those rectangles though.

    There is another interesting lua collision library that might be of interest to you. I didn;t read much about it but it does polygons and circles and stuff so maybe it could be of help..

    http://vrld.github.io/HardonCollider/

    Likes: aimoi

    +1 -1 (+1 / -0 )Share on Facebook
  • @aimoi - Hey did you ever solve this issue?
  • @antix
    Well, I gave a try to checking pixel by pixel collisions, but it was inefficient. In fact beside the "Processor Insensitivity" issues, I faced a lot of problems with breaking my maze into simple rectangles. At last (with a little embarrassment) I found using the "hitTestPoint()" the best approach into my case.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • @aimoi - Ahh ok cool. That works okay?

    I ask because I thought you could have your maze defined as rings (from inner to outer) and each ring could be split into segments. Then you could use a simple "point in circle" function to limit the players movement, if you get what I'm trying to say :)

    Likes: aimoi

    +1 -1 (+1 / -0 )Share on Facebook
  • @antix
    well, "hitTestPoint()" worked, but not so satisfying.
    Next time I'll give a try to what you said. seems like it is a better approach.
    (:
  • @aimoi - Okay cool. I got a version up and running which divides a circle into ring from the center outwards and then splits each ring into sectors, each of which can be solid or non solid.

    I have collisions registering with pixel precision but resolution is a bugger so I'm trying to kludge Hardon Collider into my code in order to resolve that issue.

    Likes: aimoi

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.