Pendulum behavior changed since 9/23 build

I had last checked out the last of the 9/23/07 builds. I had a pendulum set up as a b2CircleDef attached to a b2RevoluteJointDef which was anchored to a b2BoxDef. I then had a player object which is also just a box. With that build, I was able to stand on the pendulum, and the player would move as it swung. Starting with the next update (I've tried messing with each revision since then) which says "Fixed circle collision" in the SVN log, the player object doesn't quite stay on the pendulum and swing anymore. It'll swing a bit and then look sort of like it's hit a wall or something and slide off. The behavior looks odd. I'm not sure if this is a bug in the library, or if I was doing something wrong in the first place that happened to work previously but is now finally invalid. My world is set up as such:

Code:
   // mWidth/mHeight are 1024/768
   b2AABB worldbb;
   worldbb.minVertex.Set(0, 0);
   worldbb.maxVertex.Set(mApp->mWidth, mApp->mHeight);


My code to add the pendulum is:

Code:
      // gTileWidth/height are 32x32
      b2BoxDef tile;
      tile.extents.Set(gTileWidth/2, gTileHeight/2);
      tile.density = 0.0f;
      tile.friction = 0.0f;

      b2BodyDef tile_body;
      tile_body.position.Set(470 + gTileWidth/2, 230 + gTileHeight/2);
      tile_body.AddShape(&tile);
      gBodies.push_back(gWorld->CreateBody(&tile_body));
      gBodies.back()->m_invI = 0.0f;
      b2Body* b = gBodies.back();

      gInfoVec.push_back(TileInfo());
      gInfoVec.back().mId = ++gLastId;
      gInfoVec.back().mBody = gBodies.back();
      b->m_userData = (void*)(&gInfoVec.back());

      
      b2CircleDef sd;
      sd.radius = 20.0f;
      sd.density = 1.0f;
      sd.friction = 0.0f;
         
      b2BodyDef bd;
      bd.AddShape(&sd);
      bd.position.Set(b->m_position.x + 150, b->m_position.y + 150);

      b2RevoluteJointDef jd;
      jd.anchorPoint.Set(b->m_position.x, b->m_position.y);
      jd.body1 = b;

      b2Body* body = gWorld->CreateBody(&bd);
      gBodies.push_back(body);
      gPendulum = gBodies.back();
      jd.body2 =body;
      gJoints.push_back(gWorld->CreateJoint(&jd));

      gInfoVec.push_back(TileInfo());
      gInfoVec.back().mId = ++gLastId;
      gPendulumId = gLastId;
      gInfoVec.back().mBody = gBodies.back();
      gBodies.back()->m_userData = (void*)(&gInfoVec.back());


My player is created as:
Code:
   // gPlayerWidth/height are 35/80
   b2BoxDef boxDef;
   boxDef.extents.Set(gPlayerWidth/2, gPlayerHeight/2);
   boxDef.density = 0.000001f;
   boxDef.friction = 1.0f;
   
   b2BodyDef bodyDef;
   bodyDef.position.Set(100 + gPlayerWidth/2, 100 + gPlayerHeight/2);
   bodyDef.AddShape(&boxDef);
   gPlayer = gWorld->CreateBody(&bodyDef);
   gPlayer->m_invI = 0.0f;

   gInfoVec.push_back(TileInfo());
   gInfoVec.back().mId = ++gLastId;
   gInfoVec.back().mBody = gPlayer;
   gPlayer->m_userData = (void*)(&gInfoVec.back());
   gPlayer->AllowSleeping(false);


If the user presses an arrow key, I move the player like this (right direction only shown)

Code:
      // amount is 30
      gPlayer->WakeUp();
      gPlayer->ApplyForce(b2Vec2(amount, 0), b2Vec2(0, 0));
      amount_applied = amount;


I then loop over all the contacts in the world. If I find that the manifold count is > 0, and if the manifold normal y is either -1 or 1 and if the ID of the user object stored in one of the contact shapes is the pendulum and the other is the player, I assume the player is standing on the pendulum.

After the looping, if I'm on the pendulum, I add the X part of the pendulum's linear velocity to the x component of the player's linear velocity (to keep the player moving at the same speed and thus stay on the pendulum). I then step the world, and to keep the player from sliding all over the place, if I was standing on the pendulum I then zero the x component of the player's linear velocity.

I hope that makes sense....I can send you the actual project file if this is confusing. Anyways, a link to a zip that has 2 exe's, one that shows the expected behavior, one that shows the changes after the 9/23 build. Press space to jump, arrow keys to move. Stand on the pendulum to see what I mean. Thanks for the help, I appreciate it. http://www.megaupload.com/?d=SGZPEFA1
I wonder if the player is momentarily losing contact. Try increasing b2_linearSlop a bit (you'll see more overlap).

You can also try drawing the contact points like I do in the test bed. This will let you see if the contact was lost.

Making the player's mass so small is a bit dangerous. You may need a motor to drive the pendulum back and forth anyway (because the motion will diminish), so the there is less of a reason to make the player mass so small.

Maybe you should give the player a way to add energy to the pendulum.
Well, the reason I made the player's density so small was because I found that otherwise I'd need to apply a huge force to move the player. I tried making the player's density 1.0f and found that I needed to increase the force from 30 to 30,000,000 to move him equivalently. I'm not sure how else I'd get more normal looking values.

I tried increasing the linear slop to 1.005 from 0.005 and it helps...but you still kind of get the same problem, though not as often. As for giving the player a way to add energy to the pendulum, I'm just doing a simple test of some basic features in Box2D, it's kind of a quick and dirty sandbox. I'll definately be looking into making it more interactive and incorporating some of the cool new features later on.

I drew the contact points, and it doesn't appear to ever actually lose contact while on the pendulum. Maybe the real issue is my ultra miniscule values for density.
You might have better luck if you use more normalized units. For example, normalize your units so each tile is 1x1. This will help bring all numbers closer to 1.

As far as the glitch you are, I can take a look at your project. You can find my email address on this page:
http://www.gphysics.com/about-me
I normalized my units. I suppose it's more sensical for the player to have a density of 1 rather than 0.00000001. However, this didn't change anything regarding the behavior of the pendulum.
What you are doing is a little tricky. I found that the player actually does lose contact some frames.

Here's how you can prove that to yourself:

Code:
bool on_pendulum = false;
static bool on_pendulum_last = false;
...
if (on_pendulum == false && on_pendulum_last == true)
{
   // lost contact, set a break point here
}
...
on_pendulum_last = on_pendulum;


Not to fear, you can make your code work by just setting the player velocity to the circle velocity (don't increment). Also, don't zero the player velocity at the end of the step.

You can also apply a downward force on the player to make him stick better.

I also tried just using friction (player and circle friction = 1.0f). This works without any direct velocity manipulation.
Awesome, that worked (setting the velocity to the pendulum's and not resetting it). I do still reset the velocity to 0 if the player is just walking on a normal ground tile so that he doesn't fly across the screen and only moves when the key is down.

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