Get strange behavior when I try to setup my own project using box2d. When I use Box2d TestBed everything is ok, but in my own environment, when box falls on to the "ground" it does not rotate.
I use MSVC 7.1 for my own project and MSVC8 Express Edition for testbed (because it fails to start when compiled with MSVC 7.1)
I use MSVC 7.1 for my own project and MSVC8 Express Edition for testbed (because it fails to start when compiled with MSVC 7.1)
Code:
#include "hge/include/hge.h" // http://hge.relishgames.com
#include "Box2D/Include/box2d.h"
HGE *hge = 0;
b2World * world = 0;
float const timeStep = 1/60.f;
int const iterationCount = 10;
void InitWorld()
{
// the same code works good in TestBed
{
b2BoxDef boxDef;
boxDef.extents.Set(1.f, 1.f);
boxDef.density = 0.6f;
boxDef.friction = 0.3f;
b2BodyDef bodyDef;
bodyDef.rotation = 0.3f;
bodyDef.AddShape(&boxDef);
world->CreateBody(&bodyDef);
}
{
b2BoxDef boxDef;
boxDef.extents.Set(10.f, 1.f);
boxDef.density = 0.0f;
boxDef.friction = 0.3f;
boxDef.localRotation = 0.3f;
b2BodyDef bodyDef;
bodyDef.position.Set(0.f, -3.f);
bodyDef.AddShape(&boxDef);
world->CreateBody(&bodyDef);
}
}
bool FrameFunc()
{
if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
static float time = hge->Timer_GetTime();
float currentTime = hge->Timer_GetTime();
while(currentTime - time > timeStep)
{
world->Step(timeStep, iterationCount);
time += timeStep;
}
hge->Gfx_SetTransform(0, 0, 400, 300, 0, 5.f, -5.f);
hge->Gfx_BeginScene();
hge->Gfx_Clear(0);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for (b2Shape* s = b->GetShapeList(); s; s = s->GetNext())
{
switch(s->GetType())
{
case e_polyShape:
{
b2Vec2 pos = s->GetPosition();
b2Mat22 rot = s->GetRotationMatrix();
b2PolyShape* poly = (b2PolyShape*)s;
if(poly->m_vertexCount == 4)
{
hgeQuad q = {0};
q.blend = BLEND_DEFAULT;
for(int i = 0; i < poly->m_vertexCount; ++i)
{
b2Vec2 vPos = pos + rot.Solve(poly->m_vertices[i]);
q.v[i].x = vPos.x;
q.v[i].y = vPos.y;
q.v[i].col = ARGB(0xff, 0xff, 0xff, 0xff);
}
hge->Gfx_RenderQuad(&q);
}
}
break;
}
}
}
hge->Gfx_EndScene();
return false;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
hge = hgeCreate(HGE_VERSION);
hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
hge->System_SetState(HGE_TITLE, "");
hge->System_SetState(HGE_WINDOWED, true);
hge->System_SetState(HGE_USESOUND, false);
hge->System_SetState(HGE_SHOWSPLASH, false);
if(hge->System_Initiate())
{
b2AABB aabb;
aabb.maxVertex.Set(-10.f, -10.f);
aabb.maxVertex.Set(10.f, 10.f);
b2Vec2 gravity(0, -10.f);
world = new b2World(aabb, gravity, true);
InitWorld();
hge->System_Start();
}
else
MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
delete world;
hge->System_Shutdown();
hge->Release();
return 0;
}
#include "Box2D/Include/box2d.h"
HGE *hge = 0;
b2World * world = 0;
float const timeStep = 1/60.f;
int const iterationCount = 10;
void InitWorld()
{
// the same code works good in TestBed
{
b2BoxDef boxDef;
boxDef.extents.Set(1.f, 1.f);
boxDef.density = 0.6f;
boxDef.friction = 0.3f;
b2BodyDef bodyDef;
bodyDef.rotation = 0.3f;
bodyDef.AddShape(&boxDef);
world->CreateBody(&bodyDef);
}
{
b2BoxDef boxDef;
boxDef.extents.Set(10.f, 1.f);
boxDef.density = 0.0f;
boxDef.friction = 0.3f;
boxDef.localRotation = 0.3f;
b2BodyDef bodyDef;
bodyDef.position.Set(0.f, -3.f);
bodyDef.AddShape(&boxDef);
world->CreateBody(&bodyDef);
}
}
bool FrameFunc()
{
if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
static float time = hge->Timer_GetTime();
float currentTime = hge->Timer_GetTime();
while(currentTime - time > timeStep)
{
world->Step(timeStep, iterationCount);
time += timeStep;
}
hge->Gfx_SetTransform(0, 0, 400, 300, 0, 5.f, -5.f);
hge->Gfx_BeginScene();
hge->Gfx_Clear(0);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for (b2Shape* s = b->GetShapeList(); s; s = s->GetNext())
{
switch(s->GetType())
{
case e_polyShape:
{
b2Vec2 pos = s->GetPosition();
b2Mat22 rot = s->GetRotationMatrix();
b2PolyShape* poly = (b2PolyShape*)s;
if(poly->m_vertexCount == 4)
{
hgeQuad q = {0};
q.blend = BLEND_DEFAULT;
for(int i = 0; i < poly->m_vertexCount; ++i)
{
b2Vec2 vPos = pos + rot.Solve(poly->m_vertices[i]);
q.v[i].x = vPos.x;
q.v[i].y = vPos.y;
q.v[i].col = ARGB(0xff, 0xff, 0xff, 0xff);
}
hge->Gfx_RenderQuad(&q);
}
}
break;
}
}
}
hge->Gfx_EndScene();
return false;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
hge = hgeCreate(HGE_VERSION);
hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
hge->System_SetState(HGE_TITLE, "");
hge->System_SetState(HGE_WINDOWED, true);
hge->System_SetState(HGE_USESOUND, false);
hge->System_SetState(HGE_SHOWSPLASH, false);
if(hge->System_Initiate())
{
b2AABB aabb;
aabb.maxVertex.Set(-10.f, -10.f);
aabb.maxVertex.Set(10.f, 10.f);
b2Vec2 gravity(0, -10.f);
world = new b2World(aabb, gravity, true);
InitWorld();
hge->System_Start();
}
else
MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
delete world;
hge->System_Shutdown();
hge->Release();
return 0;
}