HOME | DD

Published: 2008-09-26 13:39:54 +0000 UTC; Views: 3811; Favourites: 15; Downloads: 93
Redirect to original
Description
So this was initiated by who recently asked me about algorithm I would recommend for collision detection broad phase and in particular mentioned sweep+prune [link]This initiated series of tests and thinking on my part as I found this algorithm to be interesting. So result was that I tested different algorithms of sorting/finding/inserting items in arrays in flash [link] and came to a conclusion that Array.sort and Array.indexOf work so fast that probably should win over even faster algorithms of collision detection if I manage to make algorithm based on those core procedures.
Also I digged out my previous algorithm which turned out to be something like QuadTree algorithm(found out that recently
Usage and performance information/comparison
Usage. Well there is one bull under your cursor which you control and you can input ball count in box in Left-Bottom corner.
MoveAndRemove - step at which algorithm runs trough all balls and moves them and check if it is needed to remove those which flew beyond the screen.
SortAxis - there are separate array for each axis in which balls are contained. On this step those arrays are resorted.
checkSortedForColission - on this step sorted axis arrays are checked for object bondings intersections in pretty fast way. From what I can say a very fast way. Problem is then to find same pairs that intersected on both axis X and Y. And this so far is biggest slow down in this step. Need to think some more on it.
finalPairCheckAndResolve - on this step pairs that intersected on both axis are checked for collision more precisely and if determined to collide then collision is resolved.
Memory: Determines how much memory program currently uses.
Update
After some thinking and performance testings made changes to the algorithm. In short I changed how checkSortedForColission works and combined second part of checkSortedForColission with finalPairCheckAndResolve. As a result I got some 50% of performance boost
And extended explanation. Well Sweep+Prune sorts objects in separate arrays according to one of the axis of space. In those arrays those those objects are represented as one dimensional intervals which are easy to check for intersection. So in the end we have groups of intersections on each axis. Then we need to combine our results so that object pairs that intersect on all axis are pushed further to more precise/resource intensive checking and others are filtered out. Most resource taking part here is finding pairs that are contained in all axis arrays. Well before I was using Object for this. Turned out that I can use integers instead of strings as keys which makes it much faster but in the end made an algorithm based on Array and custom hashing function. So now bottleneck is sorting which is ridiculously fast
Also before I was creating final pair list and looping trough it in separate loop but now understood that that was not necessary and merged it with last axis loop.
Related content
Comments: 37
axcho [2008-11-16 07:03:58 +0000 UTC]
Wow, thanks for the breakdown on sweep and prune! I'll be interested in comparing when I get around to writing my own implementation of it.
👍: 0 ⏩: 1
wonderwhy-ER In reply to axcho [2008-11-16 10:34:30 +0000 UTC]
Welcome Was interesting to do it anyways. Seems it is wining because of possibility to use Array.sort
👍: 0 ⏩: 0
rocketship123 [2008-10-03 01:37:55 +0000 UTC]
coordinate rotation??
Thats fun stuff!!
Nice job!!
👍: 0 ⏩: 1
wonderwhy-ER In reply to rocketship123 [2008-10-03 06:45:07 +0000 UTC]
Nut shore what you mean by coordinate rotation...
👍: 0 ⏩: 1
rocketship123 In reply to wonderwhy-ER [2008-10-04 02:00:53 +0000 UTC]
its rotating the entire stage, invisibly.. like instead of bouncing angles at angles, you rotate everything bounce it, rotate it back and continue
👍: 0 ⏩: 1
wonderwhy-ER In reply to rocketship123 [2008-10-04 08:16:46 +0000 UTC]
Ouh no nothing like that
👍: 0 ⏩: 1
rocketship123 In reply to wonderwhy-ER [2008-10-04 16:49:15 +0000 UTC]
figured youd have a better way
👍: 0 ⏩: 1
wonderwhy-ER In reply to rocketship123 [2008-10-04 17:02:35 +0000 UTC]
Well what you described seems too processor intensive... I just used projection of force vector and calculated energy transfer according to it. Calculation intensive thing actuality and sometimes there are few ways to do it faster but most expendable. Direction, forces involved etc etc may be retrieved if needed and used to parametrize effects or different events like "this punch was too weak"
👍: 0 ⏩: 1
rocketship123 In reply to wonderwhy-ER [2008-10-05 01:11:17 +0000 UTC]
oh.. that makes sense...
when I first heard about coodinate rotation, i though its would be way to processor intensive... but when i tried it, it was the opposite.. when I post my new particle system, i have it in there...
👍: 0 ⏩: 1
wonderwhy-ER In reply to rocketship123 [2008-10-05 09:10:18 +0000 UTC]
Well thing is that you don't really need to rotate one direction make calculation and rotate beck for pair that collided. It is enough to leave coordinates of balls intact but trough projections get needed information about collision. Though it will be interesting to see your experiment
👍: 0 ⏩: 1
rocketship123 In reply to wonderwhy-ER [2008-10-05 18:43:42 +0000 UTC]
ill find it and upload it today
👍: 0 ⏩: 0
Niallmeister [2008-09-29 12:01:08 +0000 UTC]
Yaayyy, you fixed the collisions problem. All runs smooth too, nice one ;D
👍: 0 ⏩: 1
wonderwhy-ER In reply to Niallmeister [2008-09-29 12:01:36 +0000 UTC]
I think I will make stylized version later.
👍: 0 ⏩: 0
Supa-Monky [2008-09-29 04:35:35 +0000 UTC]
1000 balls and still running at a decent fps, nice one matey
👍: 0 ⏩: 1
wonderwhy-ER In reply to Supa-Monky [2008-09-29 08:29:13 +0000 UTC]
Hah yeah on my home PC with two CPU's it runs smooth on 1000 too But testing on older notebook as majority of people online still have something closer to one 2Ghz I presume
Though slowly they get some cheap version of Dual core 1.6 Ghz or something like that
👍: 0 ⏩: 0
SPHEAR-Designs [2008-09-26 18:37:23 +0000 UTC]
I don't know anything about Flash, much less about programming...
I'm having a lot of fun with this though!
Do the circles loop Asteroids style, where the circle carries velocity from one side to the other?
I see random circles come flying out of corners every few seconds...
👍: 0 ⏩: 1
wonderwhy-ER In reply to SPHEAR-Designs [2008-09-26 19:22:18 +0000 UTC]
Ouh I was removing balls when they fly out and creating new ones when they fly in. It is for performance testing of add/remove too not only collisions
👍: 0 ⏩: 1
SPHEAR-Designs In reply to wonderwhy-ER [2008-09-26 19:26:45 +0000 UTC]
Works for me!
*Has fun with collisions & add/remove*
👍: 0 ⏩: 1
wonderwhy-ER In reply to ssjskipp [2008-09-26 17:49:38 +0000 UTC]
There is System.totalMemory or something like this. Search flash help. I knew it was possible and found in help trough with "memory" request
👍: 0 ⏩: 1
ohnno In reply to wonderwhy-ER [2008-09-29 16:57:34 +0000 UTC]
a cool i was looking for that as wel
👍: 0 ⏩: 0
Niallmeister [2008-09-26 15:04:23 +0000 UTC]
Hmm, it's pretty buggy compared to v2, occasionally the ball's would go into the mouse and then suddenly shake and shoot out. Other than that it's pretty awesome (:
👍: 0 ⏩: 1
wonderwhy-ER In reply to Niallmeister [2008-09-26 17:52:42 +0000 UTC]
Yeah saw that. I actually know why it happens but code responsible is also code that is bottleneck of the algorithm. So it will be changed in next version anyways. I will post journal on those compartments latter.
👍: 0 ⏩: 0
Donitz [2008-09-26 14:50:26 +0000 UTC]
Nice. I always used a simple loop for each of the elements that should collide and compared the distances using some simple Pythagorean theorem when working with circular collisions, but it was definitely very slow.
👍: 0 ⏩: 1
wonderwhy-ER In reply to Donitz [2008-09-26 17:57:21 +0000 UTC]
Well that's the idea. Use some less resource eating thing that can determine if precise collision does not happen but can't with 100% possibility if it happens. So such algorithms are made that are able to use some advanced things like quicksort and use some approximation to divide objects in to groups saying that no collisions happen between groups but some are possible in the groups. Afterwards you do checks like you described are used to determine if any collision happen in the groups. So those two v2 and v3 are algorithms from this family. They are called broad phase algorithms which quickly filter out pairs of impossible collisions using different ideas like space sorting or some other more harder things.
👍: 0 ⏩: 1
Donitz In reply to wonderwhy-ER [2008-09-27 06:38:40 +0000 UTC]
I have tried using something similar in my "unit engine". A project I made that has it's own scripting language, just haven't made any games with it yet.
To determine collisions in it I added the edges of each unit, which are rectangular, to two separate arrays. So the arrays kinda looked like.
X1: U1 50 (left edge)
X2: U2 75 (left edge)
X3: U1 100 (right edge)
X4: U2 120 (right edge)
Then it was a simple matter of putting or removing unit couples into a seperate XCollision list when the their edges jumped over each other,
compare the XCollision and YCollision list and update a RealCollision one when the same two units were found in both.
👍: 0 ⏩: 1
wonderwhy-ER In reply to Donitz [2008-09-27 09:35:44 +0000 UTC]
Actually sounds similar to Sweep+Prune
👍: 0 ⏩: 1
Donitz In reply to wonderwhy-ER [2008-09-27 10:09:46 +0000 UTC]
Yeah might be.
The initial concept with the engine was to be able to have huge levels and that the engine would never have to loop through all units at once anywhere in the code. So there was no sorting involved, only immediate update and switching of the elements in the arrays as a unit moved.
👍: 0 ⏩: 1
machinemalfunction [2008-09-26 14:31:16 +0000 UTC]
Ah, now this is good. I've tried using Sweep and Prune style algorithms, and they have worked for me pretty well so far. I have to agree with you about the Array.sort + Array.indexOf methods
This seems to run extremely fast, but there are a lot of collisions being missed, even on slow ones. Why do you think this is happening?
👍: 0 ⏩: 1
wonderwhy-ER In reply to machinemalfunction [2008-09-26 17:59:17 +0000 UTC]
I have mistake in part after sorting step where sorted arrays are checked. But this part is also a bottleneck of algorithm and I am thinking on hwo to change it. Actually already got an idea. Will try it on weekends. Also will post a journal that will serve as log of improvements and performance comparison.
👍: 0 ⏩: 1
machinemalfunction In reply to wonderwhy-ER [2008-09-27 02:05:16 +0000 UTC]
Great. I'll be gripping my cock in anticipation.
👍: 0 ⏩: 0