Be a Supporter!

Hundreds of Entities with no lag?

  • 544 Views
  • 13 Replies
New Topic Respond to this Topic
DavidHan
DavidHan
  • Member since: Sep. 19, 2011
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Hundreds of Entities with no lag? Feb. 28th, 2013 @ 11:56 PM Reply

Hi. if you've ever played Coinbox Hero (great game), you'll notice that there are literally hundreds of coin entities on screen at a time with gravity and collision, but there doesn't seem to be any lag. How did Jmtb02 accomplish this?

Is it in the code? Is his code super efficient? Is it the way he displayed the coin graphic? What is the most efficient and processor friendly way to display graphics, especially for something that doesn't change, like a coin?

I am interested, because I want to rain down lots of blocks in my game. All they need to do is fall at a constant velocity, then if it detects ground, it snaps into place. right now, I think My code is as efficient as it can get, with ONE hittestPoint function and some math to figure out where its final landing zone will be.

If at all you have any experience and have tips on performance management with lots of entities, please comment. Right now, I'm seeing frames drop from 60 at around 80-90 entities.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 12:04 AM Reply

I've wondered this too, they're all vector art too hence quality options modifying them.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 12:14 AM Reply

At 3/1/13 12:04 AM, MintPaw wrote: I've wondered this too, they're all vector art too hence quality options modifying them.

Stage.quality affects bitmaps as well, just not as noticeably from what I've tried. It might just be toggling smoothing or something, though.

BitmapData and blitting is the way I've been working with for a while now. You could have thousands of entities with no lag (if the rest of the code is optimized as well). You use the copyPixels function of bitmapdata to copy and paste other bitmapdata (image file data is a bitmap/bitmapdata) to one central one and display that. Animation is just pasting the next frame in the animation over the previous one. The problem is that it doesn't support rotation or scaling, so you'd either have to have the rotated or scaled image as another part of the image file, or you'd have to use the draw function of bitmapdata, which is slow and will give bad performance with a lot of entities.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 12:18 AM Reply

For clarification, this is an example of a spritesheet that you'd copy pixels from. It doesn't have to be organized into rows and columns like this one, but it makes finding the rectangle that contains the image much easier.

Each row is an animation in this case.

Fooliolo
Fooliolo
  • Member since: May. 1, 2005
  • Offline.
Forum Stats
Member
Level 11
Gamer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 03:54 AM Reply

At 3/1/13 12:14 AM, MSGhero wrote: The problem is that it doesn't support rotation or scaling, so you'd either have to have the rotated or scaled image as another part of the image file, or you'd have to use the draw function of bitmapdata, which is slow and will give bad performance with a lot of entities.

Or you could just have the base image to keep download small, expand the original bitmap, tell the user's computer to render rotated/scaled copies of the image once (loading screens, yey!), and then blit it just like any other blittable image. You'll want to use the matrix class to do the rotation or whatever other fancy transformation you want.


Medal Games in a Nutshell: a general overview of how to earn NG medals!

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 03:54 AM Reply

You could always PM him and ask. I'm sure he'd love to shed some light on his efficient coding techniques.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 09:58 AM Reply

MSGhero is correct, it involved drawing bitmaps to a stage bitmap. The idea is that Flash's native displayObject classes have a lot of fluff in there for rotation and scale and whatnot that it uses a lot of memory. if you guys haven't messed with http://flixel.org/ I would recommend it.

DavidHan
DavidHan
  • Member since: Sep. 19, 2011
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 06:50 PM Reply

At 3/1/13 09:58 AM, ImpotentBoy2 wrote: MSGhero is correct, it involved drawing bitmaps to a stage bitmap. The idea is that Flash's native displayObject classes have a lot of fluff in there for rotation and scale and whatnot that it uses a lot of memory. if you guys haven't messed with http://flixel.org/ I would recommend it.

I've tried flashpunk, and I really like it, but there was one big thing that keeps me from using it. The art making workflow and limitations are just too much. Especially as I like to do my art myself, I really like, and am used to the flash IDE workflow where I could draw something, then press control + enter and see it immediately after. Also, it allowed me to nest mc's inside each other to have custom gear and whatnot. Creating and exporting new sprite sheets was just really annoying.

Anyways, back to the main topic, is selecting "export as bitmap" the same as blitting? Also, since the graphic of my block can easily be drawn with the as3 drawing tools, would that be less intensive than a bitmap image? All i need is a square with a solid fill.

DavidHan
DavidHan
  • Member since: Sep. 19, 2011
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 06:58 PM Reply

At 3/1/13 09:58 AM, ImpotentBoy2 wrote: MSGhero is correct, it involved drawing bitmaps to a stage bitmap. The idea is that Flash's native displayObject classes have a lot of fluff in there for rotation and scale and whatnot that it uses a lot of memory. if you guys haven't messed with http://flixel.org/ I would recommend it.

