Hands-On Optimization - Collisions
Howdy,
The stickied optimization thread has some great stuff in it, but I always learn better when I get my hands dirty. The point of this thread is to give a place for everyone to actually go in and see if they can optimize working code (or at least watch other people go in and optimize working code :)).
I'm obviously focusing on collision detection here, and for our first run at it I figured we could start with the most straight-forward method I can think of - each object handling its own movement and detection. I'm all for looking at the other approaches (centralized handling w/ bookeeping, tilebased, etc.), but ya gotta start somewhere :).
So without furter adeu...
De-Centralized Method
Basic Description
Each square calls a central function to preform it's collision detection and movement on enterFrame. I've taken all the steps I can think of to remove any redundant checks (squares only check for collisions with objects created after themselves, if they have a collision, they tell the other square to skip it's check, etc.). Regardless, its all pretty basic, so I am sure there is plenty of room for improvement.
Examples
Note - either use the .html or download the .swf and open it in the standalone player for the most consistent results (prevents scaling).
Instructions
*Upper Left Button - Adds a square
*Upper Right Button - Removes a square
*Lower left text - maximum velocity (changable)
*Lower right text - current FPS
Full collision detection: .html .swf
This one has everything turned on.
With 30 objects on screen, my FPS stays around 29-30.
No Physics: .html .swf
Same basic idea, but I commented out the meat of the object-object calculations (still goes through the loops, just doesn't do anything inside of them).
With 30 on screen, I get about 38 fps, which means the physics crap is taking some decent calculation time
Walls Only: .html .swf
This time I skip the object-object checks completely.
I can get a crapload (90-100ish) of objects on the screen before FPS takes a hit.
"Optimized" Full Collision: .html .swf
On this one I went through and stored off any object references (i.e. "object._x") to a variable which I used in place of the actual reference whenever possible. The end result was the FPS taking a severe nose dive to around...
30 objects on screen, 22ish FPS.
Fun stuff :/
.FLA source: Click Me
Just what it sounds like, the original .FLA. This version does not have any of the optimization stuff I did in the last example (since it didn't help FPS, I didn't see much of a point). As for the content, pretty much everything of importance is in frame 1 of the Actions level, main timeline.
Let me know and I'll go through and explain what I did in the code in more depth.
So whadaya say? :)
Edit: Oh yah, and I am using Flash 5 (doing it from the home machine), sorry for any inconvenience :/
Re: Hands-On Optimization - Collisions
Quote:
Originally posted by TiefighT
Walls Only: .html .swf
This time I skip the object-object checks completely.
I can get a crapload (90-100ish) of objects on the screen before FPS takes a hit.
I wonder if your fps counter is accurate.
You see, it starts fine, around 30, then if I add and add and add objects, it starts to raise. When I have around 80 objects on screen, fps has raised to 45.
If raising fps would be just a matter of adding 80 objects on screen, we wouldnt need optimisation at all :)