How do I Properly Use a 3D Array

0 favourites
  • 7 posts
From the Asset Store
Template for a basketball game with a 3D aspect (illusion of 3D)
  • So, I have an array with a width of 5, a height of 5, and a depth of 2.

    And, I have a sprite with a width of 70 and a height of 100.

    For testing purposes, the sprite that I am using is intended to be a cube viewed from an orthogonal perspective.

    Next what I do, is best demonstrated if I first start with another image.

    On a triggered event I spawn a sprite using the "for each element" condition of the array.

    The sprite has 3 instance variables, "arrayX", "arrayY", and "arrayZ". I don't know what order the "for each element" conditions uses to loop through the array, but that isn't particularly important. The intention is to essentially have a sprite (only one sprite) linked to each position in the array.

    Each time an object is created, I set it's instance variable e.g. "arrayX" to the current position in each dimension using Array.CurX/Y/Z.

    Once this is done I then use these instance variables to offset the position of the sprite in order to create a grid layout. Importantly though, there is a third dimension, so I offset the Y more depending on the sprite's "arrayZ" value, in this case either 0 or 1 I believe.

    On layout start, I get this

    Which, so far, is good and is exactly what I expected.

    Next, I have a function that I thought would be rather simple. When the function "sortz" is called, a "for each" loop is run, ordered by the sprite's "arrayZ" variable, the sprite is moved to the top of the layer. This should first run through all the sprites with an "arrayZ" value of 0, move them to the top of the layer, then move onto the sprites with an "arrayZ" value of 1 and move them to the top.

    Since no sprite with the same "arrayZ" value should share the same X and Y position on the array, it should create the effect of a stack of blocks.

    What I get is this

    What I expected to get is this

    I'm not really sure where to go from here. I had thought that if I had a sprite corresponding to each position in the array that the rest would be simple. I've been through the manual section regarding arrays and checked the forums for details on using 3D arrays.

    Also, using a "for each" loop, I've counted and there are 25 sprites with an "arrayZ" value of 0 and 25 with a value of 1. So I have the correct number of sprites in each layer.

    Note: I add "128" to each sprite's X and Y so that the group will be moved down and away from the top left corner of the viewport. This allows me to see all the sprites. The results are the same with or without this so I don't believe it is related to my problem.

    If necessary I can clarify further and even upload an example capx, but perhaps the solutions is something simple that I am overlooking.

  • Imagine you have a house, this is your array. In your house there are rooms where you could store things. Rooms are in this case your X -Axis. Of course you could throw all your clothes on the floor of your room, but some of us decide to sort them. Thats why you need a wandrobe in your room. This is your Y-Axis. Now some kind of people (expecially women) are still not happy with just throwing all their clothes in the wandrobe. That's why they use drawers to seperate the underwear from the socks and so on. The drawers represent the Z-Axis.

    House -> Array

    Room -> X

    Wandrobe -> Y

    Drawers -> Z

    I don't really understand what you're trying to do. But I can tell you what you're doing right now: You have a lot of underwear and to alocate them you write down in which room, which wandrobe and which drawer your underwear is right now. Afterwards you try to sort your underwear by the kind of drawer they're in. I think what you are really looking for is to sort them by the wandrobe they are in.

    (it's also kinda strange that you say you have 6 rooms and with 6 wandrobes each but there's just a 5 * 6 matrix.

  • I appreciate the reply, I'll spend some time reading what you have said to see if I can apply it to what I am doing.

    Simply put, the eventual goal was to use a 3D array or arrays to help me generate and keep track of terrain.

    Construct 2 is a 2D engine, but in my little game experiments I usually try to inject some element of depth to the game environment. This is often done with the use of layers and different parallax values; but there are other ways as well. Some have attempted to make use of an Isometric perspective, "top-down", or orthogonal.

    I admit that there is some confusion of the terms, but my understanding is that the orthogonal perspective differs from purely top-down perspective games in that it attempts to simulate an angled camera such as 45 degrees.

    What I'm trying to do here (at least I think) is to use an orthogonal perspective. This can be done without the use of an array object. It differs little from the creation of a normal tile-based top-down or isometric game. But I wanted to add an extra dimension if possible. And one idea that came to mind to simplify the process of assigning an X, Y, and Z dimensions to a sprite and managing those, was to use an array/s.

    When using an Isometric or Orthogonal perspective in a game, one of the basic functions of such an engine is the depth sorting of the various 2D elements, e.g. when a player sprite moves behind or in front of another object, the various sprites involved need to be layered accordingly. Normally, when using an orthogonal perspective, this is simple to do (though a bit more complex to make efficient). In my case, I'm just trying to add another somewhat imaginary dimension.

    I'm very much stumbling through this as that is my normal process with such experiments so it's possibly I've overlooked something very simple related to my problem.

    I'll try to add more information soon to better explain my problem and what I am trying to achieve.

  • I'm going to bump this thread; I'm hoping that perhaps someone might see my problem.

    I'll also take the opportunity to correct a small error I was making (though sadly it doesn't solve my main problem).

    In the sixth action of the first event, I'm offsetting the Y position of the sprite extra, based on it's "arrayZ" value. However, I'm offsetting the sprites with a higher "arrayZ" value in the wrong direction. Sprites with an "arrayZ" value of 1 should appear to be higher than those with a value of 0.

    So instead of

    self.arrayY*(self.Height*0.50)+(self.arrayZ*(self.Height*0.5))+128[/code:2jywncrp]
    
    It should be 
    
    [code:2jywncrp]self.arrayY*(self.Height*0.50)-(self.arrayZ*(self.Height*0.5))+128[/code:2jywncrp]
    
    Also, for those (I hope) looking at my first post, I'll try to state somewhat more simply what I'm trying to do.
    
    In my example, I use an array to assign a unique X, Y, and Z to 50 sprites. I then use these values to give each sprite a coordinate (an X and Y position in the layout) that should simulate the effect of a 5x5x2 stack of blocks. I've checked, and I'm fairly sure that I've been mostly successful. No sprite shares all three values of their "arrayX/Y/Z" variables with another sprite and all positions in the array are filled and correspond to one sprite and only one.
    
    However, since my first post, I have discovered that though they don't share this, some of them do share X and Y coordinates in the layout when they should not, as they also share the same "arrayZ" value.
    
    I don't know why this is, but it's likely connected to why when I try to use a "for each" condition ordered by the sprite's "arrayZ" variable, that I get a jumbled mess instead of an ordered and properly depth-sorted stack.
  • I could help you with isometric perspective but I never worked with orthogonal.

    Do you already work with debugger layout? It will show you all properties of the existing objects. Maybe this will help you.

  • well, I recreated your project... and I could not get it to fail. It worked just the way you wanted... and I tried several variations.

    I added Drag and Drop to the blocks so I could move them around to see how they were stacked.

    I also added an animation - grey for the bottom layer, white for the top, to help see which blocks are which.

    And I added a text box at the top of the screen to display the block coordinates when you click on it or move it.

    My first thought when I started was that your sort routine was not working the way you expected. You sort on arrayZ, but the 25 blocks that make up the upper layer could display incorrectly (like in your picture) if you don't also sort them by arrayY - because the blocks with a lower arrayY need to be behind the ones with a higher arrayY. But, like I said, it worked fine for me when I tried it.

    I am using the latest version of C2, so it could be an issue with an older version - where the ambiguity of just sorting 25 blocks with the same arrayZ gives a random order or something like that...

    anyway, try my version and see what happens. you can get it at:

    http://www.rieperts.com/games/forum/Blocks.capx

    In my version, if you press A it creates another set of blocks using loops rather than the array. But that way also worked as expected.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I used your method in my project. It seems... to work. I see now why my method wouldn't have produced quite the desired results. Oddly, I'm still not sure why I was getting the result that I was, i.e. the jumble of blocks.

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