Hi...I just downloaded Box2D for the first time today..and i can't get this to work right.
I an trying to set this up in a way that I can use it in a top down driving game.
I am having two main problems right now. First, things are not drawing in the right place. I couldn't find a detailed enough specifications about how bodies and fixtures are defined in the documentation. Because of this, I can't really tell if things are moving correctly in my scene.
Also, for some reason, when I set the linear velocity of the player body, it moves really slow until it hits something..then it bounces off at an insane speed..not sure why =/
any help with this would be appreciated.
I an trying to set this up in a way that I can use it in a top down driving game.
I am having two main problems right now. First, things are not drawing in the right place. I couldn't find a detailed enough specifications about how bodies and fixtures are defined in the documentation. Because of this, I can't really tell if things are moving correctly in my scene.
Also, for some reason, when I set the linear velocity of the player body, it moves really slow until it hits something..then it bounces off at an insane speed..not sure why =/
any help with this would be appreciated.
Code:
#include "main.h"
#include <vector>
using std::vector;
// used to cap the framerate to 30 frames per second
unsigned int lastFlip = 0;
unsigned int FPS = 60;
// 1000 milliseconds divided by the desired frames per second
unsigned int frameTime = 1000 / FPS;
/////////////////// BOX 2D ///////////////////
int iterations = 10;
b2Vec2 gravity(0.0f, 0.0f); // TOP DOWN: no gravity
float maxPlayerSpeed = 50.0f;
float secToTopSpeed = 5.0f;
b2World world(gravity, true);
vector<b2Body*> bodies;
b2Body *player;
//////////////////////////////////////////////
void AddStaticBody(b2Vec2 position, b2Vec2 &width, float friction, float angle)
{
b2BodyDef bodyDef;
bodyDef.position.Set(position.x, position.y);
b2Body *body = world.CreateBody(&bodyDef);
b2PolygonShape box;
box.SetAsBox(width.x / 2, width.y / 2);
body->CreateFixture(&box, 0.0f);
bodies.push_back(body);
}
void DrawBody(SDL_Surface *dest, b2Body* body)
{
const b2Transform& transform = body->GetTransform();
for (b2Fixture* f = body->GetFixtureList(); f != NULL; f = f->GetNext())
{
b2PolygonShape *box = (b2PolygonShape*)f->GetShape();
b2Vec2 v1 = b2Mul(transform, box->GetVertex(0));
b2Vec2 v2 = b2Mul(transform, box->GetVertex(1));
b2Vec2 v3 = b2Mul(transform, box->GetVertex(2));
b2Vec2 v4 = b2Mul(transform, box->GetVertex(3));
lineRGBA(dest, (short)v1.x, (short)v1.y, (short)v2.x, (short)v2.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v2.x, (short)v2.y, (short)v3.x, (short)v3.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v3.x, (short)v3.y, (short)v4.x, (short)v4.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v4.x, (short)v4.y, (short)v1.x, (short)v1.y, 255, 0, 0, 255);
}
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
// show a standard windows message box to notify of failure, and end the program
MessageBox(0, "Failed to Initialize SDL", "Errror", MB_OK | MB_ICONERROR);
return -1;
}
// create an SDL window
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
if(screen == NULL)
{
MessageBox(0, "Failed to create an SDL window", "Errror", MB_OK | MB_ICONERROR);
return -1;
}
SDL_WM_SetCaption("Box2D Demo", NULL);
////////////////////////// INIT BOX2D ////////////////////////////
b2BodyDef playerBodyDef;
playerBodyDef.type = b2_dynamicBody;
playerBodyDef.position.Set(200.0f, 200.0f);
player = world.CreateBody(&playerBodyDef);
b2PolygonShape box;
box.SetAsBox(25, 50);
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.density = 0.000001f;
fixtureDef.friction = 0.2f;
player->CreateFixture(&fixtureDef);
player->SetLinearDamping(5.0f);
player->SetAngularDamping(1.0f);
//player->SetSleepingAllowed(false);
// void AddStaticBody(b2Vec2 position, b2Vec2 &width, float friction, float angle)
AddStaticBody(b2Vec2(400, 100), b2Vec2(100, 75), 0.2f, 0.0f);
AddStaticBody(b2Vec2(380, 210), b2Vec2(75, 100), 0.2f, 0.0f);
AddStaticBody(b2Vec2(410, 320), b2Vec2(75, 75), 0.2f, 0.0f);
AddStaticBody(b2Vec2(460, 430), b2Vec2(100, 100), 0.2f, 0.0f);
AddStaticBody(b2Vec2(350, 360), b2Vec2(75, 100), 0.2f, 0.0f);
//////////////////////////////////////////////////////////////////
// set this to true to end the game
float time = 0.0f;
lastFlip = SDL_GetTicks();
b2Vec2 velocity;
bool gameDone = false;
while(gameDone == false)
{
// if enough time has passed, render the next frame
if(SDL_GetTicks() - lastFlip >= frameTime)
{
// calculate time
float elapsed = (float)(SDL_GetTicks() - lastFlip) / 1000.0f;
time += elapsed;
lastFlip = SDL_GetTicks();
// move player
SDL_PumpEvents();
Uint8 *keys = SDL_GetKeyState(NULL);
b2Vec2 input(0, 0);
if(keys[SDLK_LEFT]) input.x -= 1.0f;
if(keys[SDLK_RIGHT]) input.x += 1.0f;
if(keys[SDLK_UP]) input.y -= 1.0f;
if(keys[SDLK_DOWN]) input.y += 1.0f;
float length = input.Length();
if(length > 0)
{
float force = (maxPlayerSpeed / secToTopSpeed) * elapsed;
input.Normalize();
input *= force;
player->ApplyLinearImpulse(input, player->GetPosition());
}
// update world
world.Step(elapsed, 10, 10);
// clear screen
SDL_FillRect(screen, 0, 0);
// draw world
for (b2Body* b = world.GetBodyList(); b; b = b->GetNext())
{
DrawBody(screen, b);
}
// redraw
SDL_Flip(screen);
SDL_Event evt;
while(SDL_PollEvent(&evt)){
switch(evt.type){
case SDL_QUIT:
gameDone = true;
break;
}
}
}
}
SDL_Quit();
return 0;
}
#include <vector>
using std::vector;
// used to cap the framerate to 30 frames per second
unsigned int lastFlip = 0;
unsigned int FPS = 60;
// 1000 milliseconds divided by the desired frames per second
unsigned int frameTime = 1000 / FPS;
/////////////////// BOX 2D ///////////////////
int iterations = 10;
b2Vec2 gravity(0.0f, 0.0f); // TOP DOWN: no gravity
float maxPlayerSpeed = 50.0f;
float secToTopSpeed = 5.0f;
b2World world(gravity, true);
vector<b2Body*> bodies;
b2Body *player;
//////////////////////////////////////////////
void AddStaticBody(b2Vec2 position, b2Vec2 &width, float friction, float angle)
{
b2BodyDef bodyDef;
bodyDef.position.Set(position.x, position.y);
b2Body *body = world.CreateBody(&bodyDef);
b2PolygonShape box;
box.SetAsBox(width.x / 2, width.y / 2);
body->CreateFixture(&box, 0.0f);
bodies.push_back(body);
}
void DrawBody(SDL_Surface *dest, b2Body* body)
{
const b2Transform& transform = body->GetTransform();
for (b2Fixture* f = body->GetFixtureList(); f != NULL; f = f->GetNext())
{
b2PolygonShape *box = (b2PolygonShape*)f->GetShape();
b2Vec2 v1 = b2Mul(transform, box->GetVertex(0));
b2Vec2 v2 = b2Mul(transform, box->GetVertex(1));
b2Vec2 v3 = b2Mul(transform, box->GetVertex(2));
b2Vec2 v4 = b2Mul(transform, box->GetVertex(3));
lineRGBA(dest, (short)v1.x, (short)v1.y, (short)v2.x, (short)v2.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v2.x, (short)v2.y, (short)v3.x, (short)v3.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v3.x, (short)v3.y, (short)v4.x, (short)v4.y, 255, 0, 0, 255);
lineRGBA(dest, (short)v4.x, (short)v4.y, (short)v1.x, (short)v1.y, 255, 0, 0, 255);
}
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
// show a standard windows message box to notify of failure, and end the program
MessageBox(0, "Failed to Initialize SDL", "Errror", MB_OK | MB_ICONERROR);
return -1;
}
// create an SDL window
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
if(screen == NULL)
{
MessageBox(0, "Failed to create an SDL window", "Errror", MB_OK | MB_ICONERROR);
return -1;
}
SDL_WM_SetCaption("Box2D Demo", NULL);
////////////////////////// INIT BOX2D ////////////////////////////
b2BodyDef playerBodyDef;
playerBodyDef.type = b2_dynamicBody;
playerBodyDef.position.Set(200.0f, 200.0f);
player = world.CreateBody(&playerBodyDef);
b2PolygonShape box;
box.SetAsBox(25, 50);
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.density = 0.000001f;
fixtureDef.friction = 0.2f;
player->CreateFixture(&fixtureDef);
player->SetLinearDamping(5.0f);
player->SetAngularDamping(1.0f);
//player->SetSleepingAllowed(false);
// void AddStaticBody(b2Vec2 position, b2Vec2 &width, float friction, float angle)
AddStaticBody(b2Vec2(400, 100), b2Vec2(100, 75), 0.2f, 0.0f);
AddStaticBody(b2Vec2(380, 210), b2Vec2(75, 100), 0.2f, 0.0f);
AddStaticBody(b2Vec2(410, 320), b2Vec2(75, 75), 0.2f, 0.0f);
AddStaticBody(b2Vec2(460, 430), b2Vec2(100, 100), 0.2f, 0.0f);
AddStaticBody(b2Vec2(350, 360), b2Vec2(75, 100), 0.2f, 0.0f);
//////////////////////////////////////////////////////////////////
// set this to true to end the game
float time = 0.0f;
lastFlip = SDL_GetTicks();
b2Vec2 velocity;
bool gameDone = false;
while(gameDone == false)
{
// if enough time has passed, render the next frame
if(SDL_GetTicks() - lastFlip >= frameTime)
{
// calculate time
float elapsed = (float)(SDL_GetTicks() - lastFlip) / 1000.0f;
time += elapsed;
lastFlip = SDL_GetTicks();
// move player
SDL_PumpEvents();
Uint8 *keys = SDL_GetKeyState(NULL);
b2Vec2 input(0, 0);
if(keys[SDLK_LEFT]) input.x -= 1.0f;
if(keys[SDLK_RIGHT]) input.x += 1.0f;
if(keys[SDLK_UP]) input.y -= 1.0f;
if(keys[SDLK_DOWN]) input.y += 1.0f;
float length = input.Length();
if(length > 0)
{
float force = (maxPlayerSpeed / secToTopSpeed) * elapsed;
input.Normalize();
input *= force;
player->ApplyLinearImpulse(input, player->GetPosition());
}
// update world
world.Step(elapsed, 10, 10);
// clear screen
SDL_FillRect(screen, 0, 0);
// draw world
for (b2Body* b = world.GetBodyList(); b; b = b->GetNext())
{
DrawBody(screen, b);
}
// redraw
SDL_Flip(screen);
SDL_Event evt;
while(SDL_PollEvent(&evt)){
switch(evt.type){
case SDL_QUIT:
gameDone = true;
break;
}
}
}
}
SDL_Quit();
return 0;
}