[slvd]Not rendering bitmap-filled shapes on bodies correctly

Hello,

I am trying to use bitmap-filled shapes to render Box2D bodies. However, the bitmaps I fill the shapes to render them with are magnified by 60 (each pixel becomes 60x60 pixels) because of bodyDef.userData.width = w * 2 * 30; and bodyDef.userData.height = h * 2 * 30;. I assume that the * 2 * 30 at the end of those is rudimentary when using anything to render Box2D bodies correctly, but it's also magnifying my images.

Anyone have any idea how to get around this?

Here is the code for those who care:
Code:
[Embed(source="texture.png")]
private var Texture:Class;

createBox(Math.random() * 28, Math.random() * 1, Math.random() * 2, Math.random() * 2, 0.3, 0.2, false, 2, Texture);

      private function createBox(x:Number, y:Number, w:Number, h:Number, friction:Number, restitution:Number, static:Boolean, mode:uint, fill:Object):void {
         // Modes: 0 - solid fill, 1 - gradient fill, 2 - bitmap fill
         trace("function:createBox");
         
         bodyDef = new b2BodyDef();
         bodyDef.position.Set(x, y);
         boxDef = new b2PolygonDef();
         var boxCenter:b2Vec2 = new b2Vec2(w, h);
         boxDef.SetAsOrientedBox(w, h, boxCenter);
         boxDef.friction = friction;
         boxDef.restitution = restitution;
         if (static) {boxDef.density = 0;} else {boxDef.density = 1.0;}
         bodyDef.userData = new Shape();
         switch(mode) {
         case 0:
            bodyDef.userData.graphics.beginFill(fill);
            bodyDef.userData.graphics.lineStyle(0, 0x000000);
            bodyDef.userData.graphics.drawRect(0, 0, w, h);
            bodyDef.userData.graphics.endFill();
            break;
         case 2:
            var bitmap:Bitmap = new fill();
            var bitmapdata:BitmapData = bitmap.bitmapData;
            bodyDef.userData.graphics.beginBitmapFill(bitmapdata);
            bodyDef.userData.graphics.drawRect(0, 0, w, h);
            bodyDef.userData.graphics.endFill();
            break;
         }
         bodyDef.userData.width = w * 2 * 30;
         bodyDef.userData.height = h * 2 * 30;
         addChild(bodyDef.userData);
         body = m_world.CreateBody(bodyDef);
         body.CreateShape(boxDef);
         body.SetMassFromShapes();
      }
is this serious 🙂
Skofo wrote:
the bitmaps I fill the shapes to render them with are magnified by 60 (each pixel becomes 60x60 pixels) because of bodyDef.userData.width = w * 2 * 30; and bodyDef.userData.height = h * 2 * 30;... magnifying my images. Anyone have any idea how to get around this?
How about using actionscript comment construct to comment out these lines?
makc wrote:
is this serious 🙂
Skofo wrote:
the bitmaps I fill the shapes to render them with are magnified by 60 (each pixel becomes 60x60 pixels) because of bodyDef.userData.width = w * 2 * 30; and bodyDef.userData.height = h * 2 * 30;... magnifying my images. Anyone have any idea how to get around this?
How about using actionscript comment construct to comment out these lines?
I don't appreciate your elitism. I assume that this is a place to help people, not make fun of their problems.

Also, I'd rather not comment out those lines, as I assume that it is advantageous to use a physics scale.
I think you want to apply your physics scale during graphics.drawRect() and skip the width and height settings. For a 1m x 1m box, instead of drawing a 1px x 1px rectangle then magnifying it by 60x, draw a 60px x 60px rectangle.
chriswa wrote:
I think you want to apply your physics scale during graphics.drawRect() and skip the width and height settings. For a 1m x 1m box, instead of drawing a 1px x 1px rectangle then magnifying it by 60x, draw a 60px x 60px rectangle.
I already "fixed" my problem using matrices, but your way works better because it allows me to change line widths more intricately. Thank you so much!

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