In the screenshot, the red block is the "player". The white large rectangle on the left is a box that represents a wall. The smaller 32x32 boxes on the bottom are tiles, also represented as boxes for collision detection. If I let the player drop all the way to the bottom, I can move left/right as I would expect, not getting stuck on anything. However, while falling, if I press left to basically try to hug the wall while falling, the Y value of the player doesn't change, it's as if it's stuck. But if I let go of the left button, he falls to the ground as normal, under the effect of gravity. I'm not sure as to why he gets stuck here. My initialization code is as follows:
In my update loop, this is all I'm doing:
I doubt my draw code matters, but I'll post it anyways.
Am I perhaps just moving the player incorrectly? Is there a better method than just adding to the linear velocity? Thanks for the help.
Code:
b2AABB worldbb;
worldbb.minVertex.Set(0, 0);
worldbb.maxVertex.Set(800, 600);
gWorld = new b2World(worldbb, b2Vec2(0, 100.0f), true);
// gTileWidth and height are defined as 32 and 32
for (int i = 0; i < 800 / gTileWidth; i++)
{
b2BoxDef tile;
tile.extents.Set(gTileWidth/2, gTileHeight/2);
tile.density = 0.0f;
tile.friction = 1.0f;
b2BodyDef tile_body;
tile_body.position.Set(i*gTileWidth + gTileWidth/2, 600 - gTileHeight + gTileHeight/2);
tile_body.AddShape(&tile);
gTiles.push_back(gWorld->CreateBody(&tile_body));
}
b2BoxDef tile;
tile.extents.Set(gTileWidth/2, (600 - gTileHeight)/2);
tile.density = 0.0f;
tile.friction = 1.0f;
b2BodyDef tile_body;
tile_body.position.Set(0 + tile.extents.x, 0 + tile.extents.y);
tile_body.AddShape(&tile);
gTiles.push_back(gWorld->CreateBody(&tile_body));
// gPlayerWidth and height are defined as 100x73
b2BoxDef boxDef;
boxDef.extents.Set(gPlayerWidth/2, gPlayerHeight/2);
boxDef.density = 1.0f;
boxDef.friction = 1.0f;
b2BodyDef bodyDef;
bodyDef.position.Set(100 + gPlayerWidth/2, 200 + gPlayerHeight/2);
bodyDef.AddShape(&boxDef);
gPlayer = gWorld->CreateBody(&bodyDef);
gPlayer->m_invI = 0.0f;
gPlayer->AllowSleeping(false);
worldbb.minVertex.Set(0, 0);
worldbb.maxVertex.Set(800, 600);
gWorld = new b2World(worldbb, b2Vec2(0, 100.0f), true);
// gTileWidth and height are defined as 32 and 32
for (int i = 0; i < 800 / gTileWidth; i++)
{
b2BoxDef tile;
tile.extents.Set(gTileWidth/2, gTileHeight/2);
tile.density = 0.0f;
tile.friction = 1.0f;
b2BodyDef tile_body;
tile_body.position.Set(i*gTileWidth + gTileWidth/2, 600 - gTileHeight + gTileHeight/2);
tile_body.AddShape(&tile);
gTiles.push_back(gWorld->CreateBody(&tile_body));
}
b2BoxDef tile;
tile.extents.Set(gTileWidth/2, (600 - gTileHeight)/2);
tile.density = 0.0f;
tile.friction = 1.0f;
b2BodyDef tile_body;
tile_body.position.Set(0 + tile.extents.x, 0 + tile.extents.y);
tile_body.AddShape(&tile);
gTiles.push_back(gWorld->CreateBody(&tile_body));
// gPlayerWidth and height are defined as 100x73
b2BoxDef boxDef;
boxDef.extents.Set(gPlayerWidth/2, gPlayerHeight/2);
boxDef.density = 1.0f;
boxDef.friction = 1.0f;
b2BodyDef bodyDef;
bodyDef.position.Set(100 + gPlayerWidth/2, 200 + gPlayerHeight/2);
bodyDef.AddShape(&boxDef);
gPlayer = gWorld->CreateBody(&bodyDef);
gPlayer->m_invI = 0.0f;
gPlayer->AllowSleeping(false);
In my update loop, this is all I'm doing:
Code:
float amount = 200;
if (mKeyDown[KEYCODE_RIGHT])
gPlayer->m_linearVelocity += b2Vec2(amount, 0);
else if (mKeyDown[KEYCODE_LEFT])
gPlayer->m_linearVelocity += b2Vec2(-amount, 0);
// gTimeStep and iterations are defined as 1.0f / 60.0 and 10
gWorld->Step(gTimeStep, gIterations);
// stop the player from sliding as if the tiles were icy
if (mKeyDown[KEYCODE_RIGHT] || mKeyDown[KEYCODE_LEFT])
gPlayer->m_linearVelocity.x = 0;
if (mKeyDown[KEYCODE_RIGHT])
gPlayer->m_linearVelocity += b2Vec2(amount, 0);
else if (mKeyDown[KEYCODE_LEFT])
gPlayer->m_linearVelocity += b2Vec2(-amount, 0);
// gTimeStep and iterations are defined as 1.0f / 60.0 and 10
gWorld->Step(gTimeStep, gIterations);
// stop the player from sliding as if the tiles were icy
if (mKeyDown[KEYCODE_RIGHT] || mKeyDown[KEYCODE_LEFT])
gPlayer->m_linearVelocity.x = 0;
I doubt my draw code matters, but I'll post it anyways.
Code:
b2Vec2 ppos = gPlayer->GetOriginPosition();
SetColor(Color::White);
for (int i = 0; i < gTiles.size(); i++)
{
b2Vec2 tpos = gTiles[i]->GetOriginPosition();
b2Body* b = gTiles[i];
if (i < gTiles.size() - 1)
DrawRect(tpos.x - gTileWidth/2, tpos.y - gTileHeight/2, gTileWidth, gTileHeight);
else
DrawRect(tpos.x - gTileWidth/2, tpos.y - (600 - gTileHeight)/2, gTileWidth, 600 - gTileHeight);
}
SetColor(Color(255, 0, 0));
FillRect(ppos.x - gPlayerWidth/2, ppos.y - gPlayerHeight/2, gPlayerWidth, gPlayerHeight);
SetColor(Color::White);
for (int i = 0; i < gTiles.size(); i++)
{
b2Vec2 tpos = gTiles[i]->GetOriginPosition();
b2Body* b = gTiles[i];
if (i < gTiles.size() - 1)
DrawRect(tpos.x - gTileWidth/2, tpos.y - gTileHeight/2, gTileWidth, gTileHeight);
else
DrawRect(tpos.x - gTileWidth/2, tpos.y - (600 - gTileHeight)/2, gTileWidth, 600 - gTileHeight);
}
SetColor(Color(255, 0, 0));
FillRect(ppos.x - gPlayerWidth/2, ppos.y - gPlayerHeight/2, gPlayerWidth, gPlayerHeight);
Am I perhaps just moving the player incorrectly? Is there a better method than just adding to the linear velocity? Thanks for the help.