Thanks Diggsey my #macros were bad. (Bad macro,bad!)
The rotation was messed up because i didnt know about the dbOffsetSprite command...
Again , thanks... have you tought on making a wrapper of BOX2D ?
You know for physics noobs like me
Code fixed:
#include "DarkGDK.h"
#include "Box2D.h"
#define CUBE 1
#define CUBE2 2
#define CUBE3 3
#define IMAGE_CUBE 1
#define SCREEN_TO_WORLD(n) ((n) / 30.0f)
#define WORLD_TO_SCREEN(n) ((n) * 30.0f)
float fAngle = 0;
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetDisplayMode(800,600,32);
//Make sprites from Cube.png
dbLoadImage("Cube.png",IMAGE_CUBE); //Cube.png size is 100x100
dbSprite(CUBE,0,0,IMAGE_CUBE);
dbSprite(CUBE2,0,0,IMAGE_CUBE);
dbSprite(CUBE3,0,0,IMAGE_CUBE);
dbOffsetSprite(CUBE,dbSpriteWidth(CUBE)/2,dbSpriteHeight(CUBE)/2);
dbOffsetSprite(CUBE2,dbSpriteWidth(CUBE2)/2,dbSpriteHeight(CUBE2)/2);
dbOffsetSprite(CUBE3,dbSpriteWidth(CUBE3)/2,dbSpriteHeight(CUBE3)/2);
dbHideSprite(CUBE);
dbHideSprite(CUBE2);
dbHideSprite(CUBE3);
// Define the size of the world. Simulation will still work
// if bodies reach the end of the world, but it will be slower.
b2AABB worldAABB;
worldAABB.lowerBound.Set(0,0);
worldAABB.upperBound.Set(SCREEN_TO_WORLD(800), SCREEN_TO_WORLD(600));
// Define the gravity vector.
b2Vec2 gravity(0.0f, 10.0f);
// Do we want to let bodies sleep?
bool doSleep = true;
// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(worldAABB, gravity, doSleep);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, SCREEN_TO_WORLD(600));
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world.CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonDef groundShapeDef;
// The extents are the half-widths of the box.
groundShapeDef.SetAsBox(50.0f, 5.0f);
// Add the ground shape to the ground body.
groundBody->CreateShape(&groundShapeDef);
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.position.Set(SCREEN_TO_WORLD(400), SCREEN_TO_WORLD(100));
b2Body* body = world.CreateBody(&bodyDef);
bodyDef.position.Set(SCREEN_TO_WORLD(450), SCREEN_TO_WORLD(-20));
b2Body* body2 = world.CreateBody(&bodyDef);
bodyDef.position.Set(SCREEN_TO_WORLD(360), SCREEN_TO_WORLD(250));
b2Body* body3 = world.CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonDef shapeDef;
shapeDef.SetAsBox(1.0f, 1.0f);
// Set the box density to be non-zero, so it will be dynamic.
shapeDef.density = 1.0f;
// Override the default friction.
shapeDef.friction = 0.3f;
// Add the shape to the body.
body->CreateShape(&shapeDef);
body2->CreateShape(&shapeDef);
body3->CreateShape(&shapeDef);
// Now tell the dynamic body to compute it's mass properties base
// on its shape.
body->SetMassFromShapes();
body2->SetMassFromShapes();
body3->SetMassFromShapes();
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
// our main loop
while ( LoopGDK ( ) )
{
world.Step(timeStep, iterations);
// update the screen
dbRotateSprite(CUBE,body->GetAngle()*(180.0 / 3.141592653589793238));
dbRotateSprite(CUBE2,body2->GetAngle()*(180.0 / 3.141592653589793238));
dbRotateSprite(CUBE3,body3->GetAngle()*(180.0 / 3.141592653589793238));
dbPasteSprite(CUBE,WORLD_TO_SCREEN(body->GetPosition().x),WORLD_TO_SCREEN(body->GetPosition().y));
dbPasteSprite(CUBE2,WORLD_TO_SCREEN(body2->GetPosition().x),WORLD_TO_SCREEN(body2->GetPosition().y));
dbPasteSprite(CUBE3,WORLD_TO_SCREEN(body3->GetPosition().x),WORLD_TO_SCREEN(body3->GetPosition().y));
dbSync ( );
}
// return back to windows
return;
}