Search code examples
c++box2dsfml

SFML and Box2D not compatible


So,I'm learning to use Box2D,and for rendering I use SFML,but I've run into some issues.Note: I'm using Visual Studio 2017 on Windows 10,and latest versions of SFML and Box2D.

I've created a ground box like in the Box2D manual:

b2Vec2 gravity(0.0f, -9.81f);
b2World world(gravity);

b2BodyDef groundBodyDef;
groundBodyDef.position.Set(50.0f, 100.0f);

b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 10.0f);
groundBody->CreateFixture(&groundBox, 0.0f);

And also some SFML code for creating a window,running the game loop and creating a sf::RectangleShape object,which is at the same place as groundBox,however I'm not sure how to do that so at the time I just use magical numbers.

RenderWindow window;
window.create(VideoMode(640, 480),"Test for Box2D");
Event event;

//Box----------------------------------
RectangleShape box(Vector2f(100.0f, 20.0f));
box.setPosition(50.0f, 100.f);
box.setFillColor(Color::Red);


//Game loop---------------------------
while (window.isOpen()) {
    //window closure
    while (window.pollEvent(event)) {
        if (event.type == Event::Closed) {
            window.close();
        }
    }

    //////// Drawing  ////////
    window.clear();
    //------------------------------------------
    window.draw(box);
    //------------------------------------------
    window.display();
}

My problem is an linking error; if I use x86 I get a bunch of Box2D errors, and at the end this:

warning LNK4272: library machine type 'x64' conflicts with target machine 'x86'
fatal error LNK1120: 11 unresolved externals

Here's a few of the errors I get:

`Source.obj : error LNK2001: unresolved external symbol "public: virtual 
class b2Shape * __thiscall b2PolygonShape::Clone(class b2BlockAllocator 
*)const " (?Clone@b2PolygonShape@@UBEPAVb2Shape@@PAVb2BlockAllocator@@@Z)`

Source.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall b2PolygonShape::GetChildCount(void)const " (? GetChildCount@b2PolygonShape@@UBEHXZ)

Source.obj : error LNK2019: unresolved external symbol "public: void __thiscall b2PolygonShape::SetAsBox(float,float)" (? SetAsBox@b2PolygonShape@@QAEXMM@Z) referenced in function _main

Source.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall b2PolygonShape::TestPoint(struct b2Transform const &,struct b2Vec2 const &)const " (? TestPoint@b2PolygonShape@@UBE_NABUb2Transform@@ABUb2Vec2@@@Z)

So I tried changing it to x64,there however I'm greeted with this:

sfml-graphics-d.lib(sfml-graphics-d-2.dll) : fatal error LNK1112: module machine type 'x86' conflicts with target machine type 'x64'

I checked multiple times to make sure to include lib and include directories, also check additional dependacies and pate right .dll SFML files into the Build (Debug) folder. I don't know where the problem lies,so any help is greatly appreciated!


Solution

  • You seem to be mixing x86 and x64 assemblies.

    If you want to create your application using a x64 compiler, you'll need to get pre-built binaries for x64 for both Box2D and SFML.

    If you want to create your application using a x86 compiler, you'll need to get pre-built binaries for x86 for both Box2D and SFML.

    But since I don't see pre-built binaries for Box2D, you probably compiled your own binaries. Then you simply need to make sure, you pick the correct compiler when building Box2D.