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
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...