[Gambas-user] gb3: sorting an array-like structure

Benoît Minisini gambas at ...1...
Tue May 10 03:59:24 CEST 2011


> On 05/05/2011 07:50 AM, Fabien Bodard wrote:
> > 2011/5/5 Kevin Fishburne<kevinfishburne at ...1887...>:
> >> On 05/05/2011 06:31 AM, Fabien Bodard wrote:
> >>> We use that to sort icons in the iconview component
> >>> 
> >>> take a look in the source
> >>> 
> >>> trunk/comp/gb.form
> >>> 
> >>> IconView.class
> > 
> > it depend what you want to  sort !!!
> > 
> > say us please
> > 
> > and more simply
> 
> Alright. Basically I have an array of structures. Each structure
> represents an object in the game world. Each frame/loop I examine all
> the objects' coordinates to determine which ones are inside the
> rendering area. Finally the visible objects' positions are rotated to
> match the orientation of the player and rendered to the screen.
> 
> I need to maintain an index of the objects' rotated y coordinates and
> sort it. It will only be sorted when the player changes orientation (or
> if an object is moved by another player or physics). I need to know the
> order that each object should be rendered in so that they overlap
> properly. Rendering them in order by their post-rotated y coordinate
> should accomplish this.
> 
> Right now objects are drawn in the order they appear in the array of
> object structures (random relative to their world coordinates). It needs
> to change dynamically based on player orientation and each object's
> coordinates. You can see the results of unsorted object rendering in
> this video:
> 
> http://www.youtube.com/watch?v=HpvIcGYHVLY
> 
> Sometimes rocks are behind the trees, sometimes they are in front. It's
> a mess.
> 
> The player and other players' positions will also have to be added into
> this array of object numbers and y coordinates. I'd prefer to maintain a
> separate array, which will hopefully be faster than sorting the entire
> array of object structures. The hypothetical array to be sorted would
> look something like this:
> 
> Public RenderOrder[32767, 2] As Single ' [ObjectsTotal, 0 =
> ObjectNumber, 1 = ObjectY]
> 
> The array would be sorted by ObjectY, or RenderOrder[x, 1]. An example
> of before/after values would look like this:
> 
> Unsorted/Before:
> RenderOrder[0, 0] = 10   ' Object 10
> RenderOrder[0, 1] = 6000 ' Object 10 Y coordinate
> RenderOrder[1, 0] = 20   ' Object 20
> RenderOrder[1, 1] = 2000 ' Object 20 Ycoordinate
> 
> Sorted/After:
> RenderOrder[0, 0] = 20   ' Object 20
> RenderOrder[0, 1] = 2000 ' Object 20 Y coordinate
> RenderOrder[1, 0] = 10   ' Object 10
> RenderOrder[1, 1] = 6000 ' Object 10 Ycoordinate
> 
> Think of it like a spreadsheet instead of an array. I need each "row"
> sorted by the value in a "column". The two column values are
> ObjectNumber and ObjectY and the row represents the array index.
> 
> There's got to be something like this in GAMBAS, but if not I can start
> Google searching sort algorithms and make a procedure.

- Put the objects you have to draw on the screen in a temporary array.
- Define the (X,Y) final coordinates in two object variables.
- Implement the "_sort" special method in the object class.

That way, you just have to call "Sort" to sort only the objects you want the 
way you want.

Regards,

-- 
Benoît Minisini




More information about the User mailing list