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:
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();
}
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();
}