Simple tutorial using graphics

does anybody have any advice on how to start coding simple physics based games or applications/demos using box 2d and c++.

I understand about 90% of the hello world application however I have no idea how to start using any sort of graphics (eg opengl)

the testbed examples use graphics, but for a newb like me they are impossible to understand.

i have had a little experience in programming with c++ and glut. howeverI have no idea how to integrate something like the glut render loop with the box2d update stuff.

thanks for any advice, links to tutorials (c++ only please) or sample code you man have for me.

thanks!
I don't know any tutorials about using box2d in a graphical environment and I doubt they exist since Box2D has nothing to do with graphics.

What you need to do first is to find a graphic engine that will enable you to do what it is you want to do. Mess around with their tutorials without using Box2D. You need to figure out how to set X, Y and angle values so your graphic object is displayed at the correct place. Then it's only a matter of stepping Box2D every frame and getting these values from Box2D to update your graphic objects.

You should end up with something like this for a game loop
Code:
while(gameRunning) {
Box2D.Step();
gameLogic();
updateObjectsPosition();
GraphicEngine.renderOneFrame();
}
Thanks for your reply, I realize that vox2d has no inbuipt graphics support, but I have been playing around with opengl and glut for a little while, if you could be a little more specific within the opengl and glut setup then it would be very useful
I never used glut and last time I played with opengl was about 10 years ago... and it wasn't pretty. Can't help you there sorry. Try looking up glut's website, you should find something to get you started there.
Here's a good place to find some OpenGL tutorials:
http://www.google.com/search?q=opengl+tutorials
Thanks for all your replies, however, what I was trying to say is that I have had experience with opengl, but am very new to box 2d. I was really asking about how to use them together, as the helloworld example is very different to how glut sets up windows and draws maby I'm just being a bit stupid with box2d and how it works...
Basically you add some bodies to Box2D, step the simulation, then get the positions of the bodies. Use the positions of the bodies to move your graphical actors. Do you know how to move actors around in OpenGL?
yeah, I guess Im just being lazy... Ive had experience in opengl (very basic though) and am looking into box2d. I understand how opengl works and glut in particular, but i was having trouble meshing the two together (like the helloworld example and a basic glut thing). I suppose I should look over the documentation in more detail to get how box2d really works... like I think I understand the very basics but Im not sure how to really start implementing it myself

thanks anyway
hey, Im back, Ive got a more in depth question for you now. Ive managed to combine glut and box2d but collision detection isint really working, i was wondering how you would suggest I went about it.

here is my code:
Code:
#include <stdlib.h>

#include <stdio.h>

#include<GL/glut.h>

#include "Box2D.h"

float angle = 0.0;

///box2d
b2World* myWorld;

b2Body* body;


void changeSize(int w, int h) {
   
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).

if(h == 0)
   
   h = 1;

   
float ratio = 1.0* w / h;

   
// Reset the coordinate system before modifying

glMatrixMode(GL_PROJECTION);
   
glLoadIdentity();
   
   
// Set the viewport to be the entire window
   
glViewport(0, 0, w, h);

   
// Set the correct perspective.
   
gluPerspective(45,ratio,1,1000);
   
glMatrixMode(GL_MODELVIEW);
   
glLoadIdentity();
   
gluLookAt(0.0,0.0,5.0, 
            0.0,0.0,-1.0,
           0.0f,1.0f,0.0f);

}

void renderScene(void)
{
   
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_BLEND);
   
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
   
glRotatef(angle,0.0f,1.0f,0.0f);
   
//draw here
   float32 timeStep = 1.0f / 60.0f;
   
int32 iterations = 10;
   
myWorld->Step(timeStep, iterations);
    b2Vec2 position = body->GetPosition();
    float32 angle = body->GetAngle();
    printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
   glColor3f(1.0f,1.0f,1.0f);
   glBegin(GL_POINTS);
   glVertex2f(position.x/10,position.y/10);
   glEnd();
   glBegin(GL_LINE_LOOP);
   for (int32 i = 0; i < 4; ++i)
   {
      //glVertex2f( ->vertices[i].x, ->vertices[i].y);
      glVertex2f((float)i/1,(float)i/1);
   }
   glEnd();
   
//end draw
   
glPopMatrix();
   
glutSwapBuffers();
   
//printf("%f\n", angle);

}

void main(int argc, char **argv)
{
   
//box2d
   
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
   
b2Vec2 gravity(0.0f, -10.0f);
bool doSleep = true;

myWorld = new b2World(worldAABB, gravity, doSleep);
   
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
   
b2Body* groundBody = myWorld->CreateBody(&groundBodyDef);
   
b2PolygonDef groundShapeDef;
   groundShapeDef.SetAsBox(50.0f, 10.0f);
   
groundShapeDef.isSensor = true;
   
groundShapeDef.filter.groupIndex = 1;
   
b2BodyDef bodyDef;
   bodyDef.position.Set(0.0f, 4.0f);
   body = myWorld->CreateBody(&bodyDef);
   
b2PolygonDef shapeDef;
   shapeDef.SetAsBox(1.0f, 1.0f);
   shapeDef.density = 1.0f;
   shapeDef.friction = 0.3f;
   shapeDef.isSensor = true;
   shapeDef.filter.groupIndex = 1;
   body->CreateShape(&shapeDef);
   body->SetMassFromShapes();

   
   
//glut
   glutInit(&argc, argv);
   
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);
   
glutInitWindowPosition(1000,1000);
   
glutInitWindowSize(800,500);
   
glutCreateWindow("ShockAndAwe");
   
glutFullScreen();
   glutDisplayFunc(renderScene);
   
glutIdleFunc(renderScene);
   
glutReshapeFunc(changeSize);
   
glEnable(GL_DEPTH_TEST);
   
glutMainLoop();

}





You need to be more precise about what's the problem and what you're trying to achieve.

Original thread: https://box2d.org/forum/viewtopic.php?t=3257