Does anyone have an example of how to use b2Body.m_rotation to rotate a BitmapData representation of the body for use in a draw function? Thanks
b2Body.m_rotation to rotate BitmapData
FlashBitmapData doesn't rotate, it's meant to be the 'origin' of the display image and as such has no placement properties. You want to rotate the Bitmap that displays this. If you actually want to rotate the data of the BitmapData itself, use BitmapData.draw to copy the image of the transformed Bitmap object onto another.
Gust wrote:
BitmapData doesn't rotate, it's meant to be the 'origin' of the display image and as such has no placement properties. You want to rotate the Bitmap that displays this. If you actually want to rotate the data of the BitmapData itself, use BitmapData.draw to copy the image of the transformed Bitmap object onto another.
Exactly. I use a matrix to rotate the BitmapData before drawing it onto another, but I got something wrong I think.
@Ygryggil:
This should do what you want. It's not optimized, but it works. The biggest thing that could be sped up is when creating the bitmap to rotate the shape in it is too big in most cases. I use either the height or width, whichever is greater and just make both height and width that big. So if you have a long rectangle or something and it is oriented horizontally, this will waste a lot of memory, and slow down the copyPixels being drawn to the screen. I have done tests and it slows down copyPixels even if it's a bunch of empty transparent space. So as long as your shapes aren't horribly oblong it shouldn't be too inefficient. I will improve it later at some point though.
I would also recommend having static objects not run through this rotation. Also objects should probably store their last rotation number and bitmap, and reuse the bitmap if their current rotation matches their last. This would stop objects that have gone to sleep from taking up time rotating themselves when they are just sitting there. Hopefully I will be able to finish a general purpose library that handles all this at some point.
Here it is:
This should do what you want. It's not optimized, but it works. The biggest thing that could be sped up is when creating the bitmap to rotate the shape in it is too big in most cases. I use either the height or width, whichever is greater and just make both height and width that big. So if you have a long rectangle or something and it is oriented horizontally, this will waste a lot of memory, and slow down the copyPixels being drawn to the screen. I have done tests and it slows down copyPixels even if it's a bunch of empty transparent space. So as long as your shapes aren't horribly oblong it shouldn't be too inefficient. I will improve it later at some point though.
I would also recommend having static objects not run through this rotation. Also objects should probably store their last rotation number and bitmap, and reuse the bitmap if their current rotation matches their last. This would stop objects that have gone to sleep from taking up time rotating themselves when they are just sitting there. Hopefully I will be able to finish a general purpose library that handles all this at some point.
Here it is:
Code:
//poly is a b2PolyShape object
var pos:b2Vec2 = poly.m_position;
//get its extents
var extents:b2Vec2 = poly.m_localOBB.extents;
//this is the Bitmap object for the screen
var screenBitmap:Bitmap;
//this is the bitmap data it will display
var bufferBitmap:BitmapData;
screenBitmap.bitmapData = bufferBitmap;
//get upper left corner of where the bitmap should be on the screen
//pos is the center, so we subtract the extents, scale to pixel values from physics values
//and then adjust for where the display bitmap is on the screen (probably 0,0)
var d_x:Number = (pos.x - extents.x) * m_physScale - screenBitmap.x;
var d_y:Number = (pos.y - extents.y) * m_physScale - screenBitmap.y;
//this is the bitmap you're going to rotate (it should have something in it)
var pbitmap:BitmapData;
//the matrix we're going to use to rotate and move the BitmapData into place
var pmatrix:Matrix = new Matrix();
var rot:Number;
var point:Point;
//create space to rotate shape in
var rotatesize:b2Vec2 = new b2Vec2();
var maxextent:Number;
if (extents.x > extents.y) {
maxextent = extents.x * 2 * m_physScale;
} else {
maxextent = extents.y * 2 * m_physScale;
}
rotatesize.x = maxextent;
rotatesize.y = maxextent;
//calculate the center of rotation space
var rot_offset:b2Vec2 = new b2Vec2();
rot_offset.x = (rotatesize.x / 2) - (extents.x * m_physScale);
rot_offset.y = (rotatesize.y / 2) - (extents.y * m_physScale);
//the shape has to be moved into the center of rotation space so that it rotates cleanly
//the offset of where to draw it on screen has to be adjusted accordingly
d_x -= rot_offset.x;
d_y -= rot_offset.y;
//get the rotation of the body
rot = poly.m_body.m_rotation;
//get the center of the original bitmap space, this is what we want to
//rotate around
point = new Point(extents.x * m_physScale,extents.y * m_physScale);
point = pmatrix.transformPoint(point);
pmatrix.tx -= point.x;
pmatrix.ty -= point.y;
pmatrix.rotate(rot);
pmatrix.tx += point.x;
pmatrix.ty += point.y;
pmatrix.translate(rot_offset.x,rot_offset.y);
//create the bitmap to draw the rotate object into
var temprotate:BitmapData = new BitmapData(rotatesize.x,rotatesize.y,true,0x00000000);
//draw bitmap into rotation space using rotation matrix
temprotate.draw(pbitmap,pmatrix,null,"normal",null,true);
//draw the bitmap onto the BitmapData object of the screenBitmap
bufferBitmap.copyPixels(temprotate,temprotate.rect,new Point(d_x,d_y),null,null,true);
var pos:b2Vec2 = poly.m_position;
//get its extents
var extents:b2Vec2 = poly.m_localOBB.extents;
//this is the Bitmap object for the screen
var screenBitmap:Bitmap;
//this is the bitmap data it will display
var bufferBitmap:BitmapData;
screenBitmap.bitmapData = bufferBitmap;
//get upper left corner of where the bitmap should be on the screen
//pos is the center, so we subtract the extents, scale to pixel values from physics values
//and then adjust for where the display bitmap is on the screen (probably 0,0)
var d_x:Number = (pos.x - extents.x) * m_physScale - screenBitmap.x;
var d_y:Number = (pos.y - extents.y) * m_physScale - screenBitmap.y;
//this is the bitmap you're going to rotate (it should have something in it)
var pbitmap:BitmapData;
//the matrix we're going to use to rotate and move the BitmapData into place
var pmatrix:Matrix = new Matrix();
var rot:Number;
var point:Point;
//create space to rotate shape in
var rotatesize:b2Vec2 = new b2Vec2();
var maxextent:Number;
if (extents.x > extents.y) {
maxextent = extents.x * 2 * m_physScale;
} else {
maxextent = extents.y * 2 * m_physScale;
}
rotatesize.x = maxextent;
rotatesize.y = maxextent;
//calculate the center of rotation space
var rot_offset:b2Vec2 = new b2Vec2();
rot_offset.x = (rotatesize.x / 2) - (extents.x * m_physScale);
rot_offset.y = (rotatesize.y / 2) - (extents.y * m_physScale);
//the shape has to be moved into the center of rotation space so that it rotates cleanly
//the offset of where to draw it on screen has to be adjusted accordingly
d_x -= rot_offset.x;
d_y -= rot_offset.y;
//get the rotation of the body
rot = poly.m_body.m_rotation;
//get the center of the original bitmap space, this is what we want to
//rotate around
point = new Point(extents.x * m_physScale,extents.y * m_physScale);
point = pmatrix.transformPoint(point);
pmatrix.tx -= point.x;
pmatrix.ty -= point.y;
pmatrix.rotate(rot);
pmatrix.tx += point.x;
pmatrix.ty += point.y;
pmatrix.translate(rot_offset.x,rot_offset.y);
//create the bitmap to draw the rotate object into
var temprotate:BitmapData = new BitmapData(rotatesize.x,rotatesize.y,true,0x00000000);
//draw bitmap into rotation space using rotation matrix
temprotate.draw(pbitmap,pmatrix,null,"normal",null,true);
//draw the bitmap onto the BitmapData object of the screenBitmap
bufferBitmap.copyPixels(temprotate,temprotate.rect,new Point(d_x,d_y),null,null,true);
Thankyou brainclog, I will try it tonight.
Thanks. I didn't treat the position as the center of the bitmap, which in turn gave very odd effects. This is a working version of my code. As you can see I'm using m_userData to hold BitmapData.
BTW offsetP is used to scroll the world.
Code:
public function drawDisplayObject(bmd:BitmapData, bb:b2Body):void{
// get actor
if(!(bb.m_userData is BitmapData)){return;}
var bb_bmd:BitmapData = BitmapData(bb.m_userData);
// get loc
var p:Point = new Point();
p.x = (bb.m_position.x * m_physScale) + offsetP.x - (bb_bmd.width/2);
p.y = (bb.m_position.y * m_physScale) + offsetP.y - (bb_bmd.height/2);
// draw
if(bb.m_rotation){
var degrees:int = bb.m_rotation * (180.0 / Math.PI);
var angle_in_radians:Number = Math.PI * 2 * (degrees / 360);
var rotationMatrix:Matrix = new Matrix();
rotationMatrix.translate(-bb_bmd.width/2,-bb_bmd.height/2);
rotationMatrix.rotate(angle_in_radians);
rotationMatrix.translate(p.x, p.y);
rotationMatrix.translate(bb_bmd.width/2,bb_bmd.height/2);
bmd.draw(bb_bmd, rotationMatrix, null, null, null, true);
}else{
bmd.copyPixels(bb_bmd,bb_bmd.rect,p,null,null,true);
}
}
// get actor
if(!(bb.m_userData is BitmapData)){return;}
var bb_bmd:BitmapData = BitmapData(bb.m_userData);
// get loc
var p:Point = new Point();
p.x = (bb.m_position.x * m_physScale) + offsetP.x - (bb_bmd.width/2);
p.y = (bb.m_position.y * m_physScale) + offsetP.y - (bb_bmd.height/2);
// draw
if(bb.m_rotation){
var degrees:int = bb.m_rotation * (180.0 / Math.PI);
var angle_in_radians:Number = Math.PI * 2 * (degrees / 360);
var rotationMatrix:Matrix = new Matrix();
rotationMatrix.translate(-bb_bmd.width/2,-bb_bmd.height/2);
rotationMatrix.rotate(angle_in_radians);
rotationMatrix.translate(p.x, p.y);
rotationMatrix.translate(bb_bmd.width/2,bb_bmd.height/2);
bmd.draw(bb_bmd, rotationMatrix, null, null, null, true);
}else{
bmd.copyPixels(bb_bmd,bb_bmd.rect,p,null,null,true);
}
}
BTW offsetP is used to scroll the world.
Original thread: https://box2d.org/forum/viewtopic.php?t=424