Looking for some pointers.

Thanks! It will take me a while to digest all of this but I appreciate your tips (I wasn't even aware of "b2Settings").

After my last post I realized that the friction was really acting as expected - the ball should get stuck - and considered just avoiding acute angles altogether.
Erin Catto wrote:
Another possibility would be to disable warm-starting on character bodies. This will help prevent the build up of large friction forces.


How do you disable warm-staring on character bodies?

I found that my character could move fine initially but after playing around and letting him rest, it got permanently stuck. I then set allowSleep = false and it works fine, however I don't really understand why. Can you please explain this to me?
When bodies are inactive (i.e. there are no active forces moving them), they go to sleep mode so that Box2D can exclude them from computations for efficiency's sake. The bodies are woke up when another objects intersect with them. However doing applyForce or applyImpulse does not wake up a body. I didn't realise that your "got stuck" problem was that it went into sleep mode. I just do wakeUp() (or whatever the method was, can't check atm) for the player's body before I apply an impulse.
Yes, I feel like an idiot. I had no idea about the wakeUp() function until just now.

Regarding objects falling into an item and bouncing back up, can that be fixed with a setting? Should I add some additional checks in the code?
Quote:
Regarding objects falling into an item and bouncing back up, can that be fixed with a setting? Should I add some additional checks in the code?


Try running at 60Hz. Also, what size are your shapes? Did you adjust the length scale in b2Settings.h?
Quote:
Try running at 60Hz.[/QUOTE]

I'm running at 120 FPS, but you're right this is the key. The test bed examples that are around 60 fps do not have the problem I described, while those that run at ~30 do. Obviously the huge frame drop occurs when there are many shapes on the screen, but are there any optimizations I can do?

[QOUTE]Also, what size are your shapes? Did you adjust the length scale in b2Settings.h?.[/QUOTE]


So far I have only been playing with the test bed examples. I have modified the TestBridge.as (AS3 version).

The rectangle I'm creating for the character has a width of 0.35 and a height of 0.3. I basically took one of the rectangles created in that test bed example and made it controllable.

I have not tampered with the b2Settings, so I'm using the default 1.0 length.
For anyone who ran into the same problem I encountered (high friction causing circles to get stuck in acute angles), here's the approach I took to solve it:

1. Detect when the player (the circle) should move. In my case, this is when a "roll-left" or "roll-right" key is pressed but the rotation of the body remains the same after 2 steps.

2. Arbitrarily set the friction of the first contactNode to zero (node.GetContactList().contact.m_friction = 0).

This works - the circle can escape the acute angle. There are still some problems though:

1. The circle may get stuck in a situation involving more than 2 other bodies.

2. Since the selection of a contact to negate friction is arbitrary, the player can only escape if they roll in the right direction. The other direction will still result in them being stuck.

I think this is the approach I'm going to have to take - on each iteration, if the player is stuck, keep trying different combinations of contacts (to set friction to zero on) until the player moves. So, on the first step I'd negate the first contact, on the second I'd negate the second contact (and restore the first). This might get pretty hairy for situations involving more than 2 contacts though - I'd have to eventually try negating 2 or more contacts in the search for the perfect combination that will free the player. It's kind of brute force. Is there an easier way to detect which contact or contacts are causing the most problem? For example, say the ball is stuck in a "V" shaped acute angle. If the player "rolls-right" they should escape up the right side (I should negate the friction of the left side), and vise-versa if they "roll-left."
@mayobutter:

There are a variety of ways you could set this up, if you really need to manually modify the friction of a contact. I'm not sure whether that's really necessary because it seems as if maybe the friction of the bodies involved should just be lower to begin with.

If you absolutely need to modify the friction manually, you can figure out which specific contacts involve the controlled body. You could store information in the userData pointer that would indicate what type of game object it is.

Flash code:
Code:
for (var c:b2Contact = Environment.world.m_contactList; c; c = c.m_next) {
   if (c.GetManifoldCount() > 0) {
      var contactManifold:b2Manifold;
      var shape1:b2Shape = c.GetShape1();
      var shape2:b2Shape = c.GetShape2();
      contactManifold = c.GetManifolds()[0] as b2Manifold;

                //check userData property of shapes here and set c.m_friction if the shape
                //you want is involved

   }   
}      


So you would have to set the userData for the shapes that are part of your controllable body, or you could just set it for the body itself and check shape1.m_body.m_userData for instance.

I actually use something like this loop to check against a list of shape pair types to look for so that I can execute a certain function or event when a collision happens. I'm using a 2-dimensional Dictionary to hold these pairs. Works pretty well!
brainclog wrote:
I'm not sure whether that's really necessary because it seems as if maybe the friction of the bodies involved should just be lower to begin with.


I need the high friction because the player is a rolling ball. Without the friction, the ball just spins and doesn't go anywhere.

brainclog wrote:
I actually use something like this loop to check against a list of shape pair types to look for so that I can execute a certain function or event when a collision happens. I'm using a 2-dimensional Dictionary to hold these pairs. Works pretty well!


Thats very, very similar to what I'm doing right now (except I loop through on a per-body basis, as opposed to per-shape). The problem isn't knowing which bodies are in contact, but determining what contacts are preventing rotation via friction. For every iteration, if the ball should-be-but-isn't rotating, I have a set of high friction contacts, some subset of which are preventing the ball from rolling. The problem is figuring out which contacts are the problem and negating them without guessing over each iteration.

So say a ball is stuck in a high friction "V", and tries to roll-right, up and out of the V. It can't, because it will also be rolling down the left side of the V at that instance (it's in contact with both sides when it's at the bottom ). No problem - set the friction of the left side of the V to 0. The problem is doing this in a general sense.
Nevermind... ended up just applying impulses in the opposite direction of each contact when the ball gets stuck, so it "pops" out of the acute angle. Much simpler.

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