I have SFML set up with a Box2D world that has gravity, but my bodies won't move. I std::cout
'ed the position of the body in the game loop, but it's always at (0, 14)
!
No errors or warnings, just no movement when there's supposed to be.
I'm quite new to Box2D, so I'm unsure if I'm doing this correctly. What mistake am I making?
main.cpp:
#include
#include
#include
#include
#include "Box.h"
sf::RenderWindow window;
int main(int argc, char* argv[])
{
// Initialize stuff here since the things here will only run on startup
b2Vec2 gravity(0, 9.8f);
b2World world = b2World(gravity, true);
// Initialize entities
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 10.0f);
groundBody->CreateFixture(&groundBox, 0.0f);
Box box;
box.init(&world, sf::Vector2f(0.0f, 14.0f), sf::Vector2f(15.0f, 15.0f), sf::Color::White);
sf::RenderWindow window(sf::VideoMode(1200, 840), "Title");
while (window.isOpen())
{
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
window.close();
}
// Drawing
world.Step(1 / 60, 8, 3);
window.clear();
box.getSfShape().setPosition(sf::Vector2f(box.getBody()->GetPosition().x / 30.0f, box.getBody()->GetPosition().y / 30.0f));
box.getSfShape().setRotation(box.getBody()->GetAngle());
window.draw(box.getSfShape());
//std::cout << "X : " << box.getSfShape().getPosition().x << " Y : " << box.getSfShape().getPosition().y << std::endl;
std::cout << "X : " << box.getBody()->GetPosition().x << " Y : " << box.getBody()->GetPosition().y << std::endl;
window.display();
}
}
Box.h:
#pragma once
#include
#include
class Box
{
public:
Box();
~Box();
void init(b2World* world, sf::Vector2f position, sf::Vector2f dimensions, sf::Color color);
b2Body* getBody() { return body; }
b2Fixture* getFixture() { return fixture; }
sf::RectangleShape getSfShape() { return sfmlShape; }
private:
b2Body* body = nullptr;
b2Fixture* fixture = nullptr;
sf::RectangleShape sfmlShape;
};
Box.cpp:
#include "Box.h"
Box::Box()
{
}
Box::~Box()
{
}
void Box::init(b2World * world, sf::Vector2f position, sf::Vector2f dimensions, sf::Color color)
{
// Creates a body definition
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(position.x, position.y);
// Create the body into the world
body = world->CreateBody(&bodyDef);
// Create the box for the body
b2PolygonShape boxShape;
boxShape.SetAsBox(dimensions.x / 2.0f, dimensions.y / 2.0f);
// Create a fixture for the body
b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixture = body->CreateFixture(&fixtureDef);
sfmlShape.setFillColor(color);
sfmlShape.setPosition(body->GetPosition().x / 30.0f, body->GetPosition().y / 30.0f);
sfmlShape.setRotation(body->GetAngle());
}
Answer
1/60
in your world.Step
call is an integer division, which truncates toward zero. The world is being told to step by 0 seconds every iteration of the loop, hence nothing moves.
Write 1.0f / 60.0f
instead.
No comments:
Post a Comment