In a .dll?

Firstly, Box2D is AMAZING, and thanks soooo much for makng it open source 😀
I have only 1 problem with it 😢
I can compile the included projects fine, with no errors/warnings, and they run correctly, but I need to include your engine in a .dll. Whenever I try to create a new project with your engine, I get some not very nice errors, and it's just generally very annoying and difficult.

I get errors such as these:
Code:
error LNK2019: unresolved external symbol "public: __thiscall b2World::~b2World(void)" (??lb2World@@QAE@XZ) referenced in function _DoNothing

error LNK2019: unresolved external symbol "public: __thiscall b2World::b2World(struct b2AABB const &, struct b2Vec2 const &, bool)" (??Ob2World@@QAE@ABUb2AABB@@ABUb2Vec2@@_N@Z) referenced in function _DoNothing


Please could you show me how I can use this engine in a .dll file 😁
Thanks in advance 🙂
Hi Diggsey,

Are you sure you've linked to the .lib which should have been compiled alongside your .dll? This process is known as statically linking to a DLL.

If you wish to dynamically link to the .dll you'd need to make a call to LoadLibrary and a few more calls to GetProcAddress for each function you wish to import from the DLL.

Hope this helps,
- niX
Hi Diggsey,

I got your email, but I'm having some trouble with my isp's SMTP server.

nix_BB's analysis seems on track. You probably need to include the lib stub when you link. This lib should have been generated when you built the dll.

Another possibility is that you don't have all the __declspec(export) stuff you need when you built the dll.

