Checking for collisions on multiple layers

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Does anyone know a clever way to check for collisions on multiple layers? For example I have my sprites on Layer 2 but my collision mask is on Layer 1 which is also invisible. This is mainly for projectiles so I want to avoid having multiple objects for collisions. The way I figured how to do it is that each object on Layer 2 has these four events:

    - Always
    Run a for each loop "collision"

    - On loop "collision"
    Move to Layer 1

    - On loop "collision"
    - Object is overlapping obstacle
    Destroy

    - On loop "collision"
    Move to Layer 2

    I have no idea how costly this would be performance-wise. What do you guys think?

    BTW - This is not intended to be a pixel-perfect detection system, it's mostly used for projectiles when they smash against walls and the player, etc.

    Please login to see this link. Please login to see this link.
    Freelance Dev | Currently Working on Jarvis | Please login to see this link.

  • Would have done the same :(

    Don't know how taxing "move to layer" is,
    but other solutions I can think of would require a couple extra actions x loop so a rough guess is that they would be anyway worse.

    Maybe a very little optimization could be:

    - Always
    Move to Layer 1
    Run a for each loop "collision"
    Move to Layer 2

    - On loop "collision"
    - Object is overlapping obstacle
    Destroy

    Don't know if/how faster that would be, maybe a little bit,
    but is shorter code-wise

    (not tested, but should work I guess?)

    Dumb question would be:
    why needing the collision mask to be on a different layer?
    But there's surely a good reason behind X)

    a selection of my Fusion examples can be found Please login to see this link.

  • Ah yeah good idea, that's less events to go through at least. Maybe I can run an isolated stress test and see if there is a big difference.

    Please login to see this link. Please login to see this link.
    Freelance Dev | Currently Working on Jarvis | Please login to see this link.

  • Yeah, I recall finding this didn't work, at times, not really sure why
    (I think after a foreach, scoping gets confused)
    (...this is why I put that "disclaimer" XD)

    so this is the inelegant workaround I usually do:

    Please login to see this attachment.

    But not really tested on performances.
    At first glance seems to be very similar...
    guess the nicest code for you would win

    a selection of my Fusion examples can be found Please login to see this link.

  • I was curious and tested both solutions with the good old speed tester
    there's a slightly better performance from the "optimized" solution,
    but I wouldn't say the gain is very important, in a general use-case.

    Please login to see this attachment.

    (change outloop: layer is changed outside the foreach loop,
    while the other inside)


    And the gap grows with growing number of objects tested:


    Please login to see this attachment.

    But seems like it's a pretty taxing operation to do anyway.
    Starting to think checking collision mask through coordinates with bounding boxes (since you don't need precision) could be lighter?
    Could try another little test out of curiosity..

    a selection of my Fusion examples can be found Please login to see this link.

  • And this is a slightly quicker method in fact (seems like move to layer is more intensive).

    Just placed a collision detector on the other layer,
    on loop you place the detector at sprite X,Y
    and test for backdrop collisions.

    Please login to see this attachment.

    It's more than twice as fast.

    But I guess you would have to store multiple animations for your different object types,
    and if you launch the loop on different objects at the same time
    don't know if you would have to switch the animation just once before the loop gets fired
    or do it multiple times inside the same loop (different objects, same qualifier..)

    Or you could go with a simple additional X and Y scaling of the detector
    (say you want to have a scalable bounding box based on Owidth/Oheight of the object to test)
    would still be faster than previous solution:
    Please login to see this attachment.


    (just a side note:
    bouncing works bad when detached from it's own object "on collision" event
    but I guess you won't really need to bounce in real-case,
    if you need to destroy it would be good imo)

    Sorry for flooding the thread, was interested in this as well XD

    a selection of my Fusion examples can be found Please login to see this link.

  • Thanks for doing help shrodinger! I might use a mixture of the methods actually. For some objects I want their animation and shape to be the collision mask while for some objects a square would do just fine so I think both approaches would work. :)

    Please login to see this link. Please login to see this link.
    Freelance Dev | Currently Working on Jarvis | Please login to see this link.

  • Hi guys,

    I normally go for the following approach:
    1. Beginn of frame loop (layering for collisions): Move ALL my actives that need collision detections to my collision layer
    2. Run the frame loop and check for collisions
    3. At the end of the frame loop (layering for renderer): Move my objects to the layers I want them to be displayed

    This way you detach the whole collision detection from the rendering, might not work for you in some cases though but I found it to be more simple and effective than most of the stuff I did before

    Please login to see this link.
    Please login to see this link. | Please login to see this link. | Please login to see this link. | Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!