#Performance issue when creating a 3d crowd

6 messages · Page 1 of 1 (latest)

untold rain
#

Hello! I am quite new to game development and have just encountered my first big issue with performance.

I am basically trying to create a crowd of npcs that spawn on one end of the map and try to reach the other end. I want to emulate running through a crowd that is walking in the opposite direction the player is. All while being chased by one big enemy.

I read up on ways to improve performance and this currently how I have set it up:

  • I am using object pooling, all the npcs that I want in the scene are instantiated beforehand and added to the navigation region. Once they reach their destination, they are warped back to their spawn location. So they are being reused.
  • The npcs have their collision and process turned off automatically, and only activate once they start moving (one by one) according to a timer I have set up in the main scene.

The issues I have are:

  • Whenever there is a simple collision between **ANY **character bodies, there is a big loss in frames.
  • There is also a loss in frames whenever I get closer to the spawn location.

I would really appreciate any feedback from someone that has had similar problems when using a large volume of 3d character bodies.

frozen wing
#

Hi there 😊,

If I recall and understand you correctly, you toggle collisions (shapes) on and off frequently? This is bad for performance, as the collision shape needs to be "readded" to the physics space which incurs evaluations.

Is there any other code involved, (E.g. Area3D) which triggers on contact?

How are the NPCs moved? Navigation agents?

Did you check the debugger monitors where most time is spent and how often certain functions are called?

untold rain
# frozen wing Hi there 😊, If I recall and understand you correctly, you toggle collisions (s...

Oh I did not use the monitors.I have not read the documentation for that so I will be checking those out in a few hours. As for the other questions you asked:

  • So far no other code involved except 'on_target_reached' signals, though I will probably add some other code later on.
  • NPCs all use navigation agents.

So far I think it would be better if I have the npcs spawn at a different location an then teleport them to their starting position as shown in the video. I'll let you know much later once I experiment based on your feedback! Hopefully I don't have to remove the navAgents, since I really liked how the npcs were behaving.

sterile kayak
#

on such a simple navigation mesh with basically no polygon edges the actual pathfinding performance cost will be hardly noticeable even with many agents. If you would enable avoidance on all of them that would be more a performance concern but the pathfinding alone isnt.

#

When a simple character collision causes a frame drop the most likely cause is CharacterBody3D move_and_slide() or test_motions() . Both those physics functions have a brutal performance cost at scale with many characters especially when shapes more complex than simple spheres are involved.

untold rain
#

Quick update: The reason for a lag spike whenever there was collisions between the character bodies, was because the collision shape for both was convex collision shapes. I switched the collision shapes for both the npc and player to a simple capsule shape which made all the collisions smooth now (rookie and very dumb mistake, I know). This has for the most part fixed my problem, so I will mark the post as solved. I severely underestimated the complexity of making crowds for a game. I do want to thank you both for the feedback you gave me since it led me to learning quite a bit on crowds, which I believe that what I learned after hours of tutorials and reading will help me later on.

Edit: I also made it so that the physics process was not shut off at all. I only shut off collisions, but this will likely change later.

P.S.
I even found this old post https://www.reddit.com/r/godot/comments/mgtplb/best_way_to_handle_a_large_amount_of_enemies_in_3d/ where smix8 commented lol.

Reddit

Explore this post and more from the godot community