How to move bullets in a pattern without lag spike

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DaZel77

Hello, I am trying to make a bullet hell game, and I am calculating the sin(x) of each bullet in every frame to move it. This is causing lag when the number of bullets goes up. I was wondering if anyone has any other strategies for making bullet hell patterns, or if anyone knows how to use the trig functions with less lag. I can put together an example if it is needed, but thanks in advance fr your help!

This really depends on your game and where the bottleneck is (simple sprites usually wouldn’t incur much overhead). Perhaps an alternative is to decouple the calculation from the movement code so it can run in parallel, with the movement code then just using the result.

You might also consider a plugin: GitHub - samdze/godot-native-bullets-plugin: Efficiently spawn and move high amounts of objects like bullets for bullet hells, particles and more.

spaceyjase | 2023-02-21 11:12

You may like the MultiMesh strategy: https://www.youtube.com/watch?v=-r4o3JWzw4U

TheNormandyProject | 2023-02-21 11:49

@spaceyjase, the plugin you mention isn’t compatible with Godot 4 at time of writing.

TheNormandyProject | 2023-02-21 11:51

Do you think that there would be a noticeable difference using a multimesh over sprites? Right now I am just instancing sprites and using the PhyscisServer2D to move them.

DaZel77 | 2023-02-21 14:48

:bust_in_silhouette: Reply From: Inces

Also, are You sure it is the movement that causes lag spikes ? I would rather think, that the lag lies in instantiating and childing massive amounts of bullets. GDQuest made some good tutorials about precaching bullets for bullet hell game, that eliminates the lag entirely.

:bust_in_silhouette: Reply From: godot_dev_

What Inces pointed out was my guess too. I bet it’s the number of projectiles being instanced that is lagging the game. You should try removing any movement logic from the bullets, and try to shoot to see if it still lags. If the game lags when you shoot without moving the bullets, the number of instances are lagging the game. If it doesn’t lag, then the sin function is lagging the game.

If the number of instances is the source of the lag, I suggest pre-instancing your bullets, so that they already exist (invisible with disabled collision boxes) when you shoot them, so you just have to make them visible and activate the collision detection instead of adding a new bullet to the scene tree each time

Not just instancing, queue_free() can also cause this in quick succession.
A render / pool bucket is the best way to go like Inces said

Wakatta | 2023-02-22 00:17