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:
My code to add the pendulum is:
My player is created as:
If the user presses an arrow key, I move the player like this (right direction only shown)
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
Code:
// mWidth/mHeight are 1024/768
b2AABB worldbb;
worldbb.minVertex.Set(0, 0);
worldbb.maxVertex.Set(mApp->mWidth, mApp->mHeight);
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());
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);
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;
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