How to connect an image with a box2d object

Ok i realize that it's not an exclusively box2d question, but i think here is the best place to ask...
So in my html file i have:

Code: Select all

<img id="car_pic" src="images/da_car.svg">

and i'm trying to connect it to a rectangle body:

Code: Select all

var car_bodyDef = new b2BodyDef;
...
car_bodyDef.UserData = document.getElementById("car_pic");
...

and it didn't work. Also tried this, just in case...

Code: Select all

...
var car_fixtrDef = new b2FixtureDef;
...
car_fixtrDef.UserData = document.getElementById("car_pic");
...

and ofcourse that didn't work either...
can somebody help me out?
No, nobody can help you out until you define what "didn't work" means.
So with some help i managed to connect/attach images with box2d objects....
However it works only if the objects are still or moving in a linear motion (more on that later).

first, a global var, so it's possible to change debugDraw.SetDrawScale(SCALE)(and everywhere it's used) from here

Code: Select all

const SCALE = 20;

inserting images via javascript

Code: Select all

var box_img = new Image();
box_img.src = "box.png";
var ground_img = new Image();
ground_img.src = "ground.png";

later i create 2 objects a dynamic with userData="box_img" and a static with userData="ground_img".
then make a function to attach the images with the bodies.

Code: Select all

function Draw_and_Rotate(){
   for (b = world.GetBodyList() ; b; b = b.GetNext())   {
      var angle = b.GetAngle();
      var pos = b.GetPosition();
      if (b.GetUserData() == "box_img"){
         ctx.drawImage(box_img, pos.x*SCALE - (box_img.width / 2), pos.y*SCALE - (box_img.height / 2));
      }
      else if(b.GetUserData() == "ground"){
         ctx.drawImage(ground_img, (pos.x*SCALE) -(ground_img.width / 2), (pos.y*SCALE) -(ground_img.height / 2));
      }
   }   
}

and put that function in a gameloop/update/step function.
It will work as long as no object rotates.
And here's the problem: in html the reference point is the top left corner, in box2d the center of the object.
While rotating i could not match the image with the object anymore.
The best i could do so far is:

Code: Select all

function Draw_and_Rotate(){
   for (b = world.GetBodyList() ; b; b = b.GetNext())   {
      var angle = b.GetAngle();
      var pos = b.GetPosition();
      //console.log((angle));
      if (b.GetUserData() == "box_img"){
         ctx.save();
         ctx.translate( pos.x*SCALE - (box_img.width*3/5), pos.y*SCALE - (box_img.height*3/5) );
         ctx.rotate(angle);
         ctx.drawImage(box_img, pos.x - (box_img.width / 2), pos.y - (box_img.height / 2));
         ctx.restore();
      }
      else if(b.GetUserData() == "ground"){
         ctx.drawImage(ground_img, (pos.x*SCALE) -(ground_img.width / 2), (pos.y*SCALE) -(ground_img.height / 2));
      }
   }   
}

which is not good by any means...
I don't know what to do... Is there a function in box2d that helps in this situation???

P.S. looking forward for some info but if someone needs the whole code please say so and i'll post it...
I was able to ALMOST fix completely the rotation problem.
I managed to put the img "center" top left corner with the object center on top of each other while keeping them completely aligned:

Code: Select all

function Draw_and_Rotate(){
   for (b = world.GetBodyList() ; b; b = b.GetNext()){
      var angle = ((b.GetAngle()*180)/(Math.PI))/SCALE;
      var pos = b.GetPosition();
      console.log((angle));
      if (b.GetUserData() == "bo_img"){
         ctx.save();//save the ctx state prior changing it.This method pushes the current state onto the stack.
         ctx.translate(pos.x*SCALE , pos.y*SCALE );
         ctx.rotate(angle);
         ctx.drawImage(box_img, - (box_img.width / 2), - (box_img.height / 2));
         ctx.restore();//restoring ctx state.This method pops the TOP state on the stack, restoring the context to that state.
      }
      else if(b.GetUserData() == "ground"){
         ctx.drawImage(ground_img, (pos.x*SCALE) -(ground_img.width / 2), (pos.y*SCALE) -(ground_img.height / 2));
      }
   }   
}

However although i convert GetAnlge() to degrees (var angle = ((b.GetAngle()*180)/(Math.PI));) it is still for a few angles off...
Why is that???

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