So here it is, a code snippet for setting and checking if two pair of individual ID's have been set. We use a very similar method in our GameEngine to ensure that two of the same objects are not checked for collision more than once, unfortunately I can't provide the exact code we use in our engine however here is an alternative, Hope it helps. It is the Java implementation of the BitFlags found in RealTime Collision Detection Book by Christer Ericson (Excellent Book!).

Update 09/08/2012 - Got rid of long mask, cheers RoquenUpdated the Benchmark suite to include the (long) version and (int) version. Tried to make it as less biased as possible. The new results are very surprising.

publicBooleanBitFlag(finalintsize) {this.size = size;/* construct a bucket of bits depending on size. * Normally you would need a 2D array of "booleans" to check * for pair-pair collisions, this system uses less memory by using "bits" * as flags instead. By definition, it can squeeze 32 bits from a single * integer, allowing you to store 32 pair collisions per integer. */bits = newint[((size * (size - 1) / 2) + 31) / 32]; }

publicbooleansetPair(finalintflag1, finalintflag2) {/* * ensure that the flags are not the same, we don't check same object-object * collisions. (doesn't really make much sense to do so). */

if (flag1 != flag2) {intmin = flag1;intmax = flag2;

/* * Perform a min and max operation, swap the values if one is larger than the other * This way the result (5,6) shares the same bit index as (6,5) */if (flag2 < flag1) {min = flag2;max = flag1; }

And that's pretty much it, say you have two GameObjects with ID of 5 and 6, using this class you can check that a single operation is only done once on those pair of objects, namely collisions. If you have any improvements over the code, please post it for everyone to see! Good Luck and have fun =]

Look at the example: It sets a bit based on a pair of integers. Assume the set is empty and 'a' & 'b' are integers (within size), then set(a,b) sets a single bit for the pair. so check(a,b) and check(b,a) will return true, everything else false.

The example doesn't make it clear that it sets exactly one unique bit per pair. Still not entirely sure what the use of that is unless you can scan down the array like a bitmap index and construct pairs from it. I imagine the book has a more fleshed out use case?

I don't know why this is useful? Surely there are better ways to accomplish the scenario that's described. Blockmaps for dividing a world into smaller pieces of buckets holding instances to check for collision makes sense but when does this come into use and in what sort of logical architecture?

I'm still trying to figure out how this is very useful for collisions. Even if you had to check multiple sets of objects against each other, wouldn't you just do it by checking an array twice. Below is a very lightweight example if all the hitboxes were 1x1 pixel each.

Even if you had flags for objects, I an still trying to grasp how this method would be any quicker or easier than just a straight check. Maybe I just need more information, or maybe there is something I'm missing. I'm trying to stay optimistic here .

Sorry guys, I checked this thread last night and no replies, wake up this morning and there seems to be a whole lot of arguments going around! I've updated the above code with some comments, hopefully they clarify on how the code works. As for as where the code is useful, it all depends on your implementations of the "Game" or "GameEngine", for us, it was something that was extremly useful.

Consider a Scene of 100 Dynamic GameObjects, just moving around doing their own thing. Our engine at this point performs collision detection in steps.

First Step -> Insert or update the GameObjects into a BroadPhase Collision System, We use a Grid and a variant of QuadtreeSecond Step -> The BroadPhase does a sweep and build a list of potential "pair-pair" CollisionsThird Step -> The NarrowPhase detector takes this list, performs a bounding volume test as an early "out" reducing the size of the listFourth Step -> A more expensive Collision Test is performed on the rest of the list, which generates more accurate data needed for physics and suchFifth Step -> Resolution is performed on the data generated on the fourth step, when needed callbacks are performed, telling the "user" that something happened

The problem is during the 2nd step, where potential pair-pair collisions are generated, depending on your implementation of the Broadphase, The system may generate pairs which have already been generatedpreviously, especially if you have an implementation of a Grid for example, where a single object can overlap multiple grid cells.

So we use something similar to the above bitflags to check first to ensure such a pair has not been added to the list, and if it has, its simply ignored, saving precious frame time and simplifying the entire collision process.

OP: Showing up and posting code straight away = awesome. Minor notes on your code: since java doesn't have unsigned (and I don't know if static analysis will change it) I'd change the divides by power-of-two to shifts. Likewise mask doesn't need to be a long. Also it could be interesting at the end of your project to test a variant which is backed by longs instead of ints (assuming 64-bit VM).

WRT: Usage. Personally I don't THINK I'd go this route (don't take this to mean I think you should change something working...if it works, move on). Assuming uniform grid and 2D (to keep things simple), then grid-based sweeping handles objects which cross boundaries (as you note, the need is implementation specific...I'm just providing details for everybody). Potential collision from broadphase I'd place in a container (probably an ArrayList). Why? On building you're only performing writes on linear memory. Narrowphase digests this list (linear reads) and spews into a different (say ArrayList).

According to benchmark, the fastest method is a straight 1D Boolean Array while slowest method is BitSet. In higher runs of the benchmark using larger data types of 1000x1000 potential pairs, The difference in speed between BitFlag using Long and 1D Boolean Array is only approx 5-10% compared to memory usage of (approx) Long using 62440 bytes vs Boolean 499500 bytes. Assuming of course the VM gives 8 bits (1 byte) per boolean value.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org