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
😢