How do I organize Z-Index on collision with 2 objects?

  • Hi lovely people! I'll do my best to describe the problem here. The problem seems simple but I've attached a GIF for better understanding anyway.

    Problem:

    I have a character sprite, and a family of objects called Layer_Objects. What I'm trying to do is give some depth to the game, by having the character move behind or in front of these objects. So, on collision with Layer objects, I find the Y index of the player, and the layer object collided with, and move the player in front or behind the layer object depending on if the player's y-index is higher or lower than that of the collided object. Following me?

    The function itself actually works, all seems fine and dandy - until two objects are collided with. As you can see in the GIF, one object is no problem, but the second layer object will always override the first, and puts both objects in front or behind of the player. I've been trying to fix this for literally days and am now having Z-Index nightmares. Please help!

    Here's how it looks in action, you can see the problem at the end of the GIF when he collides with the chairs:

    Here's the code I've used thus far (which seems to work the best - ignore the shadow condition):

    I also tried a for loop, but this gave the same results:

    Something to note: on the start of the layout I sort all the Z-Indexes of the Layer_Objects by their Y-index (so the higher they are on the layout, the further back in space they are). That's all kosher. No problems there. It just seems to be this multiple object selection that's the problem.

    I've toyed with the idea of creating an array which stores all the Y-values of all the Layer_Objects, and simply checks the player's Y constantly to determine if he's 'above' or 'below' an object, but this seems like CPU overkill for a simple effect, and what I suspect might be a simple solution.

    Any help would be much appreciated! Thank you in advance.

    Jimzip

  • Nobody? Is there something simple I'm missing about this? Any answer here would be really appreciated!

    Jimzip

  • How about you select everything (perhaps within a certain range of the player ?) and just step through each of these (including the player) and z order them ?

    I will have a quick test and see if it works...

  • Hey RamPackWobble,

    thanks kindly for making the examples! Unfortunately your solution won't work for this problem, as then the layer objects themselves are being shifted to the front/back of the layer. Rather, the player should be moving in between them.

    To see why your approach would be problematic, here's an example of a level:

    As you can see, there are a lot of overlapping objects, and having them shift up and down causes them to 'pop' behind and in front of each other (I tried this approach early on). So, the challenge is to get the player to move around them, and to have the layer objects stay in order in z-space. Sorry for not clarifying this in the original post (thought I had everything this time)!

    In any case, I assumed (as you did) that I could simply select all the objects in a range, and then move the player in front/behind them, but when there's two objects in the collision zone, there's some issue I just can't seem to resolve whereby construct picks the most recently 'hit' object and disregards the first, rather than applying the conditions to both (edit: it seems to happen if the original object is lower than the new one. If it's above, then things seem to work).

    Anyone else like to take a stab at this? It's trickier than it looks!

    If there was a way to directly edit the z-index of objects (rather than just 'move to top/bottom/another object') it would be very helpful, but I can't seem to find a way to do that.

    Many thanks in advance.

    Jimzip

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What would be the issue moving the layer-objects in front of the player?

    You are not using move to top or bottom so the z-order of the layer-objects should stay the same..

    The issue might be (in the for each at least) that you are using

    for overlapping : playerrange-object

    for comparing the y : playerpin-object

    for moving the player : player family

    If the playerrange object is part of the player family, I would suggest using the player family in the overlapping event so at least the object referenced in the condition is the same as the one in the action..

  • Here, I had an idea and tried it out. Turned out to work pretty good !

    https://dl.dropboxusercontent.com/u/700 ... ering.capx

    I basically systematically put the player behind all overlapping objects, and then find the first object in the z-order list that should be behind the player. Once found, move the player in front of it and then stop iterating the list.

  • Hey all, thank you so much for the help & sorry I didn't reply (was away the last couple of days).

    Littlestain, I thought that as well, but no matter how I did it the problem remained the same X/ . I had the objects I was referencing pinned to one another in any case, so they moved together. Thank you for taking the time though. Much appreciated.

    Magistross, you hit it! This works like a charm. I had the logic in my head but I couldn't figure out how to do it. This works very nicely. For a visual, I've attached the final block:

    Thanks a heap peeps! This gave me many nights of annoyance! Rock on!

    Jimzip

  • Jimzip

    You shouldn't need to use collision or overlap checks at all. Just throw all the objects you want sorted into a family and set it up like this:

    It'll just continually sort everything correctly based on object.Y.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)