Hello,
this is my first time writing a game and first time using a physics engine. I thought I'll write a simple arkanoid-clone just to gain some experience with box2d before moving onto something bigger.
In the game there is a ball bouncing off everything (0 gravity) and a "stick" with which you save the ball from falling into the abyss at the bottom. The ball bounces great but I have a problem with the stick. It appears that if it has constant linear velocity applied it sometimes passes through the walls or let's the ball through. What am I doing wrong? My code: (the stick is "palbody", the last definition)
Then I render to screen with something like that (Using Simple directmedia layer):
Code for moving the stick:
Please help... I can make a video of what exactly is happening or send you the compiled code (only for mac os x I'm afraid...)
this is my first time writing a game and first time using a physics engine. I thought I'll write a simple arkanoid-clone just to gain some experience with box2d before moving onto something bigger.
In the game there is a ball bouncing off everything (0 gravity) and a "stick" with which you save the ball from falling into the abyss at the bottom. The ball bounces great but I have a problem with the stick. It appears that if it has constant linear velocity applied it sometimes passes through the walls or let's the ball through. What am I doing wrong? My code: (the stick is "palbody", the last definition)
Code:
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity(0.0f, 0.0f);
bool doSleep = false;
b2World world(worldAABB, gravity, doSleep);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(3.2f, 4.78f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(3.2f, 0.1f);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef groundBodyDef2;
groundBodyDef2.position.Set(3.2f, 0.0f);
b2Body* groundBody2 = world.CreateBody(&groundBodyDef2);
b2PolygonDef groundShapeDef2;
groundShapeDef2.SetAsBox(3.2f, 0.1f);
groundBody2->CreateShape(&groundShapeDef2);
//Wall left
b2BodyDef wallDef1;
wallDef1.position.Set(0.0f, 2.4f);
b2Body* wallBody1 = world.CreateBody(&wallDef1);
b2PolygonDef wallShapeDef1;
wallShapeDef1.SetAsBox(0.1f, 2.4f);
wallBody1->CreateShape(&wallShapeDef1);
//Wall right
b2BodyDef wallDef2;
wallDef2.position.Set(6.4f, 2.4f);
b2Body* wallBody2 = world.CreateBody(&wallDef2);
b2PolygonDef wallShapeDef2;
wallShapeDef2.SetAsBox(0.1f, 2.4f);
wallBody2->CreateShape(&wallShapeDef2);
b2BodyDef bodyDef;
bodyDef.position.Set(1.0f, 1.0f);
bodyDef.isBullet=true;
b2Body* body = world.CreateBody(&bodyDef);
b2CircleDef shapeDef;
shapeDef.radius=0.05f;
shapeDef.density = 0.1f;
shapeDef.friction = 0.0f;
shapeDef.restitution = 1.0f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
b2BodyDef palbodyDef;
palbodyDef.position.Set(4.0f, 2.0f);
b2Body* palbody = world.CreateBody(&palbodyDef);
b2PolygonDef palshapeDef;
palshapeDef.SetAsBox(1.0f,0.05f);
palshapeDef.density = 1.0f;
palshapeDef.friction = 0.0f;
palshapeDef.restitution = 0.0f;
palbody->CreateShape(&palshapeDef);
palbody->SetMassFromShapes();
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity(0.0f, 0.0f);
bool doSleep = false;
b2World world(worldAABB, gravity, doSleep);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(3.2f, 4.78f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(3.2f, 0.1f);
groundBody->CreateShape(&groundShapeDef);
b2BodyDef groundBodyDef2;
groundBodyDef2.position.Set(3.2f, 0.0f);
b2Body* groundBody2 = world.CreateBody(&groundBodyDef2);
b2PolygonDef groundShapeDef2;
groundShapeDef2.SetAsBox(3.2f, 0.1f);
groundBody2->CreateShape(&groundShapeDef2);
//Wall left
b2BodyDef wallDef1;
wallDef1.position.Set(0.0f, 2.4f);
b2Body* wallBody1 = world.CreateBody(&wallDef1);
b2PolygonDef wallShapeDef1;
wallShapeDef1.SetAsBox(0.1f, 2.4f);
wallBody1->CreateShape(&wallShapeDef1);
//Wall right
b2BodyDef wallDef2;
wallDef2.position.Set(6.4f, 2.4f);
b2Body* wallBody2 = world.CreateBody(&wallDef2);
b2PolygonDef wallShapeDef2;
wallShapeDef2.SetAsBox(0.1f, 2.4f);
wallBody2->CreateShape(&wallShapeDef2);
b2BodyDef bodyDef;
bodyDef.position.Set(1.0f, 1.0f);
bodyDef.isBullet=true;
b2Body* body = world.CreateBody(&bodyDef);
b2CircleDef shapeDef;
shapeDef.radius=0.05f;
shapeDef.density = 0.1f;
shapeDef.friction = 0.0f;
shapeDef.restitution = 1.0f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
b2BodyDef palbodyDef;
palbodyDef.position.Set(4.0f, 2.0f);
b2Body* palbody = world.CreateBody(&palbodyDef);
b2PolygonDef palshapeDef;
palshapeDef.SetAsBox(1.0f,0.05f);
palshapeDef.density = 1.0f;
palshapeDef.friction = 0.0f;
palshapeDef.restitution = 0.0f;
palbody->CreateShape(&palshapeDef);
palbody->SetMassFromShapes();
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
Then I render to screen with something like that (Using Simple directmedia layer):
Code:
world.Step(timeStep, iterations);
b2Vec2 ballposition = body->GetPosition();
b2Vec2 palposition = palbody->GetPosition();
apply_surface( 0, 0, background, screen );
apply_surface( ballposition.x*100, ballposition.y*100, foo, screen );
apply_surface( (palposition.x*100)-100, (palposition.y*100)-5, paletka, screen );
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
b2Vec2 ballposition = body->GetPosition();
b2Vec2 palposition = palbody->GetPosition();
apply_surface( 0, 0, background, screen );
apply_surface( ballposition.x*100, ballposition.y*100, foo, screen );
apply_surface( (palposition.x*100)-100, (palposition.y*100)-5, paletka, screen );
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
Code for moving the stick:
Code:
case SDLK_LEFT:
palbody->SetLinearVelocity(b2Vec2(-1.8f,palbody->GetLinearVelocity().y)); break;
palbody->SetLinearVelocity(b2Vec2(-1.8f,palbody->GetLinearVelocity().y)); break;
Please help... I can make a video of what exactly is happening or send you the compiled code (only for mac os x I'm afraid...)