Rotating Pixelmask


(Gil) #1

I want to be able to rotate the pixelmask of my entity about the (0,0) point of the image graphic. Clockwise and counterclockwise. I did some research and i think that I can use the Mask.x and Mask.y and the (0,0) point to do so using 2d rotation vector math. This is where I screw up though I don’t know how to do this lol. Please and thank you!

It is imperative to me that I accomplish this task for further development in my game :frowning: Thanks


(Mike Evmm) #2

I had the same problem a while ago, and from what I gathered, due to the way FP handles collisions, it’s simply not possible. (I hope I’m wrong, though)


(Martí Angelats i Ribera) #3

I think this should work (i haven’t got the chance to test it yet):

private const c:Number = 2*Math.PI/360;
private var mask:Pixelmask;
private var maskBitmap:BitmapData;
private var matrix:Matrix;
private var rotatedMask:BitmapData;

//the angle in grades. If want to use radiants, delete the constant c.
private function setRotation(angle:Number):void 
{
	matrix.rotate(angle * c);
	rotatedMask.fillRect(rotatedMask.rect, 0);
	rotatedMask.draw(maskBitmap, matrix);
}


maskBitmap = FP.getBitmap(Assets.MASK);
rotatedMask = new BitmapData(width, height, 0);
mask = new Pixelmask(rotatedMask, 0, 0);
setRotation(0);

Then to rotate you just have to do

setRotation(angle);

PD: The resultant Pixelmask is used as normal.


(Gil) #4

Going to try this later, hope it works!


(Gil) #5

I get an invalid BitMapData error actually :frowning:


(TaylorAnderson) #6

Doesn’t flashpunk only have square hitboxes?


(Gil) #7

Yeah, the square is drawn as bitmapdata though.


(Martí Angelats i Ribera) #8

What does the error say?


(Mike Evmm) #9

Pixelmasks can not be square
(this sounds weird)


(Martí Angelats i Ribera) #10

Oh i think i found the error.
Try this one:

private const c:Number = 2*Math.PI/360;
private var mask:Pixelmask;
private var maskBitmap:BitmapData;
private var matrix:Matrix;
private var rotatedMask:BitmapData;

//the angle in grades. If want to use radiants, delete the constant c.
private function setRotation(angle:Number):void 
{
    matrix.rotate(angle * c);
    rotatedMask = new BitmapData(width, height, 0xFFFFFFFF);
    rotatedMask.draw(maskBitmap, matrix);
    mask.data = rotatedMask;
}


maskBitmap = FP.getBitmap(Assets.MASK);
mask = new Pixelmask(rotateBitmap, 0, 0);
setRotation(0);

Also nitce that the rotated bitmap is an square. You can calculate it using pithagoras (with the width and height of the original image); or, in case that you can put your mask in a cercle, you can se the diameter of it as width and height. I recomend to calcualte it once and add it as a constant.

Note: Remember to set the Pixelmask contructor, the last 2 numbers should be changed to the numbers that you need.

Note 2: The image will rotate in the middle. If you want to set another rotation origin you’ll have to modify the matrix to do it. (only need to change it right after create it or in the constructor).

PD: The main error was that the rotatedMask was field with 0x00000000 (invisible black) and it should be filled with flat white (0xFFFFFFFF). Then i saw another error: the hitbox wasn’t updated.


Rotating an entity... problems