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

Get help using Construct 2

Post » Sun Sep 13, 2015 8:20 pm

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:
Image

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

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

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 :D
B
6
S
1
Posts: 28
Reputation: 465

Post » Sat Sep 19, 2015 8:18 am

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

Jimzip :D
B
6
S
1
Posts: 28
Reputation: 465

Post » Sat Sep 19, 2015 2:08 pm

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...
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sat Sep 19, 2015 2:29 pm

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Wed Sep 23, 2015 1:01 pm

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:

Image

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 :D
B
6
S
1
Posts: 28
Reputation: 465

Post » Wed Sep 23, 2015 1:30 pm

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..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed Sep 23, 2015 3:15 pm

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Thu Oct 08, 2015 10:50 am

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:

Image

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

Jimzip :D
B
6
S
1
Posts: 28
Reputation: 465

Post » Thu Oct 08, 2015 5:19 pm

@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:
Image
It'll just continually sort everything correctly based on object.Y.
B
35
S
9
G
4
Posts: 201
Reputation: 5,172


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 64 guests