Sprites and Layers

Posted By: Yking

Sprites and Layers - 10/22/18 17:16

A while ago I asked for help on the best solution for a tile based 2D game in another thread here and I have been trying around many different things since then.

However, there is a basic problem that I need to solve aswell, something that I am not sure on how to approach.

I have two sprites that are created in a level.
At positions vector(-100, 0, 0) and vector(100, 0, 0)
Simply: One is left and one is right, the Z is the same

Is it possible to have one Sprite always on top, no matter what the Z is? Layers, for example would be cool, but I did not find anything that worked.
The moving tree here, should always stay in front of the other one.

(The different Y's of the trees are just for making it more clear)

If I would change the Z,
then moving the camera causes a kind of "parallax scrolling" which I don't want.

Is there any solution for this?
Either layering the sprites somehow or
getting rid of the "parallax scrolling" when Z is different?

Yours Yking

Quote:

My test code:

///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////
function main()
{

video_window(NULL,NULL,NULL,"TEST");
vec_set(sky_color,vector(0,0,0));
level_load(NULL);
camera.pan = 90;
camera.tilt = 90;
camera.x = 0;
camera.y = 0;
camera.z = -700;
camera.ambient = 100;

ENTITY* sprite_a = ent_create("data/textures/sprites/test.tga", vector(-100, 0, 0), NULL);
sprite_a.tilt = -90;
sprite_a.pan = -90;


ENTITY* sprite_b = ent_create("data/textures/sprites/test.tga", vector(100, 0, 0), NULL);
sprite_b.tilt = -90;
sprite_b.pan = -90;


while(1)
{
//Movement
if (key_w == 1) {while(key_w ==1){wait(time_step*300);sprite_b.y -= 5; }}
if (key_s == 1) {while(key_s ==1){wait(time_step*300);sprite_b.y += 5; }}
if (key_a == 1) {while(key_a ==1){wait(time_step*300);sprite_b.x -= 5; }}
if (key_d == 1) {while(key_d ==1){wait(time_step*300);sprite_b.x += 5; }}
wait(1);
}


}

Posted By: rayp

Re: Sprites and Layers - 10/22/18 17:38

Seting overlay flag or using a material might help.

When iam at home i can provide example material, but this is around here or on the wiki if u wanna search now

Greets
Posted By: txesmi

Re: Sprites and Layers - 10/22/18 18:27

Quote:
getting rid of the "parallax scrolling" when Z is different?


Maybe adding the 'isometric' camera flag so you can modify the sptites depth with no distortion of perpective. You may want also take a look to 'd3d_entsort'.
Posted By: Yking

Re: Sprites and Layers - 10/22/18 21:07

Thanks for the answers so far laugh

The thing that would be the best would probably be the Isometric camera, however i have tried that over and over and I cannot get it to work, I have set the flag but everything stays the same, am I missing a step?
(I tested it by changing the sprites Z and moving the camera, the flag doesn't make a difference for me)

I tried the "d3d_entsort" and none of the options seems to work either, setting it to 0 (not sorting at all) works in this case, but the moving sprites always go on top and that would mean that the player always walks "on" the trees and not under them.

I am not sure how materials work yet but I will research into it, too
Posted By: rayp

Re: Sprites and Layers - 10/22/18 22:12

If the Problem is transparent / sorting related this may help ( old example from wiki ), if not iam sry:
Code:
MATERIAL* mat_alphatest=
{
  effect =
  "
    technique alpha_test
    {
      pass p0
      {
        zWriteEnable = true;
        alphaTestEnable = true;
        alphaBlendEnable = false;
      }
    }
  ";
}

action apply_alphatest_material(){
  my.material = mat_alphatest;
}

i just converted from wiki to lite-c. but did not test.

Greets
Posted By: txesmi

Re: Sprites and Layers - 10/22/18 22:44

It is hard to know what are you exactly trying. Basic 2d engines use three layers: a background, a foreground and the rest in the middle. Things that can overlap and can be overlapped by a character are normally cut in two pieces, so the down part is drawn in the background and the upper part in the foreground, been an unwalkable tile the 'connection' between both.
Posted By: rayp

Re: Sprites and Layers - 10/22/18 23:41

I once faked sorting in a fun/test-project 2d sidescroller using the Y+Z axis of the sprites. Player can walk up and down + left and right. Up and Down is handled via Y pos (to sort sprites) + Z pos (visual movement) of sprites.
Maybe theres a smarter solution but for me it worked ok.
Posted By: Yking

Re: Sprites and Layers - 10/23/18 13:57

Thank you both again for your help,
I really, really appreciate it!

Originally Posted By: txesmi
It is hard to know what are you exactly trying.


I want to clarify it a bit better, it is hard for me to describe properly.
The Game is layered like this:


The Game is from a Top-Down Perspective
Left = -X
Right = +X
Up = -Y
Down = +Y
And Zooming out would then technically be -Z, zooming in +Z

The Background (Grey rectangle) is no problem, just a texture, and it could be exactly at vector(0,0,0)

The NPC-layer (orange rectangle) is where NPCs walk on. Basically, NPCs would need a smaller Z (perhaps vector(0,0,-10) so they don't clip into the background layer.
However, this
a) causes parallax scrolling when the camera moves, "floaty" layers
b) All NPCs on the NPC-layer all have the same Z, so they also clip into each other

Now, I understand that d3d_entsort could solve Problem b) by always making the one NPC go on top, who is closer to the camera on the Y-Axis but I can't wrap my head around the 3-dimensionals yet and changind d3d_entsort has not brought me any success.

This is way harder for me than I thought shocked
Posted By: Yking

Re: Sprites and Layers - 10/23/18 19:11

I got it to work, and I don't really know how
The Isometric flag suddenly started working and now I can use the sprites' Z-coordinates to layer them.
No more floatiness and no was d3d_entsort needed

If I figure out what I did differently, I will add a post!
Posted By: txesmi

Re: Sprites and Layers - 10/23/18 19:45

Congratulations! laugh

I would set d3d_entsort to 8 because is cheaper and fits your needs.
Posted By: Yking

Re: Sprites and Layers - 10/23/18 20:39

Oh my god, I understand this now.

I downloaded a 30-day-trial while at university, since I didn't have my original at hand, tried the damn testcode again and it worked.
Super happy, saving the files, going home aaaand... it doesn't work.

Looking at the website:
Isometric view needs Pro-Edition and I only have Extra.
I cannot use this feature frown
Posted By: txesmi

Re: Sprites and Layers - 10/25/18 10:49

I was a step away from explaining how to manage tiles with a shader. What a pity!
© 2024 lite-C Forums