schteppe/p2.js — pull request #41 by timrwood (2013-12-06 19:51:45+00:00)
Title: Narrowphase lookup table

I was going to submit a PR, but wanted to see if it was something you were interested in before doing so.

Right now, the narrowphase shape on shape collision handler lookups are a deeply nested `if/else` block.

[src/world/World.js#L524-L571](https://github.com/schteppe/p2.js/blob/d1deaa2682c32a3c7abb58fff76ba519a37a5bec/src/world/World.js#L524-L571)

Because each shape has a type bit, you could use that to store collision resolvers between shape types.

```js
Narrowphase.prototype[Shape.CIRCLE & Shape.LINE] = Narrowphase.prototype.circleLine;
Narrowphase.prototype[Shape.CIRCLE] = Narrowphase.prototype.circleCircle;
```

Then, when resolving the collision, lookup the correct resolver by shape type.

```js
var resolver = si.type & sj.type;
if (typeof np[resolver] === 'function') {
np[resolver](bi,si,xiw,aiw, bj,sj,xjw,ajw);
}
```

To ensure shapes are always passed in a predictable manner, you could change the order based on which shape type bit is lower.

```js
var resolver = si.type & sj.type;
if (typeof np[resolver] === 'function') {
if (si.type < sj.type) {
np[resolver](bi,si,xiw,aiw, bj,sj,xjw,ajw);
} else {
np[resolver](bj,sj,xjw,ajw, bi,si,xiw,aiw);
}
}
```

If you are interested, I can submit a PR.