Cool, yeah that's basically what I do now with the wrapper thing. I had originally just hacked a few variables into b2Shape and such, but realized that was going to make it difficult to update to new versions. Originally I was thinking with a diff program it wouldn't be too bad, but then I got worried about major Box2D changes somehow breaking my game engine because of the break-neck pace of development! I think using a wrapper is actually the recommended way to do it. For garbage collection, I would say just make a method for your game somewhere outside of the wrapper object (mine is ActorManager actually), that can call destroy on a wrapper. So it will get the wrappers shapes and call destroy from the Box2D engine for them, and then it will null the references to those wrappers.
So about getting the strength of the collision, I know basically where it is, but haven't used it for anything (a little like my toothbrush lately
😮 ). If you have the contact manifold (b2Manifold) from the collision, you do this:
Code:
var contactPoint:b2ContactPoint;
contactPoint = contactManifold.points[0] as b2ContactPoint;
Then you have the contact point which has all the juicy stuff inside of it (Erin: ever think of renaming b2Manifold to b2Coconut?
😉 hmm....didn't think so).
So I think that normalImpulse might be the strength of contact, but don't quote me on that. When I am ready to use it I will have to search in the forums, examine the code carefully, or just play around with it a bit. I do suspect that's the one though. Separation would tell you how far into the shape the other shape has penetrated, but that doesn't necessarily tell you much about the strength of contact. (same penetration could be slow-moving object and large time step or fast moving object and smaller time step).
My collision handling system has gotten a bit complicated lately, but here's a small class I built to store basically pairs of names, with an associated action (action is another of my classes). I should probably be using ints or something instead of strings, but my code is still evolving and it's most important right now that it work and be understandable
🙂 So here's where I store my "collision triggers":
Code:
public final class CollisionTriggerHash {
private var map:Dictionary;
public function CollisionTriggerHash()
{
map = new Dictionary(true);
}
public function addTrigger(group1:String, group2:String, action:ICollisionAction) : void
{
if (map[group1] == null) {
map[group1] = new Dictionary(true);
}
map[group1][group2] = action;
if (map[group2] == null) {
map[group2] = new Dictionary(true);
}
map[group2][group1] = action;
}
public function removeTrigger(group1:String, group2:String):void {
if (map[group1][group2] != null) {
delete map[group1][group2];
if (map[group1] == null) {
delete map[group1];
}
}
if (map[group2][group1] != null) {
delete map[group2][group1];
if (map[group2] == null) {
delete map[group2];
}
}
}
public function getTrigger(group1:String, group2:String):ICollisionAction {
if (map[group1] != null) {
if (map[group1][group2] != null) {
return map[group1][group2];
}
}
return null;
}
}
I have all kinds of different triggers for my game (collision, timer, constant, etc...), and they each have an associated action class. So I don't know if you are familiar with Interfaces in AS3, but they can be added to any class to say "This class has all the functions I listed in this interface". So "ICollisionAction" is the interface for actions of the collision type. So what I have defined right now for ICollisionAction is that they all have a "run()" function, that takes 2 b2Shapes and a b2Manifold, which is the contact manifold.
So somewhere I have a function in my game loop "CheckCollisions". It is doing something very similar to what yours is doing. I get the shapes involved in the contact, then I check their wrappers for their "group information". This is a custom group system so that they can belong to multiple groups, which can be categories or more specific objects. I know Box2D has groups, but that is just for whether shapes should collide, and this is for whether they should register a special event upon colliding. So once I have the two strings for the group an object belongs to, I call getTrigger("group1","group2"), and it returns an action reference if there is one for that pair of group types. I actually have to check all combinations between the two shapes if they have multiple groups they belong to, so until I program it a little more efficiently I just have to only have 1-3 groups or so for an object, which I think is sufficient for 90% of cases anyhow. So this is how that part works:
Code:
triggerAction = collisionTriggers.getTrigger("group1name","group2name");
if (triggerAction) {
triggerAction.run(shape1,shape2,contactManifold);
}
So basically I can create an action type that will do something to a specific group name when it collides, or a specific pair of group names. Then I create a trigger by calling addTrigger from the big chunk of code I pasted above. So the action will only do something to certain groups, but you are also only feeding it collision information from certain group collisions too.
Hopefully this makes some sort of sense... This is actually just for one type of trigger too. I'm not sure if this is the absolute best way to do this for flexibility/speed considerations, but it's the best I've come up with so far. I know I could optimize it eventually by using integers instead of strings, but I haven't really noticed a big impact on speed yet. Maybe when I get hundreds of triggers going I will have to optimize more
🙂Ok I'm starting to worry that this post is actually going to go past the 60,000 character limit, but I want to mention one other thing. I am using the b2World.Query method to get only the objects that are on screen. This way they don't get rendered, and sounds don't play for objects that aren't on screen. Of course if you wanted them to play for objects just a little off-screen you could adjust it for this and then calculate distance and make them quieter. I am also planning on having an actual Trigger class, that will store some flags for things like if they should always fire, or if they should just fire when the objects involved are on screen. That should speed things up quite a bit
😀 This is becoming more of an article than a post, and probably more than you wanted to know, so I guess just let me know if you have any questions.