It's not degenerate cases. It's that the contact list lags one frame behind the actual layout, aways, from the point of view of the user.
Suggestions for improving the contact callback system
General DiscussionI've posted a better explanation of the problem (with a screenshot) in my original topic:
viewtopic.php?f=3&t=2031&p=11947#p11947
I see a typical Box2d program loop executing like so:
1. Collect input in the "down time" between time steps.
2. Apply forces in response to contacts and input.
3. Run the physics time step.
4. Redraw everything to the screen with the updated positions.
The problem with delayed contact events is in step 2 - while processing input / deciding what to do with your actors, you may need to consult the contact list. BUT this contact list is lagging by one frame. Ideally, another step could be introduced:
1...
1.5. Process contacts.
2...
I have no idea if this step could be separated from the main time step, so this idea may be laughable from the start.
At high frame rates, as long as you understand this problem it's easy to work around. The discord between what you see on the screen and what's reported in the listener bother me a bit though. I've already been burned by it once.
On another note, I've resisted posting in this excellent discussion up to now. Honestly, I've grown to like the simplicity of the ContactListener in it's current iteration (with one patch - criswa's passing the b2ContactPoint). It's simple enough to built whatever you want on top of it. I like the approach of making various ContactListener subclasses available to users so they can choose the implementation that suits them. Some of the suggestions made here I can see absolutely no use for... but obviously someone has a use for them. I guess that's all the input I'll give on that 🙂
viewtopic.php?f=3&t=2031&p=11947#p11947
I see a typical Box2d program loop executing like so:
1. Collect input in the "down time" between time steps.
2. Apply forces in response to contacts and input.
3. Run the physics time step.
4. Redraw everything to the screen with the updated positions.
The problem with delayed contact events is in step 2 - while processing input / deciding what to do with your actors, you may need to consult the contact list. BUT this contact list is lagging by one frame. Ideally, another step could be introduced:
1...
1.5. Process contacts.
2...
I have no idea if this step could be separated from the main time step, so this idea may be laughable from the start.
At high frame rates, as long as you understand this problem it's easy to work around. The discord between what you see on the screen and what's reported in the listener bother me a bit though. I've already been burned by it once.
On another note, I've resisted posting in this excellent discussion up to now. Honestly, I've grown to like the simplicity of the ContactListener in it's current iteration (with one patch - criswa's passing the b2ContactPoint). It's simple enough to built whatever you want on top of it. I like the approach of making various ContactListener subclasses available to users so they can choose the implementation that suits them. Some of the suggestions made here I can see absolutely no use for... but obviously someone has a use for them. I guess that's all the input I'll give on that 🙂
Reprocessing contacts is actually pretty easy. I expect the following would "just work"
This will update the contacts, and fire off extra events as appropriate. It might not work given the AABB sweeping you point out in the other thread, that would require some extra work.
It's more the performance issues that prevent us from just putting it in. I suppose it's possible to make it an option, or (probably better), ensure contacts are up to date as and when they are queried.
The delayed re-action thing is bothering me. As you say, you can account for it by keeping stuff for more than one step, but by the time you are doing that, you might as well be using the event listener. I want to be able to avoid the listener entirely for simple applications, or for dealing with things that are continuous in nature.
I've actually only implemented a few things, and all of them have obvious uses imho:
Late sensoring:
Mario platforms, shatterable objects.
ContactStart/ContactStop:
Convenience classes when you don't care about individual contact points. Also, events for sensors make more sense now.
IsTouching:
Keeping track of what is touching what using contact listeners is tedious, and Box2D already does it, just in less efficient structures. So why do it, for the majority of cases. Also, provide b2ContactResults instead of returning more classes.
b2ContactEdge::GetNext:
Provide contact list access without having to understand about speculative contacts. Abstraction, in other words.
Most of it is convenience/simplification. The events you are using are equally powerful, but pretty unintuitive to use until you have a good grasp of the engine, and it means the user would be duplicating some code provided by the above.
Code:
for(b2Contact* contact = GetContactList();contact;contact=contact->GetNext())
{
contact.Update(/*something*/);
}
{
contact.Update(/*something*/);
}
This will update the contacts, and fire off extra events as appropriate. It might not work given the AABB sweeping you point out in the other thread, that would require some extra work.
It's more the performance issues that prevent us from just putting it in. I suppose it's possible to make it an option, or (probably better), ensure contacts are up to date as and when they are queried.
The delayed re-action thing is bothering me. As you say, you can account for it by keeping stuff for more than one step, but by the time you are doing that, you might as well be using the event listener. I want to be able to avoid the listener entirely for simple applications, or for dealing with things that are continuous in nature.
I've actually only implemented a few things, and all of them have obvious uses imho:
Late sensoring:
Mario platforms, shatterable objects.
ContactStart/ContactStop:
Convenience classes when you don't care about individual contact points. Also, events for sensors make more sense now.
IsTouching:
Keeping track of what is touching what using contact listeners is tedious, and Box2D already does it, just in less efficient structures. So why do it, for the majority of cases. Also, provide b2ContactResults instead of returning more classes.
b2ContactEdge::GetNext:
Provide contact list access without having to understand about speculative contacts. Abstraction, in other words.
Most of it is convenience/simplification. The events you are using are equally powerful, but pretty unintuitive to use until you have a good grasp of the engine, and it means the user would be duplicating some code provided by the above.
BorisTheBrave wrote:
It's not degenerate cases. It's that the contact list lags one frame behind the actual layout, aways, from the point of view of the user.
Er, I was arguing against the specific scenario. I don't see the contact list being updated immediately as being something the end-user should be able to count on or have to know about; box2d is such a complicated simulation that there are a million different ways for playerBody->IsTouching(groundBody) to not do what you want in this particular case. Polling isn't a good solution here because it doesn't capture the problem well.
Reporting events more immediately might improve the quality of the simulation, but you probably want to use IsTouching for things like continuous lava damage or keeping a count of how long you've been near a proximity switch. Stuff that doesn't care for exact timing or orientation. Otherwise you're going to be standing against a wall and the game will think you're jumping.
It's true that the main use of such access is for continuous time things, so in most cases the lag shouldn't be a problem. But jumping is an instantaneous thing, but is not well handlable by events. Wouldn't it be nice, just to loop over the contact list to check if you are standing on something, rather than setting and restoring flags. GetContactList suffers the same problem as IsTouching.
I gather that's the nature of the complaint, though for lack of any suggestsions I think we'll have to ignore it.
I gather that's the nature of the complaint, though for lack of any suggestsions I think we'll have to ignore it.
OK. If you want to fix it, then it wouldn't be too expensive to evaluate the contacts early, assuming manual shape movement is more rare.
Step 7:
Step 1:
The dirty bit defaults to true, and anything that modifies the shape sets the dirty bit.
The only problems I can see with this solution are things like resetting a shape to the same position each time might experience more instability.
Step 7:
Code:
for each shape:
shape.dirty = false
for each contact:
contact.Evaluate()
shape.dirty = false
for each contact:
contact.Evaluate()
Step 1:
Code:
for each contact:
if (contact.shape1.dirty || contact.shape2.dirty)
contact.Evaluate()
if (contact.shape1.dirty || contact.shape2.dirty)
contact.Evaluate()
The dirty bit defaults to true, and anything that modifies the shape sets the dirty bit.
The only problems I can see with this solution are things like resetting a shape to the same position each time might experience more instability.
It's worse than that. What if I move two shapes to overlap. Do I update the broadphase etc to generate a contact? Do I fire off events then.
Even if you considered that acceptable, it would harm performance, as the broadphase "batches" pairs in some way.
Even if you considered that acceptable, it would harm performance, as the broadphase "batches" pairs in some way.
I've been giving this some thought and realized another dimension to this problem. We already have that (1) contacts are always removed a frame late and (2) when the client code responds to events, it can make changes which won't be updated until the next frame. But I've realized (3) the current state of the world will always be out of sync with events, because events have to be buffered.
So, we have two views into the status of the world: (A) the current state of the bodies, which we can poll, and (B) state based upon the events that have been processed so far. To merge these two views, we would have to remove the need for buffering (3). Client code would have to be able to change box2d as events come up. Modifications wouldn't necessarily have to affect real world state, just enough to keep track of the changes to make at the end of the frame step, and box2d would have to return the correct query information as if the change had already been made. While this restructuring is possible, it's probably infeasible.
So, since it's infeasible to remove the need for buffering (3), and highly unlikely that people will be able to do the work themselves, we might as well not worry about keeping the polled data and event data perfectly consistent.
While client code has to accept differences between polled data (A) and processed event data (B), the engine could attempt to minimize those differences. One way would be to reevaluate contacts at the end of the frame, like in my last post (which I think you were thinking of doing before), fixing (1).
Generating events immediately (2) doesn't have much benefit, since events that occur within a single timestep don't necessarily have a particular order other than start before stop (or am I mistaken?). One way to simplify things for client code would be to hold off on remove events and deallocating some data until the next timestep, so that events are never reported outside of a timestep.
So, we have two views into the status of the world: (A) the current state of the bodies, which we can poll, and (B) state based upon the events that have been processed so far. To merge these two views, we would have to remove the need for buffering (3). Client code would have to be able to change box2d as events come up. Modifications wouldn't necessarily have to affect real world state, just enough to keep track of the changes to make at the end of the frame step, and box2d would have to return the correct query information as if the change had already been made. While this restructuring is possible, it's probably infeasible.
So, since it's infeasible to remove the need for buffering (3), and highly unlikely that people will be able to do the work themselves, we might as well not worry about keeping the polled data and event data perfectly consistent.
While client code has to accept differences between polled data (A) and processed event data (B), the engine could attempt to minimize those differences. One way would be to reevaluate contacts at the end of the frame, like in my last post (which I think you were thinking of doing before), fixing (1).
Generating events immediately (2) doesn't have much benefit, since events that occur within a single timestep don't necessarily have a particular order other than start before stop (or am I mistaken?). One way to simplify things for client code would be to hold off on remove events and deallocating some data until the next timestep, so that events are never reported outside of a timestep.
I'm in mayobutters camp. I've come to like the contactListener solution. I absolutely agree that it is a pain, because it's so low level. I ascribe to C++ philosophy of you don't for what you don't use. Adding in contact buffering helps beginners at the expense of any serious/complex applications.
Can all of these buffering contacts problems be solved with a well designed contactListener? It should be provided in a box2d utility library (B2UT?).
Earlier in the thread the contactListener's start callback was going to return a bool indicating if the contact should be considered a sensor collision. Then someone requested it return whether to filter the contact entirely.
I think the best solution is something RakNet does. Return an enumeration
e_never_contact,
e_always_contact,
e_sensor,
e_contact,
e_default,
The idea here is if the start returned e_never_contact. The collision would be permanently filtered. Subsequent starting contacts will be automatically filtered. To get the shapes to collide again, all the contacts would have to be refiltered, as is done currently. e_always contact, would make Box2D ignore any future return values and always assume the callback returned e_always_contact (possible optimization), until a contact refilter. e_sensor is similar to returning false from the original proposal. e_contact forces contact resolution even between shapes and sensors. and e_default is current behavior.
-nick
Can all of these buffering contacts problems be solved with a well designed contactListener? It should be provided in a box2d utility library (B2UT?).
Earlier in the thread the contactListener's start callback was going to return a bool indicating if the contact should be considered a sensor collision. Then someone requested it return whether to filter the contact entirely.
I think the best solution is something RakNet does. Return an enumeration
e_never_contact,
e_always_contact,
e_sensor,
e_contact,
e_default,
The idea here is if the start returned e_never_contact. The collision would be permanently filtered. Subsequent starting contacts will be automatically filtered. To get the shapes to collide again, all the contacts would have to be refiltered, as is done currently. e_always contact, would make Box2D ignore any future return values and always assume the callback returned e_always_contact (possible optimization), until a contact refilter. e_sensor is similar to returning false from the original proposal. e_contact forces contact resolution even between shapes and sensors. and e_default is current behavior.
-nick
You complain of adding feature to aid novices at the expense of experts, the propose exactly that? Adding always/never stuff would necessitate actually tracking this data, even after the contact is deleted. Poor for performance..
But I do prefer the idea of returning an enum to returning a bool as present.
It seems there's still work to be done on this, but interest has waned. Would it help if I put it up as a branch (now we have branches, thanks to udoprog).
But I do prefer the idea of returning an enum to returning a bool as present.
It seems there's still work to be done on this, but interest has waned. Would it help if I put it up as a branch (now we have branches, thanks to udoprog).
Original thread: https://box2d.org/forum/viewtopic.php?t=1784