Godot Version
Godot 4.3 stable , godot-cpp 4.3 stable
Question
Physics does not work directly through the PhysicServer3D. ( С++ )
Hello everyone!
For the third day now I’ve been trying to understand why my C++ code doesn’t work. I tried to use a PhysicServer3D
directly. However, no attempts helped me make a simple sphere move or at least fall.
I’ve already studied several topics on the forum. It didn’t help me.
Samantha’s topic was very useful, but unfortunately there was no result =(
At first I used Flecs systems to access the PhysicServer3D
. I thought maybe it was a lambda function or multithreading issue, but now I can’t get even the simplest code to work as expected.
So, this is simple Node3D
class, nothing more now.
cpp
void PhysicsManager::_ready()
{
test_body = ps->body_create();
sphere = ps->sphere_shape_create();
space = get_world_3d()->get_space();
ps->shape_set_data(sphere, 0.5f);
ps->body_set_space(test_body, space);
ps->body_add_shape(test_body, sphere);
ps->body_set_shape_transform(test_body, 0, Transform3D());
ps->body_set_mode(test_body, PhysicsServer3D::BODY_MODE_RIGID_LINEAR);
ps->body_set_param(test_body, PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, 1.0f);
ps->body_set_param(test_body, PhysicsServer3D::BODY_PARAM_MASS, 10.0f);
ps->body_set_collision_layer(test_body, 1);
ps->body_set_state(test_body,PhysicsServer3D::BODY_STATE_CAN_SLEEP, false);
Transform3D b_tr = get_global_transform().translated(Vector3(0.0f, 10.0f, 0.0f));
ps->body_set_state(test_body, PhysicsServer3D::BODY_STATE_TRANSFORM, b_tr);
ps->body_set_state(test_body, PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, Vector3(0.1f, 0.0f, 0.05f));
}
void PhysicsManager::_physics_process(double delta)
{
if(running)
{
ps->body_add_constant_central_force(test_body, Vector3(0.0f, 0.25f, 0.0f));
Transform3D body_transform = ps->body_get_state(test_body, PhysicsServer3D::BODY_STATE_TRANSFORM);
UtilityFunctions::print(body_transform);
UtilityFunctions::print(ps->body_get_state(test_body, PhysicsServer3D::BODY_STATE_SLEEPING));
UtilityFunctions::print(ps->body_get_state(test_body, PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY));
}
}
I tried almost all parameters, maybe it will help: MASS, LINEAR_VELOCITY, GRAVITY_SCALE, _set_collision_layer
this is log
log
Godot Engine v4.3.stable.arch_linux - https://godotengine.org
Vulkan 1.2.180 - Forward+ - Using Device #0: AMD - AMD Radeon RX 580 Series
_ready
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
[X: (1, 0, 0), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 10, 0)]
false
(0.1, 0, 0.05)
clearing
WARNING: 1 RIDs in Godot Jolt were found to not have been freed. This is likely caused by orphaned nodes. If not, consider reporting this issue.
The body just hangs at a height of 10 meters and that’s it.
I’m not a C++ pro at all. I might miss something, so I apologize if it turns out to be some really stupid mistake. =)