Import and Attach Images

How do I import and attach a PNG or GIF to a shape in Box2dWeb?

The best resource I could find online is at: http://www.jeremyhubble.com/box2d.html

However, this "tutorial" doesn't give much explanation about how images are actually used. So it is clearly possible, but I can't get the userData trick to work.

Thanks!
here is a nice example:
http://box2dweb.com/blog/2012/05/01/att ... %E5%9B%BE/

Code: Select all

 world.Step(1 / 60, 5, 2);
         world.DrawDebugData();
            world.ClearForces();
       
           
                       
               context.clearRect(0, 0, 550, 480);

        for (b = world.GetBodyList() ; b; b = b.GetNext()) {
            if (b.GetType() == b2Body.b2_dynamicBody) {
               
                 var pos = b.GetPosition();

if (pos.x<-2 || pos.x>80 )
{

b.SetLinearVelocity(new b2Vec2(0,0));
b.SetAngularVelocity(0);

b.SetPositionAndAngle(new b2Vec2(38,0),0);

}     




                context.save();
                context.translate(pos.x * 20-20, pos.y * 20);
                context.rotate((b.GetAngle())/15);
                context.drawImage(image, -25, -25);
                //context.drawImage(image2, -25, -25);
                context.restore();
            }
           
           
             
            if (b.GetType() == b2Body.b2_kinematicBody) {
               
                 var pos = b.GetPosition();

if (pos.x>40 && pos.y<5)
{
b.SetPositionAndAngle(new b2Vec2(-2,16),0);

}     
if (pos.x>40 && pos.y>6 && pos.y<14)
{
b.SetPositionAndAngle(new b2Vec2(-2,2.5),0);

} 

if (pos.x>40 && pos.y>15)
{
b.SetPositionAndAngle(new b2Vec2(-2,7),0);

}       

                context.save();
                context.translate((pos.x - 3) * 20, (pos.y-1) * 20);
                context.rotate((b.GetAngle())/15);
                //context.drawImage(image, -25, -25);
                context.drawImage(image2, -25, -25);
                context.restore();
            }

           
           
           
        }
 
Hey Box2dWeb,

Are you by chance Arch!tect? Anyways, I have a great solution over at:

http://stackoverflow.com/questions/1015 ... and-images
PlayerOne wrote:Hey Box2dWeb,

Are you by chance Arch!tect? Anyways, I have a great solution over at:

http://stackoverflow.com/questions/1015 ... and-images


Hi! You are the one who leave message on FB right? I replied u a long time ago...
I saw that solution too. And the above example made by me is using exactly the same solution. Thanks~ ^ ^

Let me know if you need any help with your project~
Will do!

I am planning on starting a Kickstarter soon! I will post more information here in the future.

Best of luck.
This might be of some help --- http://adf.ly/FxMiz

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