Skip to main content
Home Forums Silverlight Programming Game Development Simple 2d layer drawing problem
6 replies. Latest Post by Grofit on November 17, 2009.
(0)
Grofit
Member
229 points
310 Posts
10-30-2009 4:34 AM |
Hey,
Similar to one of my old posts but this one is a bit simpler a problem... but i still cant think of a *decent* solution without having to do LOTS of checks and nasty things...
In the above example you can see there are 3 elements in the *imaginary* scene, 2 characters and 1 element in the level. You can for all intents and purposes think of the level item as a tree bark, so the blue player is standing in front of the tree, the red player however is standing behind the tree...
Now currently my level is split up into 3 layer, underfoot layer which contains all ground sprites. Player layer, which contains all players/characters and can also contain sprites that should react as shown above, then finally an overhead layer which is always displayed overhead...
Now in a similar game i did with XNA (although never finished) i did a simple y axis check every time i drew something to the screen, so each object that was within this *player* layer was assigned a layer based on its y value, so the blue player may be at 30,100 and the bark is at 30, 30 so it would give the bark a layer of like 3 or something like that, and the player would have a layer of like 10 which would display it over the static entity, however the red player would have a layer of about 2 or similar so he would be displayed behind the tree bark.
Thing is in XNA im pretty sure i could use decimals so there was no chance of me running out of layers, but im not sure if silverlight is as flexible in this department, and as it doesnt render things per frame, i would need to keep cycling through active elements in the scene updating their layer somehow, and im not sure how slow that would be....
Anyone had anything similar to this and solved it with a *nice* solution?
rabbott
258 points
37 Posts
10-30-2009 7:53 AM |
Hi Grofit,
I'm assuming you're using a canvas or derived control here for composition. The Canvas.ZIndex attached property is an int32, so you theoretically have a range of -2,147,483,648 to +2,147,483,647, which I should imagine is enough for you. The approach that springs to mind is to tie the ZIndex to the integer part of the Y axis location in all elements in your scene (background element included) - this would ensure the rendering works as intended. You could specify a ZIndex offset of, say, +10000 for your 'overhead' layer to ensure everything there is displayed in front.
Two approaches for updating the Canvas.Zindex property spring to mind; either update it while updating the y axis position, or if you're using animations attach an event handler to the Canvas.Top property's change event in each of your elements and update the ZIndex that way, however I'm afraid I can't make any performance claims.
Hope this helps!
Regards,Rob
10-30-2009 9:05 AM |
Oh awesome i thought that there were only Byte.MaxValue layers, if its an int then thats brilliant...
Thats the way i was previously doing it in my other game, i guess changing it on movement only wouldnt slow it down too much, thanks for the advice!
11-03-2009 1:55 PM |
You're more than welcome Grofit! I'd be interested to know what kind of performance you get out of your game with this approach. - Rob
11-13-2009 3:10 AM |
Just a quick update as it seems some info was wrong in this thread...
First of all the layers seem to cap at short.MaxValue-1, opposed to int.MaxValue-1, although it doesnt seem to complain if you go negative and add int.MinValue as the minimum index.
Ive currently changed my system so it will update the zindex when moving and i havent seen any performance differences, although i havent tested it with LOTS of entities yet.
11-13-2009 10:02 AM |
Sorry about that - looked up the docs, read it was in Int32, missed the bit about being capped to Int16.MaxValue – 1 (32766). Although it does say you can use negative values, the documentation doesn't explicitly specify what the largest negative value allowed is (other than the normal limits of Int32). Hopefully though it should still be a large enough range for your requirements?
11-17-2009 9:24 AM |
No worries, and it seems to allow int32.MinValue so yeah it should be more than enough...
At least you gave me a starting point to go from so thanks for your help!