I've been looking into 2D physics engines and Box2D came up. I've been reading into it and trying to set things up but have had issues.
- Create the build with premake5
- Open up a new Visual Studio 2019 project
- Set the Additional Include Directory to the Box2D header folder
- Set the Additional Library Directories (under linker) to the folder that contains the lib file (Box2D.lib)
- Set the Additional Dependencies to Box2D.lib
With this all done I seem to be able to call and create Box2d Variables and functions without issue. But while I was trying to set up a world it began to cause errors.
The Errors
LNK1120 2 unresolved externals
LNK2019 unresolved external symbol "public: __thiscall b2World::b2World(struct b2Vec2 const &)" (??0b2World@@QAE@ABUb2Vec2@@@Z) referenced in function "public: void __thiscall Game::run(void)" (?run@Game@@QAEXXZ)
LNK2019 unresolved external symbol "public: __thiscall b2World::~b2World(void)" (??1b2World@@QAE@XZ) referenced in function "public: void __thiscall Game::run(void)" (?run@Game@@QAEXXZ)
The project I'm setting up is an attempt to use SFML with Box2d. Running the testbed in the generated folder works but I'm trying to figure out how to port the engine into other projects if needed. The solution works as expected. But as soon as the World Variable is created it creates errors.
But creating and setting the b2Vec2 and b2BodyDef variables cause no issues. I'm not quite sure what the cause could be, I've looked through the various guides online, checked the paths were correct and regenerated the library file.
I suspect that the issue is that Box2D was configured to build in x64 and my project is building in x86. I was wondering if anyone would know a way to either run Box2D in x86 or something of the sort.
The Code
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
sf::Time timePerFrame = sf::seconds(1.f / 60.f); // 60 fps
b2Vec2 gravity(0.0f, -10.0f);
b2World myWorld = b2World(gravity); //The errors are cause by this line.
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
while (m_window.isOpen())
{
processEvents(); // as many as possible
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
processEvents(); // at least 60 fps
update(timePerFrame); //60 fps
}
render(); // as many as possible
}
}