My suggestion is to try to get things working with a very simple DLL, like one that squares a number.
Thanks for the help (I think I've solved it now)

I can create a .dll fine (I've already made a few plugins for DarkBasic). I looked through all the settings again, and by chance saw that it was set to automatically link in static libraries that were part of the same solution that the project 'depended' upon. I updated my settings, and hopefully it will work properly now 😀 . I'll post when I find out 😎
OK, the .dll now compiles fine with no errors/warnings. It is a plugin for a language called DarkBasic Pro, and after testing (with a simple circle and a static floor) the circle drops straight through the floor 😢
It is probably something I have done wrong, but just in-case I posted here.
BTW
Is the spin-offs/ports board for projects using Box2D as well as ports? (if not, a board for that would be nice 😀 )
Post your setup code here and I'll take a look.

Good idea, I'll make a forum for Games and Applications. Hopefully we'll get a few games out there too. 🙂
This is the C++ code in the plugin:

Code:
inline DWORD dbpfloat(float pFloat)
{
   return *(DWORD*)&pFloat;
}
inline b2World* GetWorldPtr(int ID)
{
   return (b2World*)ID;
}

inline int GetWorldID(b2World* NewWorld)
{
   return (int)NewWorld;
}

inline b2ShapeDef* GetShapePtr(int ID)
{
   return (b2ShapeDef*)ID;
}

inline int GetShapeID(b2ShapeDef* NewShape)
{
   return (int)NewShape;
}

inline b2BodyDef* GetBodyTemplatePtr(int ID)
{
   return (b2BodyDef*)ID;
}

inline int GetBodyTemplateID(b2BodyDef* NewBodyTemplate)
{
   return (int)NewBodyTemplate;
}

inline b2Body* GetBodyPtr(int ID)
{
   return (b2Body*)ID;
}

inline int GetBodyID(b2Body* NewBody)
{
   return (int)NewBody;
}

DLLCMDC dbfloat GetBodyX(int BodyID)
{
   return dbpfloat(GetBodyPtr(BodyID)->m_position.x);
}
DLLCMDC dbfloat GetBodyY(int BodyID)
{
   return dbpfloat(GetBodyPtr(BodyID)->m_position.y);
}
DLLCMDC dbfloat GetBodyAngle(int BodyID)
{
   return dbpfloat(GetBodyPtr(BodyID)->m_rotation);
}
DLLCMDC void PositionBody(int BodyID, float x, float y)
{
   GetBodyPtr(BodyID)->m_position.Set(x,y);
}
DLLCMDC void XPositionBody(int BodyID, float x)
{
   GetBodyPtr(BodyID)->m_position.x = x;
}
DLLCMDC void YPositionBody(int BodyID, float y)
{
   GetBodyPtr(BodyID)->m_position.y = y;
}
DLLCMDC void RotateBody(int BodyID, float ang)
{
   GetBodyPtr(BodyID)->m_rotation = ang;
}
DLLCMDC void Step2(int ID)
{
   GetWorldPtr(ID)->Step(DefaultTimeStep, 10);
}
DLLCMDC void Step1(int ID, float TimeStep)
{
   GetWorldPtr(ID)->Step(TimeStep, 10);
}
DLLCMDC void Step(int ID, float TimeStep, int Iterations)
{
   GetWorldPtr(ID)->Step(TimeStep, Iterations);
}
DLLCMDC int CreateBody(int ID, int BodyTemplateID)
{
   b2Body* NewBody = GetWorldPtr(ID)->CreateBody(GetBodyTemplatePtr(BodyTemplateID));
   return GetBodyID(NewBody);
}
DLLCMDC void DeleteBody(int ID, int BodyID)
{
   GetWorldPtr(ID)->DestroyBody(GetBodyPtr(BodyID));
}
DLLCMDC int CreateBodyTemplate()
{
   b2BodyDef* NewBodyTemplate = new b2BodyDef();
   return GetBodyTemplateID(NewBodyTemplate);
}
DLLCMDC void AddShapeToBodyTemplate(int BodyTemplateID, int ShapeID, float x, float y, float ang)
{
   b2ShapeDef* NewShape = GetShapePtr(ShapeID);
   NewShape->localPosition.Set(x,y);
   NewShape->localRotation = ang;
   GetBodyTemplatePtr(BodyTemplateID)->AddShape(NewShape);
}
DLLCMDC void AddShapeToBodyTemplate1(int BodyTemplateID, int ShapeID, float x, float y, float ang, float density)
{
   b2ShapeDef* NewShape = GetShapePtr(ShapeID);
   NewShape->localPosition.Set(x,y);
   NewShape->localRotation = ang;
   NewShape->density = density;
   GetBodyTemplatePtr(BodyTemplateID)->AddShape(NewShape);
}
DLLCMDC int CreateCircleShape(float Radius)
{
   b2CircleDef* NewShape = new b2CircleDef();
   NewShape->radius = Radius;
   NewShape->groupIndex = 1;
   return GetShapeID(NewShape);
}

DLLCMDC int CreateBoxShape(float Width, float Height)
{
   b2BoxDef* NewShape = new b2BoxDef();
   NewShape->extents.Set(Width*0.5f, Height*0.5f);
   NewShape->groupIndex = 1;
   return GetShapeID(NewShape);
}
DLLCMDC int CreatePolygonShape(int VertexCount, float x1, float y1, float x2, float y2, float x3, float y3, float x4 = 0.0f, float y4 = 0.0f, float x5 = 0.0f, float y5 = 0.0f, float x6 = 0.0f, float y6 = 0.0f, float x7 = 0.0f, float y7 = 0.0f, float x8 = 0.0f, float y8 = 0.0f)
{
   b2PolyDef* NewShape = new b2PolyDef();
   NewShape->vertexCount = VertexCount;
   NewShape->vertices[0].Set(x1,y1);
   NewShape->vertices[1].Set(x2,y2);
   NewShape->vertices[2].Set(x3,y3);
   NewShape->vertices[3].Set(x4,y4);
   NewShape->vertices[4].Set(x5,y5);
   NewShape->vertices[5].Set(x6,y6);
   NewShape->vertices[6].Set(x7,y7);
   NewShape->vertices[7].Set(x8,y8);
   NewShape->groupIndex = 1;
   return GetShapeID(NewShape);
}
DLLCMDC int CreatePolygonShape8(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x5, float y5, float x6, float y6, float x7, float y7, float x8, float y8)
{
   return CreatePolygonShape(8,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8);
}
DLLCMDC int CreatePolygonShape7(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x5, float y5, float x6, float y6, float x7, float y7)
{
   return CreatePolygonShape(7,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7);
}
DLLCMDC int CreatePolygonShape6(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x5, float y5, float x6, float y6)
{
   return CreatePolygonShape(6,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6);
}
DLLCMDC int CreatePolygonShape5(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float x5, float y5)
{
   return CreatePolygonShape(5,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
}
DLLCMDC int CreatePolygonShape4(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{
   return CreatePolygonShape(4,x1,y1,x2,y2,x3,y3,x4,y4);
}
DLLCMDC int CreatePolygonShape3(float x1, float y1, float x2, float y2, float x3, float y3)
{
   return CreatePolygonShape(3,x1,y1,x2,y2,x3,y3);
}
DLLCMDC void DeleteShape(int ID)
{
   delete GetShapePtr(ID);
}

DLLCMDC void SetWorldGravity(int ID, float GravX, float GravY)
{
   GetWorldPtr(ID)->m_gravity.Set(GravX, GravY);
}

DLLCMDC dbfloat GetWorldGravityX(int ID)
{
   return dbpfloat(GetWorldPtr(ID)->m_gravity.x);
}

DLLCMDC dbfloat GetWorldGravityY(int ID)
{
   return dbpfloat(GetWorldPtr(ID)->m_gravity.y);
}

DLLCMDC void SetWorldSleep(int ID, bool doSleep)
{
   GetWorldPtr(ID)->m_doSleep = doSleep;
}

DLLCMDC int GetWorldSleep(int ID)
{
   return GetWorldPtr(ID)->m_doSleep;
}

DLLCMDC int CreateWorld(float Left, float Top, float Right, float Bottom, float GravX, float GravY, bool doSleep)
{
   b2AABB worldAABB;
   worldAABB.minVertex.Set(Left, Top);
   worldAABB.maxVertex.Set(Right, Bottom);
   b2Vec2 gravity(GravX, GravY);
   b2World* NewWorld = new b2World(worldAABB, gravity, doSleep);
   return GetWorldID(NewWorld);
}

DLLCMDC void DeleteWorld(int ID)
{
   delete GetWorldPtr(ID);
}


Because this is a plugin, you will notice it does some rather odd things (like converting a float to a DWORD). This is just because of the way darkbasic handles plugins, and I'm quite sure that is not the problem.

The darkbasic code I used was this:
Code:
sync on

myworld = b2CreateWorld(-1000,-1000,1000,1000,0,10,1)

myfloorshape = b2CreateBoxShape(1000,50)
myfloortemplate = b2CreateBodyTemplate()
b2AddShapeToBodyTemplate myfloortemplate,myfloorshape,0,0,0,0
myfloor = b2CreateBody(myworld, myfloortemplate)
b2PositionBody myfloor,512,600

mycircleshape = b2CreateCircleShape(20)
mycircletemplate = b2CreateBodyTemplate()
b2AddShapeToBodyTemplate mycircletemplate,mycircleshape,0,0,0,1
mycircle = b2CreateBody(myworld, mycircletemplate)
b2PositionBody mycircle,512,100

do
    b2Step myworld
    d3d_box 12,575,1012,625,rgb(0,0,255)
    d3d_circle b2GetBodyX(mycircle),b2GetBodyY(mycircle),20,1,rgb(255,0,0)
    sync
    cls 0
loop


Which equates to this C++ code:
Code:
int myworld = CreateWorld(-1000,-1000,1000,1000,0,10,1);

int myfloorshape = CreateBoxShape(1000,50);
myfloortemplate = CreateBodyTemplate();
AddShapeToBodyTemplate1(myfloortemplate,myfloorshape,0,0,0,0);
int myfloor = CreateBody(myworld, myfloortemplate);
PositionBody(myfloor,512,600);

int mycircleshape = CreateCircleShape(20);
int mycircletemplate = CreateBodyTemplate();
AddShapeToBodyTemplate1(mycircletemplate,mycircleshape,0,0,0,1);
int mycircle = CreateBody(myworld, mycircletemplate);
PositionBody(mycircle,512,100);

do
{
    Step2(myworld);
    <Draw stuff>
}


When displayed, it shows the circle affected by gravity as it should be, but then it falls straight through the static floor body 😢
The main problem is that your ground box extends outside the world. The current behavior is that something that extends outside of the world get's no collisions.

Also you shouldn't set the body position directly, because this doesn't synchronize the shapes.

I think I will change the broad phase behavior so that it only prevents collisions for objects completely outside of the world.
How else can I position the object before the simulation begins / during the simulation? 😕
Also, after extending the bounds, there is still no collision between the objects 😢
You really should set the initial position in b2BodyDef. After that you can move bodies with b2Body::SetOriginPosition and b2Body::SetCenterPosition.

I set the bounding box to -2000,-2000,2000,2000 and it worked. What is your time step? I used 1/60.

Original thread: https://box2d.org/forum/viewtopic.php?t=27