Rendering graphics using box2D

OK so in order to add graphics to bodies, I would have to create the graphic, then move it with the bodies, right?

So how would I do this? Should I create a body list, then for every body, draw something. And then how would I draw it, I've read about using openGL witch seems to be my best bet, but I cant find a tutorial on image rendering. Can anyone help me?
This really has very little to do with Box2d, so you'll have more success asking in a graphics related forum on a site like gamedev.net. The answer really depends on what you want to do. Are you on windows, mac, linux? Do you want vector type graphics or images? Are you using a library like SFML or Haaf's game engine or an api like directX, OpenGL, or XNA. Once you answer all those questions, just google for basic tutorials.
Right. Ok so first of all I'm on a mac, and I'm going to use simple tiextures for my objects using openGL. So do you think that I should just add the texture in the debut draw or do I have to make a whole new system using body lists. If I used the bodie lists, would I put it in the display function?

Thanks for the help,
Kcaz
Keep things out of the debug draw. How is your game "engine" organized? I would just keep references to your bodies and/or shapes. Then, in your game loop, render each entity based on its body location (accounting for translation to screen coordinates of course). Box2D is called by your game engine, it's not the engine itself.

For example, I have something akin to a BulletManager/Factory. When a Bullet is needed, its initialization includes creating the Box2D body, position and velocities are set. I keep references to these objects around, in the Bullet class. In the game loop, the Box2D world is updated, and the BulletManager is responsible then for rendering each active Bullet, obtaining such necessary information such as position and orientation from the Bullet object itself.

Bill
Ok thanks I think that helped. The only thing I'm wondering is how to render the image. Do you add it as a texture? Or add it some other way?
Someone has to know...how do you create an image using openGL and attach it to a body?
In OpenGL you would render the image as a texture, yes.

Per my IM, you would get the position of the body (or shape) that the texture represents. From that location, you derive the vertex coordinates, and along with the texture coordinates send those down the OpenGL pipeline.

If this is where you're stuck, you might try an OpenGL forum. It's not really appropriate (beyond body/shape position and scaling) to get rendering "how to" advice here.

Bill
hi,

you don't attach images to bodies. bodies are independent from graphics. they are just the part of the physics simulation.

you have to manage graphics yourself. so for every body you create, you have to create an entity in your graphics system (eg. a textured quad)

this entity can be simple. something like

Code:
struct GraphicRepresentation
{
  b2body *_body; //pointer to the body this graphic represents
  TexturedQuad *_quad;
};



then in your mainloop after stepping the b2world you loop through the graphic representations and update each graphic representation with its body.

something like:

Code:
  _quad->setPosition (_body->position);
  _quad->setRotation (_body->angle);
  _quad->render();


if you have no idea how to render a textured quad then this is really the wrong place to ask for 🙂
OK that makes lots of sense, I was just about to go learn how to texture something :p

Thanks,
Kcaz

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