A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: How to rotate BitmapData? + other question

  1. #1
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244

    How to rotate BitmapData? + other question

    Hey guys, I am trying to integrate my game engine with Box2D and also trying to use BitmapData for all of the rendering to increase speed however I haven't used it much and am having trouble figuring out how to get something to rotate...

    I have all of my objects stored in BitMap data objects and I call render on each of them and then copyPixels over to the main BitmapData object which is shown on screen.

    Also I was wondering how you could draw all of a Sprite / Movieclip object with BitmapData.draw() if the Sprite placed so that the origin is in the center...in other words when I draw from a sprite that is w:50,h:50 and it's x,y is at -25,-25 it only draws the positive part that shows up. Any way to fix this?

    Thanks

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    thats the problem of copyPixles, you cant rotate, you'll have to create bitmapData's for every possible rotation of an object, so 360 bmp's per object.

    the only other option is use draw with a matrix.

    use matrix.translate to fix your last problem.
    lather yourself up with soap - soap arcade

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    You *could* try Matrix.rotate(angleInRadians), but I got some funny results in my case.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  4. #4
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Well I am using an animated bitmap class I created so I'd actually have to create a HUGE amount of frames...I guess the total rotation for every frame of the animation, on every clip that was animated. Not sure if that is a good idea....but if the memory usage wouldn't be a big deal I suppose I could do it. Thoughts?

    I haven't been able to figure out the matrix rotating. It just doesn't seem to be working right...Does anybody know how to use it that can give me some tips?

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    you should rotate the matrix before you translate it.

    maybe try:

    matrix = new Matrix()
    matrix.rotate(radain)
    matrix.translate(25, 25)

    bmp.draw(clip, matrix)
    lather yourself up with soap - soap arcade

  6. #6
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    That won't work for me, I've already "drawn" all of my clips into an array of bitmap datas. I am rendering using copypixels for speed and am trying to figure out how to do it...I've already spent 5 hours looking for a way to do this I'm getting frustrated =/

    If I use bitmap.draw every single frame that seems to slow down the app by a lot.

  7. #7
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Have you tried graphics.beginBitmapFill? I g.bBF.

  8. #8
    Professional Flash Developer
    Join Date
    Aug 2007
    Location
    California
    Posts
    105
    http://www.8bitrocket.com/newsdispla...?newspage=6765
    Actionscript 3: Tutorial - BitmapData rotation with a matrix

  9. #9
    Junior Member
    Join Date
    Aug 2003
    Location
    NC, USA
    Posts
    11
    Same issue here, also, using draw incurs the same performance hit that drawing vector MCs does. So I am finding that sticking to copyPixels and blitting different sprite frames is the best way to go here. Will post back if i find out otherwise...

  10. #10
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    What I do is draw() my movie clip 72 times, with its _rotation increasing by 5º each time. I store these frames in an array, and at run-time copyPixels() the required one in.
    http://www.birchlabs.co.uk/
    You know you want to.

  11. #11
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Okay I've read some posts from 8bitrocket.com and the method you have about rotating is out for me since it uses BitmapData.draw every single frame.

    I did read some speed tests you had about using individual sprites for each character to display rather than blitting everything to one BitmapData and it seemed to be just as fast until around 10K+ objects...which should be fine for me...HOWEVER my question is that how much would it slow down once you start rotating stuff compared to a matrix rotation?

    Also, who would recommend using vengeances method? How feasable is that? Also if I did that, I'd have to write something that makes the bitmap data actually bigger because you will have to account for longer images than the height etc...

  12. #12
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    To have a bitmapData for each angle will work only for when you would rotate a static image,i mean, imagine if your animation is a walking guy that you have 12 frames for him to walk, for each frame you will have to add 360 another frames, one for each angle, and thats just for walking, imagine if he also jumps, hides, dies, whatever
    Slow of not, i dont think that there is too much option for that besides draw()

  13. #13
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Colby, to give you an idea of how fast my method is:

    http://birchlabs.co.uk/Invasion5CP.swf

    Granted, that particular swf didn't preprocess the sprites, it loaded in an existing spritesheet:



    But this spritesheet can be very easily generated as part of the game's loading sequence (would only take about half a second, probably less, for Flash to prepare it). An example of a file where I do generate a 'rotation' sheet for each sprite is here:

    http://birchlabs.co.uk/Polygon.swf

    The square (there seems to be a problem with it disappearing between 270-360º, since it rotates differently to the other objects), the ship, and all its bullets are calculated with 5º intervals of rotation. Note that they also have pre-rendered glow effects, which only add time to the preprocessing stage - they don't slow down the game at run-time. When rendered onto the screen, we round up to the closest sprite we have, so 173.23º becomes 175º. Now, you'd be hard-pushed to tell by sight that any BitmapData is being used in this at all.

    To get the dimensions of the BitmapData, I allocate Math.max(width*√2, height*√2) width and height for every cell. Now, this doesn't give us the smallest possible sprites, but culling it would make it hell to work out what coordinates to draw and render the objects at.

    FYI, this system can handle at least 7,000 animated objects without breaking a sweat. Just means a lot of work goes into the preprocessing.
    http://www.birchlabs.co.uk/
    You know you want to.

  14. #14
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Thank you vengeance, I suppose I'll just have to take into consideration what type of game I am making as to what method I would want to use: individual sprites, or preprocessed rotations.

    I may want to copy bitmapdata into sprites with bitmap smoothing for a lengthy animated char and then for all non animated objects I would probably want to use your method. That is my guess anyway.

    It wouldn't be too hard for me to write two classes for both cases and just use whichever one suited the particular object I am trying to render.

    Thanks for the help!

  15. #15
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    In the case of animated objects,if you rotate all the bitmapDatas so that you can copyPixels them, that may be a waste since you can do everything draw() based,without the copyPixels part
    Im gonna make some tests to see how fast this is compared to movieClips, i bet it will be a joke compared to copyPixels, but it will have rotation, (x,y and rotation,thats all i need 90% of the time)and besides, with a draw engine you can make some cool things with colorTransform and blendModes, that may be handy some day

  16. #16
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    I think I would be more interested in seeing the speed difference compared to sprites filled with bitmapdata already which is now what I am doing.

    I have a class that imports a movieclip and then draws in each frame into an array of bitmapdata and then animates itself by pointing a "bmpData" property to the current bmpData in the array. I then have a sprite on stage with a BMP inside of it holding the "bmpData". This allows me to rotate and resize or whatever, while the animation class handles the actual animation for me.

    This is sort of how it was done on 8bitrocket website for his speed tests and there wasn't really any slowdown but he didn't test actual rotation so I don't know how much speed is used when I actually begin rotating things.

    I may have to create my own tests as well...

  17. #17
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    Right, the conclusion with a 100% draw engine,rotating and setting x and y with a matrix is that it does not worth compared to mcs.
    ColbyCheese, im curious to see your results but this way you still gotta have a lot of sprites for each item,isnt it?Many of the speed of copyPixels engines came from not having to deal with lots of display objects

  18. #18
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    I'm not sure why this is difficult for me but I am having a hell of a time figuring out how to create prerotated sprites. I just can't seem to get it to work. Vengeance would you care to share the code you use to do this? Or anyone else?

  19. #19
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    One easy way to do this is to put your target clip inside a clip container, and in a loop that makes i goes from 1 to 360:
    -make the target rotates i
    -create a new BitmapData and makes him draw the container, not the target clip.
    -store the BitmapData on the array of BitmapDatas at the pos i
    and thats it, you will have the array of BitmapDatas, each index is also the angle the stuff is rotated inside

  20. #20
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    That's the approach I was trying to take, the problem is getting the object inside to rotate and still show up correctly when drawn. For instance when you rotate a square the corners will get clipped as it rotates since they go outside of the rect area of the draw.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center