sorting attributes of an object
Author |
Message |
shaon
|
Posted: Fri Apr 11, 2008 8:34 pm Post subject: sorting attributes of an object |
|
|
When working in 3D, objects in the far back are drawn first with the closest object to camera being the last object to be drawn. I stored the center coordinate of any object in space as it's attribute (ie. self.position = [x,y,z]). I then made an enviroment class that stores(in a list) and transforms this point based on user control. The problem that I havent figured out how to overcome is how to efficiently sort all the objects in 3D space that are held in a list, so that the object with the highest "z" point is first on the list and lowest "z" point object is last on the list.
I guess my question is, how do I sort objects in a list based on one of their attributes? I tried to make a modified merge sort, but sorting 1000 integers sucks up 16-17 ms and I can only afford 40 ms total for my game (25 fps).
In fact is there a better way to draw objects in a 3D plane correctly rather than sorting? I feel sorting takes too much processing time..... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Fri Apr 11, 2008 9:39 pm Post subject: Re: sorting attributes of an object |
|
|
You can and it's fairly easy. Read this.
In particular, pay attention to this snippet:
Python: | >>> a = [Spam(1, 4), Spam(9, 3), Spam(4, 6)]
>>> a.sort(key=lambda obj:obj.eggs())
>>> for spam in a:
>>> print spam.eggs(), str(spam)
3 12
4 5
6 10 | You can also write a key function to use instead of using a lambda and assign it to key, it's your call. Obviously obj is your object and eggs is a function of some sort. |
|
|
|
|
|
shaon
|
Posted: Fri Apr 11, 2008 10:24 pm Post subject: Re: sorting attributes of an object |
|
|
thx, I tried to search for the sort techniques in the manual and I couldn't find anything. Thank you oh so very much!!!! One step closer to a full-fledged 3D world.... |
|
|
|
|
|
|
|