chriswa wrote:
hippocoder wrote:
if (shape1.collided(shape2,optional index)... directly check a collision between two
if (shape1.collidedCategory(0x0002,optional index)... return true if collided with this category.
if (shape1.collidedCategory(0x0002,optional index)... return true if collided with this category.
It would be trivial to write a ContactListener to store this information, but a waste of performance and memory if we forced everyone to store it. If this is such a common API request, I could write one up for the Wiki. I don't think it's a good idea to force everyone to store all that data, as you can achieve greater efficiency by reacting to collisions as they are detected.
I agree that it's not a big deal to write up a contact listener to do this, and it can be more efficient; however, the lack of a single built in call to do this type of check is probably the most common complaint I've heard about the engine, so I'm not sure that I think it's enough to just have example code somewhere online to do it.
I'd be fine with leaving it at shape.isColliding instead of shape.collided, the difference being that the former doesn't store mid-step temporary contacts - for impact listening, you should almost always use a listener. But listeners are really overkill for checking persistent contacts, even if it's just cut-and-paste code.
BorisTheBrave wrote:
2)
Even in that case, such functions are really just convenience wrappers for the contact list, which must be linearly iterated over. Currently Box2D doesn't store any data structures to quickly access this, and it could be difficult to do so efficiently. I'm not sure what sort of index you had in mind, but I think that sort of thing is ugly, people should not have to do manual caching. On the other hand, it is rare to have many contacts on a single shape, so perhaps linear iteration is fine as is.
Even in that case, such functions are really just convenience wrappers for the contact list, which must be linearly iterated over. Currently Box2D doesn't store any data structures to quickly access this, and it could be difficult to do so efficiently. I'm not sure what sort of index you had in mind, but I think that sort of thing is ugly, people should not have to do manual caching. On the other hand, it is rare to have many contacts on a single shape, so perhaps linear iteration is fine as is.
I think a linear search is good enough for most people, as long as there is a note in the function documentation that the check runs in O(N) time in the number of contacts on that shape (and if it's not good enough, people can write listeners and store stuff away themselves, because they're doing something hard core with the engine and better know what they're doing anyways).
Internally we could check pairwise shape contacts a little "smarter" by iterating over the contact list on the shape with fewer contacts (the intuition being that usually if a shape has a lot of contacts, it's a large ground shape with lots of small stuff sitting on it, so it's quicker to check the small bodies lists instead of the big one).
Quote:
3)
Exposing the contact list. Having a binary test function is one thing, but I think it is equally important to be able to loop over all currently touching shapes. I can see a few ways of doing this:
a) Expose the underlying Box2D contact list. This is how it used to be, with GetContactList / m_contactList. This is dandy, but it kept tripping people up that it had speculative contacts in, and had this manifold/contact point structure that most people didn't want or understand. Perhaps better documentation would help, but is always going to have some usability issues.
Exposing the contact list. Having a binary test function is one thing, but I think it is equally important to be able to loop over all currently touching shapes. I can see a few ways of doing this:
a) Expose the underlying Box2D contact list. This is how it used to be, with GetContactList / m_contactList. This is dandy, but it kept tripping people up that it had speculative contacts in, and had this manifold/contact point structure that most people didn't want or understand. Perhaps better documentation would help, but is always going to have some usability issues.
Agreed, I remember having a rough time dealing with that stuff myself.
Quote:
b)Expose an iterator to the list. This can eliminate speculative contacts, and provide some help in browsing the list. The down side is that Box2D makes do without explicit iterator objects at the moment, preferring linked lists, so it'd be a bit of a depature.
c) Do a), but provide a GetNext() method that skips speculative contacts.
c) Do a), but provide a GetNext() method that skips speculative contacts.
I think I'd go for c) as opposed to b), mainly for the reason you gave, that Box2d doesn't use iterators at all. It would be a bit weird, IMO, to shift for this one purpose when people are used to walking linked lists for everything else.
Quote:
d)A function, or family of functions, to fill an array with info. I don't like this, but I don't like providing just a few options in any list of things 😉.
In Java or AS3, creating and returning an array would probably be the cleanest and most user-friendly solution, but in C++ not so much.
In other words, I think I'm liking option c) best at the moment.