I've tried flashpunk, and I really like it, but there was one big thing that keeps me from using it. The art making workflow and limitations are just too much. Especially as I like to do my art myself, I really like, and am used to the flash IDE workflow where I could draw something, then press control + enter and see it immediately after. Also, it allowed me to nest mc's inside each other to have custom gear and whatnot. Creating and exporting new sprite sheets was just really annoying.

Anyways, back to the main topic, is selecting "export as bitmap" the same as blitting? Also, since the graphic of my block can easily be drawn with the as3 drawing tools, would that be less intensive than a bitmap image? All i need is a square with a solid fill.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 07:06 PM Reply

At 3/1/13 06:50 PM, DavidHan wrote: Anyways, back to the main topic, is selecting "export as bitmap" the same as blitting? Also, since the graphic of my block can easily be drawn with the as3 drawing tools, would that be less intensive than a bitmap image? All i need is a square with a solid fill.

Well, blitting is actually pasting the images onto the stage; but using bitmaps is a start.

It's not necessarily the complexity of the shape that makes blitting faster, it's that the cpu doesn't have to do as much. Vector images, like your square and solid fill, require math to move, rotate, and scale.

Also, when flash renders everything, it will take time to try to display everything that's addChilded. So your entire huge background is getting math-ed around. Each of the fifty squares all on top of each other are all getting rendered even though you can only see one. With blitting, it only renders once. CopyPixels is fast enough that it's not a problem compared to rendering. So you'd copyPx a chunk of the big background and all fifty squares, but only the bitmap gets rendered instead of 51 different things.

DavidHan
DavidHan
  • Member since: Sep. 19, 2011
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 10:35 PM Reply

At 3/1/13 07:06 PM, MSGhero wrote:
At 3/1/13 06:50 PM, DavidHan wrote: Anyways, back to the main topic, is selecting "export as bitmap" the same as blitting? Also, since the graphic of my block can easily be drawn with the as3 drawing tools, would that be less intensive than a bitmap image? All i need is a square with a solid fill.
Well, blitting is actually pasting the images onto the stage; but using bitmaps is a start.

It's not necessarily the complexity of the shape that makes blitting faster, it's that the cpu doesn't have to do as much. Vector images, like your square and solid fill, require math to move, rotate, and scale.

Also, when flash renders everything, it will take time to try to display everything that's addChilded. So your entire huge background is getting math-ed around. Each of the fifty squares all on top of each other are all getting rendered even though you can only see one. With blitting, it only renders once. CopyPixels is fast enough that it's not a problem compared to rendering. So you'd copyPx a chunk of the big background and all fifty squares, but only the bitmap gets rendered instead of 51 different things.

Cool. Does export to bitmap do the same thing?

Also,the memory is only at 9~ (kb?) when the lag starts dropping. All of my graphics right now are placeholders, so theyre just solid rectangles.

Does high memory = low fps?
does this mean that the problems are in my code?

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Hundreds of Entities with no lag? Mar. 1st, 2013 @ 11:55 PM Reply

At 3/1/13 10:35 PM, DavidHan wrote: Does high memory = low fps?
does this mean that the problems are in my code?

its possible. I know flash punk was designed in a way similarly to flixel, so I'm going to talk in terms a flixel, assuming that there is some kind of equivalent in flashpunk(I haven't used flashpunk but I hear good things)

I had a game with like 50 coins on screen and I would call flixel's collide() function on each of them, the result was a laggy game. all i had to do was put all of the coins in a FlxGroup and call collide on the group, and it stopped lagging and still told me which coin was hit. the group wasn't a layer or displayobject, it was just a kind of array but the main event of the flxGroup was the quadtree, which organizes its children by location. so when I call hero.collide(coinGroup); the collide function only tested coins "fairly close" to the hero, causing my lag to completely disappear. look into flashpunk's quad trees if they exist. if not google it and make your own, its just a bunch of abstract tiles grouping the objects by location.

PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to Hundreds of Entities with no lag? Mar. 2nd, 2013 @ 02:52 AM Reply

At 3/1/13 10:35 PM, DavidHan wrote: Also,the memory is only at 9~ (kb?) when the lag starts dropping. All of my graphics right now are placeholders, so theyre just solid rectangles.

Does high memory = low fps?

Memory has nothing to do with your actual performance, unless your computer is starting to not have enough memory etc. etc..
But in some high performance cases, you need muchos memory, like in the case of blitting, you'd be generating and storing several large BitmapData's of sprite information, which will take up memory, but performance should be uber fast.

P.

prexen
prexen
  • Member since: Jun. 14, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Hundreds of Entities with no lag? Mar. 6th, 2013 @ 06:49 PM Reply

Collision detection / spatial partioning and a rendering efficient pipeline.

For collision detection a simple grid to place the "coins" to reduce pairwise tests.
The rendering, if you are going for performance forget movieclips / sprites. Use copypixels in a bitmapdata. Ive never seen anything as fast as copypixels in a software render.

With those two you can get a LOT (place a more than a lot here) of stuff on screen. The physics arent really an issue in the game you mentioned.