How to add C++ objects (new module) with custom physics?

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

Hi everybody!
For a little experiment I’m doing I’d like to develop a new C++ module that adds some objects with their own physics.
I’m not going to develop a whole, brand new physics server; I’d simply like to have some C++ objects that can be added to the scene and that can “move” with their own physics (they won’t interact with other Godot physical objects).
What is the best way to do it? I was thinking about extending the “spatial” objects and overriding the “physics_process” method in my C++ code, just like I’d do it in GDscript; but is it the “Godot way” to do it?
Before you ask: I need to write the code in C++ because it’s quite CPU-intensive and it won’t be possible to do it in GDscript.
Thank you!

:bust_in_silhouette: Reply From: Calinou

What is the best way to do it? I was thinking about extending the “spatial” objects and overriding the “physics_process” method in my C++ code, just like I’d do it in GDscript; but is it the “Godot way” to do it?

That sounds fine to me. There’s _integrate_forces() for RigidBodies, but since you’re writing your own physics, you can’t use it anyway.

Thank you very much for your answer, Calinou!
But then, I’ve a second one: how can I override the “physics-process” method, with a custom C++ class? I’ve tried to implement my own “void -notification(int)” method, but it gets called ONLY if there’s a GDscript attached that overrides the “physics_process” method. How can I have my C++ methods be called during the physics process phase, even if they are not GDscripted?..

the.Alien | 2020-03-24 15:31

I’ve tried to override the _process or _physicsprocess methods in my C++ classes, but that doesn’t work. Only implementing the _notification method works to some extent, but only if I then attach a GDscript to the objects…

the.Alien | 2020-03-24 16:17

:bust_in_silhouette: Reply From: qdeanc

Was struggling with this just today.
Here’s what worked for me. Special thanks to this Reddit post
You can quickly implement this into the Summator example module.
I added a lot of comments to make it self-explanatory.

// Declare this method in the header file!
// During the main loop (main_loop.h), the scene tree (scene_tree.h) will call the _notification method
// on all nodes within the scene tree (organized by process priority).
// This process happens whenever the engine updates (which includes the editor), but the notifications sent (as the p_notification parameter) are different
// depending on what's happening in the engine. 
void Summator::_notification(int p_notification)
{
    // This line will stop the rest of this method from running in the editor.
    // No #includes needed for the "Engine" class.
    //if(Engine::get_singleton()->is_editor_hint()) { return; }

    // Find out and print which notification the scene tree is sending.
    switch(p_notification)
    {
        case NOTIFICATION_READY:
        {
            // By default, the node isn't set to process (data.process = false).
            // If the node's data.process value is false, it will not receive notifications during the engine's regular updates.
            set_process(true);
		
            print_line("NOTIFICATION_READY");
        }
        case NOTIFICATION_PROCESS:
        {
            print_line("NOTIFICATION_PROCESS");
        }
        case NOTIFICATION_PHYSICS_PROCESS:
        {
            print_line("NOTIFICATION_PHYSICS_PROCESS");
        }
        // Have a block of code to default to in case some other notification was sent.
        default:
        {
            break;
        }
    }
}