Creating an adventure game step by step - part 5

I have some trouble with the ‘z-axis.’

I have used the editor to create an ImageTransform background.
I have translation Z set to -500 so it always is behind other transforms.

Then I added a Scene: part of the blue sky.
I set Z to -499 so it is placed before the background but still behind all other transforms.

But then the problems begin:
I added another Scene (group of trees and plants, see picture).

I set its Scene Z to -498.
It disappeared!
I had to set Z to a minimum of 1 to display it in front of the ImageTransform background.
I don’t get that because the first Scene (the sky) is displayed allright with -499.

Then I added the 2 palm trees to the left and the bushes in the sand.
I set their Z to 3,4 etcetera and the large bush on the foreground I set to 8.
They all are placed over the previous Scene and are visible so this made sense (though I still do not understand why I could make the first scene have Z -499 but the next ones had to have positive z values.

So this is my first question.

I want to place a character sprite behind the large bush.

I thought this would work if I make the Z of it: 7. (as the bush has Z: 8 in design)

In code:

Procedure TAvatar.Position;
 var V: TVector3;
    V := StatePlay.Location.NPC[2].Transform.Translation;
    V.Z := WestBeachScene8.Translation.Z - 1;
   Transform.Translation := V;

But the character is placed in front of the bush.


If I make V.Z := -490;
Then it is placed behind the bush.


Every value higher than -490 will place the character in front of the bush.
If Z value is equal or lower than -500 it disappears (that makes sense for me because ImageTransform has Z value -500.

So I don’t understand this translation.z “error”.
(see for the “order of stuff hierarchy” the editor pictures in my previous topic).

P.s: I tested V.Y := WestBeachScene8.Translation.Y and this places the character halfway height of the bush, so this works okay.

After changing Z, if you have some objects with blending (which includes images with alpha channel) be sure to sort them – see Blending (Rendering Partially-Transparent Objects) | Manual | Castle Game Engine . In the editor just select the viewport (or any object within) and use menu item “Viewport → Sort Items For Correct 2D Blending”.

I hope to remove this hindrance in the future, for now you just have to remember to use “Viewport → Sort Items For Correct 2D Blending” if you change Z order.

If this doesn’t help, please submit a testcase to reproduce – I’d like to fix it :slight_smile: It can be just a design with images (since the problem is reproducible in the editor already, we don’t need to see your code to debug it).

Hi Michalis,

It did not help.
I submitted a testcase in attachment.
See from line 93.

In the design everything is okay concerning Z order though I cannot use negative Z values for the scenes because they disappear (probably placed behind the ImageTransform).

The Z problem occurs when I want to place scenes that are created in code.
I would like to place them behind the designed scene when their Y value is larger than the Y value of the designed scene and in front of them when their Y value is smaller.
I tried the Viewport.Items.SortBackToFront2D in code but it made no difference.

Rescue_Island (15.6 MB)

Everything you designed in your world (background, bushes) is inside WestbeachTransform (which is a child of Viewport.Items), but you add Scene1 (with your character) directly to the Viewport.Items. This makes proper sorting for blending impossible – Scene1 is on a higher level than all the bushes and backgrouns, so it is always either completely in front or completely in the back.

This was easy to see by building in debug mode (just use “Run → Compile And Run” in CGE editor) and pressing F8 to invoke “Inspector”. (I know, Inspector is not a perfect tool – the Inspector UI covers a lot of parts, you have to play a bit to see in Inspector what you want to see; still it allows to nicely see what happens “at runtime” in the viewport. See Inspector invoked by F8 - new Castle Game Engine feature - YouTube . )

To fix, I removed


and added there


So you should add things to WestbeachTransform, and sort in WestbeachTransform, to have things correctly between various layers of WestbeachTransform. I understand that the resulting image is what desire:

And this is Inspector view of it:

Note that after my fix, the unnamed TCastleScene (this is the character you add from code) is between WestbeachScene6 and WestbeachScene8.

Hint: assign better names to your scenes, to make it easier to debug :slight_smile: Like BushLeft, BackgroundPlane etc. For things you place in editor, you can change the name in editor. For scenes you create from code, you can assign a name from code, like Scene.Name := 'MyCharacter'; (just make sure to keep all names unique).

Thanks for quick response.

I changed the 2 lines and then got this code

procedure TStatePlay.Start;
var T: TVector3;


    DesignUrl := 'castle-data:/gamestateplay.castle-user-interface';

    WestbeachTransform := DesignedComponent('WestbeachTransform') as TCastleImageTransform;
    WestbeachScene8 := DesignedComponent('WestbeachScene8') as TCastleScene;

    Viewport := DesignedComponent ('Viewport') as TCastleViewport;

    Scene1 := TCastleScene.Create(Application);

    Scene1.URL := 'castle-data:/characters/thari langdon/casual/tharistandidle.starling-xml';
    Scene1.Scale := Vector3(0.3, 0.3, 0.3);

    T := Scene1.Translation;
    T.X := 2400;
    T.Y := 450;
    if T.Y > WestbeachScene8.Translation.Y then T.Z := WestbeachScene8.Translation.Z - 1 else T.Z :=  WestbeachScene8.Translation.Z + 1; // should place the character scene behind the bushes (which has Z: = 10 in design)
    Scene1.Translation := T;



but then the bushes completely disappear?


And when I remove the line
if T.Y > WestbeachScene8.Translation.Y then T.Z := WestbeachScene8.Translation.Z - 1 else T.Z := WestbeachScene8.Translation.Z + 1;

I get this:


I have tried to add WestbeachScene8 to the ImageTransform in code but as it is already added in design I gues this is unneccessary. And it did not help either, it was not displayed when trying out the above code again.

Are you testing on exactly the same project that you send as a testcase? No additional modifications?

And do you use latest CGE? I recall we had a fix around a similar issue some time ago ( Increase a bit range for orthographic projection near/far, to be sure · castle-engine/[email protected] · GitHub - 24 days ago).

I have now exactly the same TStatePlay.Start as you show above (and the rest of code and data as you send in zip today), and the bush seems visible to me OK. I’m testing on Linux, but this shouldn’t matter in this case.

I just installed the latest CGE and now everything is displayed fine! :grinning:
Wow, this issue had me captured for nearly a week.
I had updated a Month ago so that was about a week before you fixed the issue you mentioned:

Maybe this was also the cause of the problem I mentioned and displayed earlier (disappearing characters while walking, in my previous topic).

1 Like