Optimization Using Servers AND GDNative / C++

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

I’ve created a Naive approach Nbody simulation just to test performance in godot, with gdscript it can run with about 200 rigid bodies, cool, i recreated the nbody part of the code in a GDnative C++ library, and it can run about 600 rigid bodies, better, but with a few optimizations and basic multithreading within the C++ code, i was able to get it running smoothly with 1200 bodies.

However, I would like to find the best method of rendering all these bodies, currently im passing in a reference to each rigidbody to the gdnative library where it keeps a list of all them.

I noticed that with no gravity, just rigidbodies sitting there doing nothing, godot will start lagging once i spawn around 5000 of them, each of these instances have 2 nodes to them, so thats 10000 nodes.

I’ve looked into the “optimization using servers” and have attempted to recreate first part of rendering a sprite in C++ to no avail, it works fine it gdscript. I’ve researched as much as i could to find out what im doing wrong but idk, it will just crash every time i try running my game, this is what i have so far:

    void RbSpawner::_init() 

    //create canvas item, child of node
    RID canvasitemrid = Visual->canvas_item_create();
    RID thisID = get_canvas_item();

    //made this node parent
    Visual->canvas_item_set_parent(canvasitemrid, thisID);
    img = ReLo->load("res://red.bmp","res//red.bmp");
    //draw sprite on it
    //this is a reference!

    //add it centered
    Visual->canvas_item_add_texture_rect(canvasitemrid, Rect2(sprite->get_texture()->get_size(), sprite->get_texture()->get_size()),sprite);
    //add the item 45 degress rotated
    Transform2D xform;
    //translated (should be down right)

And in my header file here are the variable for this class:

//member variables
    ResourceLoader* ReLo; 
    VisualServer* Visual;
    //physics server is an interface for low level 2d physics access.
    Physics2DServer* Physics;
    //handle for resources unique ID (doesn't grant access to resource by itself, need a server to do stuff.)
    RID body;
    Ref<Image> img;
    Ref<World2D> world;
    Ref<RectangleShape2D> shape;
    Sprite* sprite;

I’ve tried many variations of that, i hope its a simple fix, Is it even recommended to do this? how much faster would this be than using gdscript to run it? i guess even just running this in gdscript would be much faster than instancing all my rigidbodies in gdscript, but i would like it to run as fast as possible, but still be easy to develop. from what i understand i’d essentially have to replace the scene system for the objects i don’t in it.

btw i was unable to use Ref for many things because i got the error

Ref.hpp:180:31: error: 'class godot::ResourceLoader' has no member named 'unreference'

thank you for reading

i guess i should mention im on 3.2.1 and downloaded the headers about a week ago

osimmac | 2020-04-01 08:10

I’d try asking on IRC at this point (#godotengine-gdnative on irc.freenode.net), since this Q&A platform doesn’t have a lot of GDNative users.

Also, keep in mind rendering might be the bottleneck at this point due to the sheer amount of draw calls.

Calinou | 2020-04-01 08:21

each body just has 1 texture and its only 1 pixel big, i would hope godot rendering/ my computer could handle more, CPU particles can spawn quite a bit, at least 30,000. but yeah its a lot to ask for, im simply stress testing for now, in a game i would optimize the heck out of this and remove objects far away from the player give them a more abstract representation.

im am curious what the most optimal path is though, it would be awesome to have a very fast gravity simulator working in godot, even just 1200 particles it looks pretty cool, 10,000 would be pretty(though unrealistic)

osimmac | 2020-04-01 09:01