sorting problem

Talk about computers, hardware, applications, and consumer electronics.
Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.07.13 (05:07)

(this is for a 2D overhead game)

so I have a point - say, a camera - and I have many other points (enemies, geometry objects, whatever). imagine there's a ray coming out of the camera which slowly turns, I want to sort the points based on the order the ray would pass them.

I don't know anything about computerized sorting. Something I can initially think of is to convert all the points to angles from the camera, and order them with flash's built in array sorting function, but then I can't think of any good way to transfer this sorted order to the original list of points. Plus, converting points to angles requires trig, and i'd prefer to be able to do this with lots of points so I'd want it reasonably fast.

Any ideas? Am I being clear enough?

User avatar
Bayking
Posts: 315
Joined: 2008.10.01 (20:26)
NUMA Profile: http://nmaps.net/user/exuberance
Location: Guelph, Ontario, Canada

Postby Exüberance » 2009.07.13 (18:53)

What, exactely, do you need this function to do? Is it for a radar or something? There may be a better way to accomplish whatever you're trying to do without using arrays.

As for getting teh sorted order to the original list of points, you could change your object for points to also include a 3rd variable: the angle it makes with the camera. Then sort that array of objects directly. Here's code for a function I wrote a while ago in Actionscript 2.0 for sorting a list of objects in ascending order based on 1 property of the objects:

NOTE: This is Actionscript 2.0. If you need Actionscript 3.0, you might have to adjust a few things. I know you could change the Number type vars to int to save on memory and :Void becomes :void in AS3. Beyond that, I'm not sure what you need to change. My trial version for the new Flash ran out :(

Code: Select all

/*
	 * call this function with 2 arguments, array and varname
	 * Args:
	   * array:Array an array of objects with a common variable that you wish to search
	   * varname:String the name of the variable you want the objects to be sorted by. This is a string
	 * Returns: void
	 * Side effects: the array has been sorted by the indicated variable
	 * example: A is an array of objects containing 2 variables _x and _y. To sort the objects in ascending order of _x value use
	 *  quickSort_objectArray(A,"_x");
	 */
	function quickSort_objectArray(array:Array, varname:String, low:Number, high:Number):Void {
		var bottom:Number;
		var top:Number;
		var pivot;
		var temp:Object;
		
		if (arguments.length <= 2) {
			low = 0;
			high = array.length - 1;
		}
		
		if (high > low) {
			pivot = array[high][varname];
			bottom = low - 1;
			top = high;
			while (true) {
				while ((array[++bottom][varname] < pivot) && (bottom < array.length));
				while ((array[--top][varname] > pivot) && (top > 0));
				if (bottom >= top) {
					break;
				}
				temp = array[bottom];
				array[bottom] = array[top];
				array[top] = temp;
			}
			temp = array[bottom];
			array[bottom] = array[high];
			array[high] = temp;
			
			quickSort_objectArray(array, varname, low, bottom - 1);
			quickSort_objectArray(array, varname, bottom + 1, high);
		}
	}
