Hi I'm currently using Box2D version 2.01 for my game engine. I recently changed the way my players move in a realtime isometric simulation. They used to have fixed rotation and animate their rotation based on velocity alone. Now to move to a destination they use an "action" which involves invoking 3 other actions as needed. "FaceDestination" simply calls SetAngle() to face the destination. "Accellerate" makes them accelerate in the direction they are facing. "Brake" attempts to counteract existing velocity.
I noticed my players would start to move in weird semi circles trying to get to their destination. After a lot of debugging efforts I added code to render short lines displaying the result of GetAngle() in red during my render function, a green line for the result of GetAngle() in my logic function, and a blue line from the object to its destination. From this I identified the problem was indeed with GetAngle.
In my main update I run b2World::step() then all of my game objects run their logic. It is at this point that my objects "accelerate" action calls GetAngle() and gets some weird results that seem to veer further and further off the faster the object is going and at higher speeds starts to flicker between two or more different locations. Yet later in my main rendering function calling GetAngle() returns correct data. Also I experimented with moving the physics step() to after I run logic on my game objects and started getting intermittantly bad GetAngle() data in both rendering and the accelerate action called in object logic code. It seems to me like calling GetAngle() to soon after a step is throwing the bad data. I'm not sure why timing would matter though or why it hasn't been noticed in other projects.
I played around with different shapes, sizes, densites, step intervals, step iterations, and nothing even slightly improved the problem. Any ideas? I can post some source, but I have to rip chunks out of context because my engine code has grown very long and complex.
I noticed my players would start to move in weird semi circles trying to get to their destination. After a lot of debugging efforts I added code to render short lines displaying the result of GetAngle() in red during my render function, a green line for the result of GetAngle() in my logic function, and a blue line from the object to its destination. From this I identified the problem was indeed with GetAngle.
In my main update I run b2World::step() then all of my game objects run their logic. It is at this point that my objects "accelerate" action calls GetAngle() and gets some weird results that seem to veer further and further off the faster the object is going and at higher speeds starts to flicker between two or more different locations. Yet later in my main rendering function calling GetAngle() returns correct data. Also I experimented with moving the physics step() to after I run logic on my game objects and started getting intermittantly bad GetAngle() data in both rendering and the accelerate action called in object logic code. It seems to me like calling GetAngle() to soon after a step is throwing the bad data. I'm not sure why timing would matter though or why it hasn't been noticed in other projects.
I played around with different shapes, sizes, densites, step intervals, step iterations, and nothing even slightly improved the problem. Any ideas? I can post some source, but I have to rip chunks out of context because my engine code has grown very long and complex.
Code:
void Accelerate::doAction( float deltaTime )
{
.....
//normal way: accelerate in the direction the body is facing
// why is this returning bad data???
float32 angle = user->body->getAngle();
Vector2 direction = angleToVector( angle );
//direction *= force;
//direction *= 10.0f;
//string str = user->getName() +" action angleToVector = "+ stringify(direction.x)+","+ stringify(direction.y);
string str = user->getName() +" action angle = "+ stringify(angle);
hgeCreate(HGE_VERSION)->System_Log(str.c_str());
// Debug tool
if (globalCam)
{
hgeCreate(HGE_VERSION)->Gfx_BeginScene();
hgeCreate(HGE_VERSION)->Gfx_RenderLine(globalCam->screenX(user->body->getPosition().x), globalCam->screenY(user->body->getPosition().y),
globalCam->screenX(user->body->getPosition().x + direction.x), globalCam->screenY(user->body->getPosition().y + direction.y), ARGB(180,0,255,0));
hgeCreate(HGE_VERSION)->Gfx_EndScene();
}
user->body->body->ApplyForce(direction,Vector2(0,0));
{
.....
//normal way: accelerate in the direction the body is facing
// why is this returning bad data???
float32 angle = user->body->getAngle();
Vector2 direction = angleToVector( angle );
//direction *= force;
//direction *= 10.0f;
//string str = user->getName() +" action angleToVector = "+ stringify(direction.x)+","+ stringify(direction.y);
string str = user->getName() +" action angle = "+ stringify(angle);
hgeCreate(HGE_VERSION)->System_Log(str.c_str());
// Debug tool
if (globalCam)
{
hgeCreate(HGE_VERSION)->Gfx_BeginScene();
hgeCreate(HGE_VERSION)->Gfx_RenderLine(globalCam->screenX(user->body->getPosition().x), globalCam->screenY(user->body->getPosition().y),
globalCam->screenX(user->body->getPosition().x + direction.x), globalCam->screenY(user->body->getPosition().y + direction.y), ARGB(180,0,255,0));
hgeCreate(HGE_VERSION)->Gfx_EndScene();
}
user->body->body->ApplyForce(direction,Vector2(0,0));
Code:
// just updates the animation's frame in the engine
void Creature::RenderEx( Camera* cam )
{
.....
if ( Physics::get()->drawEnabled )
{
if ( states["destination"] )
{
Vector2 dest = ( (State::Destination*) states["destination"] )->destination;
hge->Gfx_RenderLine(cam->screenX(body->getPosition().x), cam->screenY(body->getPosition().y), cam->screenX(dest.x), cam->screenY(dest.y), ARGB(100,0,0,255));
}
Vector2 direction = angleToVector(body->getAngle());
//string str = getName() +" creature render angleToVector = "+ stringify(direction.x)+","+ stringify(direction.y);
string str = getName() +" creature render angle = "+ stringify(body->getAngle());
hgeCreate(HGE_VERSION)->System_Log(str.c_str());
direction+= body->getPosition();
hge->Gfx_RenderLine(cam->screenX(body->getPosition().x), cam->screenY(body->getPosition().y), cam->screenX(direction.x), cam->screenY(direction.y), ARGB(100,255,0,0));
}
}
void Creature::RenderEx( Camera* cam )
{
.....
if ( Physics::get()->drawEnabled )
{
if ( states["destination"] )
{
Vector2 dest = ( (State::Destination*) states["destination"] )->destination;
hge->Gfx_RenderLine(cam->screenX(body->getPosition().x), cam->screenY(body->getPosition().y), cam->screenX(dest.x), cam->screenY(dest.y), ARGB(100,0,0,255));
}
Vector2 direction = angleToVector(body->getAngle());
//string str = getName() +" creature render angleToVector = "+ stringify(direction.x)+","+ stringify(direction.y);
string str = getName() +" creature render angle = "+ stringify(body->getAngle());
hgeCreate(HGE_VERSION)->System_Log(str.c_str());
direction+= body->getPosition();
hge->Gfx_RenderLine(cam->screenX(body->getPosition().x), cam->screenY(body->getPosition().y), cam->screenX(direction.x), cam->screenY(direction.y), ARGB(100,255,0,0));
}
}
Code:
bool SimulationState::update(float dTime)
{
//IMPORTANT! -- half of the update is in the parent class
if (Parent::update(dTime))
return true;
//PHYSICS
//static float frameLength = 1.0f/60.0f;
//static float physicsTime = 0;
//physicsTime += dTime;
//if(physicsTime > frameLength)
//{
// getWorld()->Step(frameLength, 5);
// physicsTime-= frameLength;
//}
//PHYSICS
getWorld()->Step(dTime, 8);
//!!! UPDATE ALL SIMS IN SIMULATION !!!
root.update(dTime);
// Continue execution
return false;
}
{
//IMPORTANT! -- half of the update is in the parent class
if (Parent::update(dTime))
return true;
//PHYSICS
//static float frameLength = 1.0f/60.0f;
//static float physicsTime = 0;
//physicsTime += dTime;
//if(physicsTime > frameLength)
//{
// getWorld()->Step(frameLength, 5);
// physicsTime-= frameLength;
//}
//PHYSICS
getWorld()->Step(dTime, 8);
//!!! UPDATE ALL SIMS IN SIMULATION !!!
root.update(dTime);
// Continue execution
return false;
}
Code:
void SimulationState::render()
{
// Now we let the Gui Object perform its logic.
gui->logic();
// Next we begin a scene.
hge->Gfx_BeginScene();
hge->Gfx_Clear(0);
//if (&quad)
//{
// hge->Gfx_RenderQuad(&quad);
//}
//else throw GCN_EXCEPTION("terrain quad is missing!");
for( size_t x = 0; x < background.size(); x++ )
background[x]->render(*cam1);
Objects->RenderEx(cam1);
Physics::get()->draw();
// Now we let the Gui Object draw itself.
gui->draw();
// Finally we end the scene causing the screen to be updated.
hge->Gfx_EndScene();
//hge->System_Log("render");
//return false;
}
{
// Now we let the Gui Object perform its logic.
gui->logic();
// Next we begin a scene.
hge->Gfx_BeginScene();
hge->Gfx_Clear(0);
//if (&quad)
//{
// hge->Gfx_RenderQuad(&quad);
//}
//else throw GCN_EXCEPTION("terrain quad is missing!");
for( size_t x = 0; x < background.size(); x++ )
background[x]->render(*cam1);
Objects->RenderEx(cam1);
Physics::get()->draw();
// Now we let the Gui Object draw itself.
gui->draw();
// Finally we end the scene causing the screen to be updated.
hge->Gfx_EndScene();
//hge->System_Log("render");
//return false;
}
