help with design

Hi all!

First and foremost, many thank to Eric and this great piece of software. I'm being through the testbed and some other tutorials on the net and I'm finding it easy to use yet responsive enough for my needs.

I'm writing a physics manager for my game engine and I want some tips on which design approach should I take. I don't have much experience with design of large softwares and I can't seem to make a decision on which structure the physics manager should take.

Right now I've thought on the following structure:

Code:
class PhysicsManager {
private:
Box2D world's attributes;
collection of body objects (list? map?); //they will be scene based
collision listeners;
public:

init() { //will create shapes, fixtures and bodies

create Body definition based on scene object properties;
create body Object;
create Shape based on scene object properties;
create Fixture definition;
create fixture object;

attach shape to fixture;
attach fixture to body;
push_back body to collection;

}

update () {
step the physics;
}

free() {
delete world;
?delete bodies?;
}


}

};


the pseudo-code above looks ok for me, but the problem is: where do I insert the collision listeners? BTW, where can I learn more about this listeners thing? It seems like a really important subject, but I can't grasp its true meaning.
The listener is probably not something you'd usually bother with unless you need some special collision handling or want to log collision information.

One thing it can be used for is disabling a contact, for instance if you want a oneway platform. viewtopic.php?f=3&t=4871

If I understand the docs correctly, a world can only have one contactlistener. I think it would be rather messy to make lots of different things happen there, not to mention the performance impact. This place is for things that has to handled during the collision resolving.

Most gameplay features can and probably should be done outside box2d with the help of information available from your bodies.

I don't think a physicsmanager class is the right way to go. The b2World class is your physicsmanager. Your job is just to provide it with bodies and rules.

I store a pointer to b2Body in my object that has physics and provide it with the world it should be created in. void Player::Create_body(b2World& world);
humm...
that sure is new stuff for me. I thought I should always implement a listener class if I wanted to do "something" when an object collides. Where this "something" would be like sending information for the Actors to update their positions, etc. Anyway, think I should play more with Box2D. :/

Quote:
I don't think a physicsmanager class is the right way to go. The b2World class is your physicsmanager. Your job is just to provide it with bodies and rules.

I store a pointer to b2Body in my object that has physics and provide it with the world it should be created in. void Player::Create_body(b2World& world);


In your design, how do you step your simulation up?
The collision listeners don't really belong in any particular place.
They will be called by Box2D when the collision status of fixtures change, most typically during the Step() function but can also be when a body is created or destroyed. Another important thing for most cases is how to determine what should be done with the information you get. eg. if you are told that one body is now touching another body, so what? What does it mean for your game? This is usually handled by setting something in the user data field of bodies/fixtures so that you can access in the collision callback to tell what that thing is in your game, the player, an enemy etc.

A typical usage example would be letting the player object know whether it is standing on the ground or not. For this, you would set the player object as the user data in your b2Body, and in the BeginContact callback you could look at the user data in the bodies which touched, and if it is your player object, then you can tell it what other object in your game it just touched. Since your player can probably touch more than one object at a time, its a good idea to keep a list of what was touched and remove from this list when you get a EndContact so that you have full knowledge of what is touched at any given time - hopefully this makes sense... the main thing to be aware of is that getting an EndContact event for your player doesn't necessarily mean they are now not touching anything.
Sorry if I'm sounding too dummy, but I want to learn more about the problem before tackling it.

For what I understand, you're using callbacks and listeners interchangeably. So.. the question is: are they the same thing?

Quote:
Another important thing for most cases is how to determine what should be done with the information you get. eg. if you are told that one body is now touching another body, so what? What does it mean for your game? This is usually handled by setting something in the user data field of bodies/fixtures so that you can access in the collision callback to tell what that thing is in your game, the player, an enemy etc.


you nailed it! this is what I was trying to convey in my question. So, in a typical step loop, how do I handle this info? I see that a callback will be implemented outside the loop and will be called whenever the bodies collide. Then this callback will fill a list of collisions that happened at that frame and, after the EndContact is done, I can proceed with the game logic.

so... whenever I need special game logic being processed after collisions, I need to implement specific callback functions to deal with it? Or at least one callback function that knows which kind of game event happened and call the other functions.
draconar wrote:
...using callbacks and listeners interchangeably. So.. the question is: are they the same thing?
Yeah pretty much. Imagine you are the boss of a company, and you hire someone to do some number-crunching accounting work. Obviously you don't want to sit there watching him work all day because you have other things to do, but you might say to him, "Most of the stuff you do I don't need to know about, but sometimes important things happen that you should inform me of. Call this number if the xyz account goes below $1000" or something like that, and then you go off and do your other stuff. In this example you are the main game code, the employee is Box2D, the phone number is the callback function, and the listener is whoever the phone number is for.

draconar wrote:
So, in a typical step loop, how do I handle this info?
Basically how I said in my second paragraph in the post above. To be more specific, the callback functions (BeginContact, EndContact etc) will give you a b2Contact. From this you can get the two fixtures which touched (or separated, for EndContact). If you stored user data in these when you created them, you can access it to see what thing that was in your game. From a fixture you can access the body involved, which you can also store your user data in. Once you have figured out what part of your game these are related to, what you do depends on your game. See the PreSolve example in the manual for some typical code.

draconar wrote:
so... whenever I need special game logic being processed after collisions, I need to implement specific callback functions to deal with it? Or at least one callback function that knows which kind of game event happened and call the other functions.
You make a class which extends b2ContactListener, as shown in the manual at section 9.4, then you override the functions that you are interested in. If you are only interested (as most of us are) in when things are touching each other, you will only need to override the BeginContact and EndContact functions. Then pass this class to b2World::SetContactListener when you set up your world.
I can't remember if there is an example of using contact listeners in the testbed examples, but it sounds like you just need to read section 9.4 of the manual, sloooowly... 🙂 Are you using C++?

Original thread: https://box2d.org/forum/viewtopic.php?t=5715