-Loop optimization for moving objects-
Normally, whenever you make multiple objects that require precise movement, you loop through each one with a foreach and then start a different set of actual movement fastloops for each object. The process looks something like this:
-objects that need to move are looped through with a foreach loop
-the ID of each object is recorded on this foreach,
-movement fastloops are run for each individual object and its ID is compared, to move it pixel-by-pixel according to its velocities, also checking for collision
Let's assume that you have 5 objects moving at 5 pixels per second on the X axis. Using the above method, that is
25 Fastloops required for movement. As your object count grows, so does the number of fastloops, until eventually, the sheer amount of loops bogs your application down. My personal limit on my (old and very weak) laptop is about 170 moving objects with collision before the FPS drops low enough to become unplayable. That adds up to at worst
850 fastloops fired. I could probably manage more objects just moving around if they didn't require collision, but in action games collision is usually a necessity.
Naturally, the limitations of the traditional method with fastloops got to me, and I sat around brainstorming for an hour or so. The following method is the result of that, and astoundingly simple:
-objects that need to move are looped through with a foreach loop
-the highest absolute velocity of the objects is recorded (which would be 5, like earlier)
-movement fastloops are run only 5 times
-each object decides to move by checking whether its absolute velocity is >= to the move loop index
Using this method, you don't need to specify which object is currently moving, because Fusion does it for you: any object with an absolute velocity higher than the loop index is scoped. To remove an object from the loop, you would set its absolute velocity to 0, which means it wouldn't be scoped whenever the loop fires. Thanks to the comparison to the loopindex, you can also have objects that move at independent speeds. If an object only has a velocity of 1, it will remove itself from the scope after one loop, whereas another object would keep going through iterations until it reaches its cap.
Now lets go back to our earlier scenario, of 5 objects moving at 5 pixels per second. With the new method, that amounts
to only 5 fastloops vs the old 25! Starting to look like an improvement, but let's check what my new object limit is using the new method:
2000

Now
there is a significant improvement! The second method yields a total object gain of
1830! All moving simultaneously, all with collisions working!
And there are still only 5 loops running each frame. The old method would be using
10,000 loops each frame, and probably crash the application on even the strongest computer!
You don't need to worry about objects having independent movements, like an enemy that uses a certain behavior or the player's standard movement. All you need to do is handle physics separately, before the movement loops happen. I added a couple different behaviors in the following example for you to play around with:
Attachment 22862
(I threw the "un-optimized" examples together quickly, so there are leftover values from the normal examples in there and the debug string isn't accurate. Ignore them. Fine detection is also OFF by default. Turning it on doesn't change much, though. My FPS dropped from 45 on average to 30-40 on average in the optimized bench.)
I don't think I need to explain the "Real world application" benefits for this method, do I? Practically everyone will benefit, especially mobile users where loops completely wipe their performance.
