I am stepping
while(Window.isOpen())
{
Window.clear();
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
Window.close();
break;
}
}
World.Step((1/60.0f), 8, 3); // <-------
for(b2Body * BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext())
{
if(BodyIterator->GetType() == b2_dynamicBody)
{
Player.setPosition(30.0f * BodyIterator->GetPosition().x, BodyIterator->GetPosition().y * 30.0f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Body->ApplyForce(b2Vec2(5.0f, 0.0f), b2Vec2(0.0f, 0.0f), true);
std::cout << "Pressed" << std::endl;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
Body->ApplyForce(b2Vec2(-5.0f, 0.0f), b2Vec2(0.0f, 0.0f), true);
std::cout << "Pressed" << std::endl;
}
std::cout << Body->GetLinearVelocity().x << std::endl;
Window.draw(Player);
}
else if(BodyIterator->GetType() == b2_staticBody)
{
Ground.setPosition(30.0f * BodyIterator->GetPosition().x, 30.0f * BodyIterator->GetPosition().y);
Window.draw(Ground);
}
}
Window.display();
}
I think I know what the problem is but I'm not a 100% sure. I think its because my platforms are tiled, are composed of squares or sf::Rectangles that are 20 by 20. What I did is I created another program that has 1 continuous platform (one big sf::rectangle), that covers the all screen and acts as the ground. So far I haven't had any problems moving the character. So is a tiled platform design a good idea when using Box2D?
const int MAP_WIDTH = 40;
const int MAP_HEIGHT = 20;
int MAP[MAP_HEIGHT][MAP_WIDTH] =
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
const int TILE_SIZE = 20;
const int FLOOR_SIZE = 50;
sf::RectangleShape Ground;
Ground.setSize(sf::Vector2f(TILE_SIZE, TILE_SIZE));
Ground.setOrigin(20.0f, 10.0f);
Ground.setFillColor(sf::Color::Red);
sf::RectangleShape Player;
Player.setSize(sf::Vector2f(10.0f, 20.0f));
Player.setOrigin(5.0f, 10.0f);
Player.setPosition(0.0f, 10.0f);
Player.setFillColor(sf::Color::White);
for(int i = 0; i < MAP_HEIGHT; i++)
{
for(int j = 0; j < MAP_WIDTH; j++)
{
if(MAP[i][j] == 1)
{
CreateGround(World, (j * TILE_SIZE), (i * TILE_SIZE));
}
}
}
b2Body * Body = nullptr;
b2BodyDef BodyDef;
BodyDef.position = b2Vec2(Player.getPosition().x / 30.0f, Player.getPosition().y / 30.0f);
BodyDef.type = b2_dynamicBody;
Body = World.CreateBody(&BodyDef);
b2PolygonShape Shape;
Shape.SetAsBox((10.0f / 2) / 30.0f, (20.0f / 2) / 30.0f);
b2FixtureDef FixtureDef;
FixtureDef.friction = 0.5;
FixtureDef.shape = &Shape;
Body->CreateFixture(&FixtureDef);
// Followed by the while loop above
void CreateGround(b2World & World, float x, float y)
{
b2BodyDef BodyDef;
BodyDef.position = b2Vec2(x / 30.0f, y / 30.0f);
BodyDef.type = b2_staticBody;
b2Body * Body = World.CreateBody(&BodyDef);
b2PolygonShape Shape;
Shape.SetAsBox((20.0f / 2) / 30.0f, (20.0f / 2) / 30.0f);
b2FixtureDef FixtureDef;
FixtureDef.density = 0.0;
FixtureDef.shape = &Shape;
Body->CreateFixture(&FixtureDef);
}