ExüberNewsFeed: Exuberance is mostly <AFF> (Away From Forums) for a while, though I may still participate in epic contests/threads. When I return, I shall bring several comic updates (enough to finish season 1) and hopefully 1 or 2 games- at least one of which is N-related
Comic Activity-O-Meter: (how often I'm updating my comic)
(Click here to see what each level and half-level means in terms of updates per time period)

NOTE: If I just add a bunch of comics in one day, but plan on going back to normal after that, I probably won't update the status.
+ Dead: Canceled. Done. Maybe you'll get a random comic like once a year, but it's pretty much done.
- Zombie (Dead/Comatose): The comic is probably done regular updates forever, but I'll probably still add something once in a blue moon. It's still POSSIBLE, that I'll raise the status up, but not very likely. Maybe I'll have a comicplosion for like a week, then go back to being dead
+ Comatose: Complete stand-by. No (or very few) updates for some amount of time, but the comic's far from being over
- <AFK> (Comatose/Loitering): Stand-by, but you might possibly count on a few updates once and a while. Again, this is temporary
+ Loitering: Like comatose, but for short amount of times.
- Turtling (Loitering/Semi-Active): Really slooooww updates
+ Semi-Active: One every 2 weeks...ish?
- Quasi-Active (Semi-Active/Active): Averaging about 2 comics every 3 weeks
+ Active: Loosely defined status, but about a weekly update
- Over-Active (Active/Power-leveling): About 2 comics a week
+ Power-leveling: About 3 comics a week. Possible a schedule, possibly not
- Über-Epic (Power-leveling/COMICPLOSION!!): In some cases, this may actually be mean updates more frequently than COMICPLOSION!!, but I'm defining this level as a non-organized comic rush, kind of like a few days after my comic started
+ COMICPLOSION!!: Daily updates for a minimum of 5 days (since the daily updates started. It remains at this status until the 5, 7, whatever days are done)

Image
"Science without religion is lame. Religion without science is blind." ~Albert Einstein
My N+ Vector Sprite Sheet ::: My Caption Contest ::: My Comic :::Puzzles of the Exuberant ::: DEFEND YOUR NINJA: THE FLASH GAME (Release Date TBA)
Image
Exüberance on WoW
Image
Maps in the Fernat Epic (so far): (meh, let's put this in a spoiler too. My sig's gettin too big. I'm such a packrat :p)

Nmaps.netNmaps.net


Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.07.14 (01:06)

It's pretty easy to port from AS2 to AS3, especially when you do everything to standard like you did.

Specifically, I want to do something like http://metanet.2.forumer.com/index.php?showtopic=20674
It's an idea I gave up on before, but now my physics engine is nicer so I can just cast rays to all my corner points in geometry and join them up to represent visually the vision cone. The issue is, I need to order the points before joining them up.

I looked at your code, it's not making any sense to me right now but it works, so I'll try and make sense of it later. Is this considered fast for a sorting algorithm?

Sorting like this is not ideal because I wanted to avoid trig, but I can't see any way that could feasibly not involve trig so i'll use this for now. Thanks heaps!

User avatar
Bayking
Posts: 315
Joined: 2008.10.01 (20:26)
NUMA Profile: http://nmaps.net/user/exuberance
Location: Guelph, Ontario, Canada

Postby Exüberance » 2009.07.16 (03:39)

This sorting algorithm is basically quicksort, which is the fastest algorithm for sorting randomly ordered data. It's usually used for very large sets of data because it's efficiency is very clear then. It may not be the best for sorting very small amount of mostly orded data, however. This is how it works: http://en.wikipedia.org/wiki/Quicksort (This will make the source code make a LOT more sense :D)

Would it be possible to construct your function such that it finds the corner points in order so you don't have to sort it? I don't think it would be, but it's hard to say.
ExüberNewsFeed: Exuberance is mostly <AFF> (Away From Forums) for a while, though I may still participate in epic contests/threads. When I return, I shall bring several comic updates (enough to finish season 1) and hopefully 1 or 2 games- at least one of which is N-related
Comic Activity-O-Meter: (how often I'm updating my comic)
(Click here to see what each level and half-level means in terms of updates per time period)

NOTE: If I just add a bunch of comics in one day, but plan on going back to normal after that, I probably won't update the status.
+ Dead: Canceled. Done. Maybe you'll get a random comic like once a year, but it's pretty much done.
- Zombie (Dead/Comatose): The comic is probably done regular updates forever, but I'll probably still add something once in a blue moon. It's still POSSIBLE, that I'll raise the status up, but not very likely. Maybe I'll have a comicplosion for like a week, then go back to being dead
+ Comatose: Complete stand-by. No (or very few) updates for some amount of time, but the comic's far from being over
- <AFK> (Comatose/Loitering): Stand-by, but you might possibly count on a few updates once and a while. Again, this is temporary
+ Loitering: Like comatose, but for short amount of times.
- Turtling (Loitering/Semi-Active): Really slooooww updates
+ Semi-Active: One every 2 weeks...ish?
- Quasi-Active (Semi-Active/Active): Averaging about 2 comics every 3 weeks
+ Active: Loosely defined status, but about a weekly update
- Over-Active (Active/Power-leveling): About 2 comics a week
+ Power-leveling: About 3 comics a week. Possible a schedule, possibly not
- Über-Epic (Power-leveling/COMICPLOSION!!): In some cases, this may actually be mean updates more frequently than COMICPLOSION!!, but I'm defining this level as a non-organized comic rush, kind of like a few days after my comic started
+ COMICPLOSION!!: Daily updates for a minimum of 5 days (since the daily updates started. It remains at this status until the 5, 7, whatever days are done)

Image
"Science without religion is lame. Religion without science is blind." ~Albert Einstein
My N+ Vector Sprite Sheet ::: My Caption Contest ::: My Comic :::Puzzles of the Exuberant ::: DEFEND YOUR NINJA: THE FLASH GAME (Release Date TBA)
Image
Exüberance on WoW
Image
Maps in the Fernat Epic (so far): (meh, let's put this in a spoiler too. My sig's gettin too big. I'm such a packrat :p)

Nmaps.netNmaps.net


Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.07.17 (02:14)

well, the list isn't entirely random. Each object will have several points to consider, and these will be added to the array in order as points are scanned through, so they will often already be in order or reverse order. Also, the order objects is scanned is arbitrarily the order they exist in the level data, and in general level designers will create objects in order because it's more convenient, so this order might also exist in the angular sorting. I don't think there's any reliable order to the points though.

I now understand the quicksort, thanks. I can understand that it's very close to the upper limit on speed for my fairly random data, and it doesn't create enough of an impact on the processing required each step for me to worry about it further.

User avatar
Global Mod
Global Mod
Posts: 1416
Joined: 2008.09.26 (05:35)
NUMA Profile: http://nmaps.net/user/scythe33
MBTI Type: ENTP
Location: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Postby scythe » 2009.07.22 (16:52)

You don't need to use triginometry; you can just exploit the fact that a > b iff tan(a) > tan(b), assuming a and b are in the same quadrant. Just divide the points into quadrants (of the Cartesian plane) and sort them according to y / x.

(technically, you're using trig here, but you're not calculating the angle, which saves a good bit of time)
As soon as we wish to be happier, we are no longer happy.

Ice Cold
Posts: 202
Joined: 2008.09.26 (11:49)
Location: Australia
Contact:

Postby mattk210 » 2009.07.23 (06:03)

thanks! that's really clever.
I thought I'd need to mess with divide-by-zero stuff, but flash conveniently has an "Infinity" value which is a pain everywhere else but actually useful here.


Who is online

Users browsing this forum: No registered users and 4 guests