Image() is slow

I made an asteroid video game, each asteroid breaking into hundreds of pieces, and those pieces breaking into hundreds of pieces. you can imagine how many fragments there are.
each fragment takes a random direction.
each fragment has its image (spr_fragment, x, y).
but image () is very slow when called many times, it goes down from 60fps to 5fps.

I forgot to say that each fragment must detect collision with ship bullets, to be destroyed when colliding with the ship’s bullet, and until they are destroyed they must move with image (spr_fragment, x, y). it is not a simple animation.

How can I render many fragments without it getting slow, without losing fps, processing?

Hi,

why don’t use a simple animated gif or a video for this ? If you prepare some animations, you can keep the randomness appearance of your animation, and you don’t need to load a lot of images

Another idea : don’t calculate their position at each draw, it is actually useless. Around 24 frames / seconds is enough to make it smooth. You can do it like this :

if (frameCount % 2.5 == 0){ // 60/24 = 2.5
// move fragments
}

And you can also use your graphic card by setting your sketch in P3D

size(300,300, P3D);

If you do so, you’ll have to replace everything in front of the camera : size() \ Language (API) \ Processing 3+

1 Like

Use tiles. Create one large image with all the fragments. Use the 9-arg version of image() to draw them.

image(tiles, destX, destY, destWidth, destHeight, srcTopLeftX, srcTopLeftY, srcBottomRightX, srcBottomRightY);

Note that the 4th and 5th parameters are relative dimensions (width and height) while the last two are absolute coordinates.

1 Like

why not use fractals instead? You can use a series of lines and then form new fractals on the hit event.

I forgot to say that each fragment must detect collision with ship bullets, to be destroyed when colliding with ship bullet. it is not a simple animation.