Remove no-op locking in Jolt Physics module

This commit is contained in:
Mikael Hermansson
2025-04-25 14:12:38 +02:00
parent 28089c40c1
commit 2b88477efc
18 changed files with 253 additions and 926 deletions

View File

@@ -83,12 +83,12 @@ void JoltArea3D::_add_to_space() {
jolt_settings->SetShape(jolt_shape);
const JPH::BodyID new_jolt_id = space->add_rigid_body(*this, *jolt_settings);
if (new_jolt_id.IsInvalid()) {
JPH::Body *new_jolt_body = space->add_rigid_body(*this, *jolt_settings);
if (new_jolt_body == nullptr) {
return;
}
jolt_id = new_jolt_id;
jolt_body = new_jolt_body;
delete jolt_settings;
jolt_settings = nullptr;
@@ -107,8 +107,7 @@ void JoltArea3D::_dequeue_call_queries() {
}
void JoltArea3D::_add_shape_pair(Overlap &p_overlap, const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
const JoltReadableBody3D other_jolt_body = space->read_body(p_body_id);
const JoltShapedObject3D *other_object = other_jolt_body.as_shaped();
const JoltShapedObject3D *other_object = space->try_get_shaped(p_body_id);
ERR_FAIL_NULL(other_object);
p_overlap.rid = other_object->get_rid();
@@ -187,25 +186,15 @@ void JoltArea3D::_report_event(const Callable &p_callback, PhysicsServer3D::Area
}
void JoltArea3D::_notify_body_entered(const JPH::BodyID &p_body_id) {
const JoltReadableBody3D jolt_body = space->read_body(p_body_id);
JoltBody3D *body = jolt_body.as_body();
if (unlikely(body == nullptr)) {
return;
if (JoltBody3D *other_body = space->try_get_body(p_body_id)) {
other_body->add_area(this);
}
body->add_area(this);
}
void JoltArea3D::_notify_body_exited(const JPH::BodyID &p_body_id) {
const JoltReadableBody3D jolt_body = space->read_body(p_body_id);
JoltBody3D *body = jolt_body.as_body();
if (unlikely(body == nullptr)) {
return;
if (JoltBody3D *other_body = space->try_get_body(p_body_id)) {
other_body->remove_area(this);
}
body->remove_area(this);
}
void JoltArea3D::_force_bodies_entered() {
@@ -291,10 +280,7 @@ void JoltArea3D::_update_group_filter() {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetCollisionGroup().SetGroupFilter(JoltGroupFilter::instance);
jolt_body->GetCollisionGroup().SetGroupFilter(JoltGroupFilter::instance);
}
void JoltArea3D::_update_default_gravity() {
@@ -384,7 +370,7 @@ void JoltArea3D::set_transform(Transform3D p_transform) {
jolt_settings->mPosition = to_jolt_r(p_transform.origin);
jolt_settings->mRotation = to_jolt(p_transform.basis);
} else {
space->get_body_iface().SetPositionAndRotation(jolt_id, to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
space->get_body_iface().SetPositionAndRotation(jolt_body->GetID(), to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
}
}

View File

@@ -144,12 +144,12 @@ void JoltBody3D::_add_to_space() {
jolt_settings->SetShape(jolt_shape);
const JPH::BodyID new_jolt_id = space->add_rigid_body(*this, *jolt_settings, sleep_initially);
if (new_jolt_id.IsInvalid()) {
JPH::Body *new_jolt_body = space->add_rigid_body(*this, *jolt_settings, sleep_initially);
if (new_jolt_body == nullptr) {
return;
}
jolt_id = new_jolt_id;
jolt_body = new_jolt_body;
delete jolt_settings;
jolt_settings = nullptr;
@@ -295,14 +295,9 @@ JPH::MassProperties JoltBody3D::_calculate_mass_properties() const {
}
void JoltBody3D::_update_mass_properties() {
if (!in_space()) {
return;
if (in_space()) {
jolt_body->GetMotionPropertiesUnchecked()->SetMassProperties(_calculate_allowed_dofs(), _calculate_mass_properties());
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetMotionPropertiesUnchecked()->SetMassProperties(_calculate_allowed_dofs(), _calculate_mass_properties());
}
void JoltBody3D::_update_gravity(JPH::Body &p_jolt_body) {
@@ -407,10 +402,7 @@ void JoltBody3D::_update_possible_kinematic_contacts() {
if (!in_space()) {
jolt_settings->mCollideKinematicVsNonDynamic = value;
} else {
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->SetCollideKinematicVsNonDynamic(value);
jolt_body->SetCollideKinematicVsNonDynamic(value);
}
}
@@ -419,13 +411,9 @@ void JoltBody3D::_update_sleep_allowed() {
if (!in_space()) {
jolt_settings->mAllowSleeping = value;
return;
} else {
jolt_body->SetAllowSleeping(value);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->SetAllowSleeping(value);
}
void JoltBody3D::_destroy_joint_constraints() {
@@ -435,8 +423,10 @@ void JoltBody3D::_destroy_joint_constraints() {
}
void JoltBody3D::_exit_all_areas() {
ERR_FAIL_COND(!in_space());
for (JoltArea3D *area : areas) {
area->body_exited(jolt_id, false);
area->body_exited(jolt_body->GetID(), false);
}
areas.clear();
@@ -447,13 +437,9 @@ void JoltBody3D::_update_group_filter() {
if (!in_space()) {
jolt_settings->mCollisionGroup.SetGroupFilter(group_filter);
return;
} else {
jolt_body->GetCollisionGroup().SetGroupFilter(group_filter);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetCollisionGroup().SetGroupFilter(group_filter);
}
void JoltBody3D::_mode_changed() {
@@ -559,7 +545,7 @@ void JoltBody3D::set_transform(Transform3D p_transform) {
} else if (is_kinematic()) {
kinematic_transform = p_transform;
} else {
space->get_body_iface().SetPositionAndRotation(jolt_id, to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
space->get_body_iface().SetPositionAndRotation(jolt_body->GetID(), to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
}
_transform_changed();
@@ -694,29 +680,20 @@ void JoltBody3D::set_custom_integrator(bool p_enabled) {
custom_integrator = p_enabled;
if (!in_space()) {
_motion_changed();
return;
if (in_space()) {
jolt_body->ResetForce();
jolt_body->ResetTorque();
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->ResetForce();
body->ResetTorque();
_motion_changed();
}
bool JoltBody3D::is_sleeping() const {
if (!in_space()) {
return sleep_initially;
} else {
return !jolt_body->IsActive();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), false);
return !body->IsActive();
}
bool JoltBody3D::is_sleep_actually_allowed() const {
@@ -726,15 +703,12 @@ bool JoltBody3D::is_sleep_actually_allowed() const {
void JoltBody3D::set_is_sleeping(bool p_enabled) {
if (!in_space()) {
sleep_initially = p_enabled;
return;
}
JPH::BodyInterface &body_iface = space->get_body_iface();
if (p_enabled) {
body_iface.DeactivateBody(jolt_id);
} else {
body_iface.ActivateBody(jolt_id);
if (p_enabled) {
space->get_body_iface().DeactivateBody(jolt_body->GetID());
} else {
space->get_body_iface().ActivateBody(jolt_body->GetID());
}
}
}
@@ -749,85 +723,60 @@ void JoltBody3D::set_is_sleep_allowed(bool p_enabled) {
}
Basis JoltBody3D::get_principal_inertia_axes() const {
ERR_FAIL_NULL_V_MSG(space, Basis(), vformat("Failed to retrieve principal inertia axes of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_V_MSG(!in_space(), Basis(), vformat("Failed to retrieve principal inertia axes of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(is_static() || is_kinematic())) {
return Basis();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Basis());
return to_godot(body->GetRotation() * body->GetMotionProperties()->GetInertiaRotation());
return to_godot(jolt_body->GetRotation() * jolt_body->GetMotionPropertiesUnchecked()->GetInertiaRotation());
}
Vector3 JoltBody3D::get_inverse_inertia() const {
ERR_FAIL_NULL_V_MSG(space, Vector3(), vformat("Failed to retrieve inverse inertia of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_V_MSG(!in_space(), Vector3(), vformat("Failed to retrieve inverse inertia of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(is_static() || is_kinematic())) {
return Vector3();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
const JPH::MotionProperties &motion_properties = *body->GetMotionPropertiesUnchecked();
return to_godot(motion_properties.GetLocalSpaceInverseInertia().GetDiagonal3());
return to_godot(jolt_body->GetMotionPropertiesUnchecked()->GetLocalSpaceInverseInertia().GetDiagonal3());
}
Basis JoltBody3D::get_inverse_inertia_tensor() const {
ERR_FAIL_NULL_V_MSG(space, Basis(), vformat("Failed to retrieve inverse inertia tensor of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_V_MSG(!in_space(), Basis(), vformat("Failed to retrieve inverse inertia tensor of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(is_static() || is_kinematic())) {
return Basis();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Basis());
return to_godot(body->GetInverseInertia()).basis;
return to_godot(jolt_body->GetInverseInertia()).basis;
}
void JoltBody3D::set_linear_velocity(const Vector3 &p_velocity) {
if (is_static() || is_kinematic()) {
linear_surface_velocity = p_velocity;
_motion_changed();
return;
} else {
if (!in_space()) {
jolt_settings->mLinearVelocity = to_jolt(p_velocity);
} else {
jolt_body->GetMotionPropertiesUnchecked()->SetLinearVelocityClamped(to_jolt(p_velocity));
}
}
if (!in_space()) {
jolt_settings->mLinearVelocity = to_jolt(p_velocity);
_motion_changed();
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetMotionPropertiesUnchecked()->SetLinearVelocityClamped(to_jolt(p_velocity));
_motion_changed();
}
void JoltBody3D::set_angular_velocity(const Vector3 &p_velocity) {
if (is_static() || is_kinematic()) {
angular_surface_velocity = p_velocity;
_motion_changed();
return;
} else {
if (!in_space()) {
jolt_settings->mAngularVelocity = to_jolt(p_velocity);
} else {
jolt_body->GetMotionPropertiesUnchecked()->SetAngularVelocityClamped(to_jolt(p_velocity));
}
}
if (!in_space()) {
jolt_settings->mAngularVelocity = to_jolt(p_velocity);
_motion_changed();
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetMotionPropertiesUnchecked()->SetAngularVelocityClamped(to_jolt(p_velocity));
_motion_changed();
}
@@ -840,9 +789,6 @@ void JoltBody3D::set_axis_velocity(const Vector3 &p_axis_velocity) {
linear_velocity += p_axis_velocity;
jolt_settings->mLinearVelocity = to_jolt(linear_velocity);
} else {
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
Vector3 linear_velocity = get_linear_velocity();
linear_velocity -= axis * axis.dot(linear_velocity);
linear_velocity += p_axis_velocity;
@@ -857,14 +803,10 @@ Vector3 JoltBody3D::get_velocity_at_position(const Vector3 &p_position) const {
return Vector3();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
const JPH::MotionProperties &motion_properties = *body->GetMotionPropertiesUnchecked();
const JPH::MotionProperties &motion_properties = *jolt_body->GetMotionPropertiesUnchecked();
const Vector3 total_linear_velocity = to_godot(motion_properties.GetLinearVelocity()) + linear_surface_velocity;
const Vector3 total_angular_velocity = to_godot(motion_properties.GetAngularVelocity()) + angular_surface_velocity;
const Vector3 com_to_pos = p_position - to_godot(body->GetCenterOfMassPosition());
const Vector3 com_to_pos = p_position - to_godot(jolt_body->GetCenterOfMassPosition());
return total_linear_velocity + total_angular_velocity.cross(com_to_pos);
}
@@ -894,14 +836,10 @@ void JoltBody3D::set_max_contacts_reported(int p_count) {
if (!in_space()) {
jolt_settings->mUseManifoldReduction = use_manifold_reduction;
_contact_reporting_changed();
return;
} else {
space->get_body_iface().SetUseManifoldReduction(jolt_body->GetID(), use_manifold_reduction);
}
JPH::BodyInterface &body_iface = space->get_body_iface();
body_iface.SetUseManifoldReduction(jolt_id, use_manifold_reduction);
_contact_reporting_changed();
}
@@ -963,115 +901,73 @@ void JoltBody3D::reset_mass_properties() {
}
void JoltBody3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || custom_integrator || p_force == Vector3()) {
return;
}
if (custom_integrator || p_force == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddForce(to_jolt(p_force), body->GetPosition() + to_jolt(p_position));
jolt_body->AddForce(to_jolt(p_force), jolt_body->GetPosition() + to_jolt(p_position));
_motion_changed();
}
void JoltBody3D::apply_central_force(const Vector3 &p_force) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply central force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply central force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || custom_integrator || p_force == Vector3()) {
return;
}
if (custom_integrator || p_force == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddForce(to_jolt(p_force));
jolt_body->AddForce(to_jolt(p_force));
_motion_changed();
}
void JoltBody3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
return;
}
if (p_impulse == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddImpulse(to_jolt(p_impulse), body->GetPosition() + to_jolt(p_position));
jolt_body->AddImpulse(to_jolt(p_impulse), jolt_body->GetPosition() + to_jolt(p_position));
_motion_changed();
}
void JoltBody3D::apply_central_impulse(const Vector3 &p_impulse) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply central impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply central impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
return;
}
if (p_impulse == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddImpulse(to_jolt(p_impulse));
jolt_body->AddImpulse(to_jolt(p_impulse));
_motion_changed();
}
void JoltBody3D::apply_torque(const Vector3 &p_torque) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply torque to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply torque to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || custom_integrator || p_torque == Vector3()) {
return;
}
if (custom_integrator || p_torque == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddTorque(to_jolt(p_torque));
jolt_body->AddTorque(to_jolt(p_torque));
_motion_changed();
}
void JoltBody3D::apply_torque_impulse(const Vector3 &p_impulse) {
ERR_FAIL_NULL_MSG(space, vformat("Failed to apply torque impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply torque impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
if (unlikely(!is_rigid())) {
if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
return;
}
if (p_impulse == Vector3()) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->AddAngularImpulse(to_jolt(p_impulse));
jolt_body->AddAngularImpulse(to_jolt(p_impulse));
_motion_changed();
}
@@ -1091,9 +987,6 @@ void JoltBody3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_pos
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
constant_force += p_force;
constant_torque += (p_position - get_center_of_mass_relative()).cross(p_force);
@@ -1260,22 +1153,19 @@ void JoltBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {
const JPH::EMotionType motion_type = _get_motion_type();
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
if (motion_type == JPH::EMotionType::Static) {
put_to_sleep();
}
body->SetMotionType(motion_type);
jolt_body->SetMotionType(motion_type);
if (motion_type != JPH::EMotionType::Static) {
wake_up();
}
if (motion_type == JPH::EMotionType::Kinematic) {
body->SetLinearVelocity(JPH::Vec3::sZero());
body->SetAngularVelocity(JPH::Vec3::sZero());
jolt_body->SetLinearVelocity(JPH::Vec3::sZero());
jolt_body->SetAngularVelocity(JPH::Vec3::sZero());
}
linear_surface_velocity = Vector3();
@@ -1287,11 +1177,9 @@ void JoltBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {
bool JoltBody3D::is_ccd_enabled() const {
if (!in_space()) {
return jolt_settings->mMotionQuality == JPH::EMotionQuality::LinearCast;
} else {
return space->get_body_iface().GetMotionQuality(jolt_body->GetID()) == JPH::EMotionQuality::LinearCast;
}
const JPH::BodyInterface &body_iface = space->get_body_iface();
return body_iface.GetMotionQuality(jolt_id) == JPH::EMotionQuality::LinearCast;
}
void JoltBody3D::set_ccd_enabled(bool p_enabled) {
@@ -1299,12 +1187,9 @@ void JoltBody3D::set_ccd_enabled(bool p_enabled) {
if (!in_space()) {
jolt_settings->mMotionQuality = motion_quality;
return;
} else {
space->get_body_iface().SetMotionQuality(jolt_body->GetID(), motion_quality);
}
JPH::BodyInterface &body_iface = space->get_body_iface();
body_iface.SetMotionQuality(jolt_id, motion_quality);
}
void JoltBody3D::set_mass(float p_mass) {
@@ -1324,47 +1209,33 @@ void JoltBody3D::set_inertia(const Vector3 &p_inertia) {
float JoltBody3D::get_bounce() const {
if (!in_space()) {
return jolt_settings->mRestitution;
} else {
return jolt_body->GetRestitution();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), 0.0f);
return body->GetRestitution();
}
void JoltBody3D::set_bounce(float p_bounce) {
if (!in_space()) {
jolt_settings->mRestitution = p_bounce;
return;
} else {
jolt_body->SetRestitution(p_bounce);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->SetRestitution(p_bounce);
}
float JoltBody3D::get_friction() const {
if (!in_space()) {
return jolt_settings->mFriction;
} else {
return jolt_body->GetFriction();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), 0.0f);
return body->GetFriction();
}
void JoltBody3D::set_friction(float p_friction) {
if (!in_space()) {
jolt_settings->mFriction = p_friction;
return;
} else {
jolt_body->SetFriction(p_friction);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->SetFriction(p_friction);
}
void JoltBody3D::set_gravity_scale(float p_scale) {

View File

@@ -37,13 +37,12 @@
#include "jolt_group_filter.h"
void JoltObject3D::_remove_from_space() {
if (unlikely(jolt_id.IsInvalid())) {
if (!in_space()) {
return;
}
space->remove_body(jolt_id);
jolt_id = JPH::BodyID();
space->remove_body(jolt_body->GetID());
jolt_body = nullptr;
}
void JoltObject3D::_reset_space() {
@@ -60,7 +59,7 @@ void JoltObject3D::_update_object_layer() {
return;
}
space->get_body_iface().SetObjectLayer(jolt_id, _get_object_layer());
space->get_body_iface().SetObjectLayer(jolt_body->GetID(), _get_object_layer());
}
void JoltObject3D::_collision_layer_changed() {

View File

@@ -66,7 +66,7 @@ protected:
RID rid;
ObjectID instance_id;
JoltSpace3D *space = nullptr;
JPH::BodyID jolt_id;
JPH::Body *jolt_body = nullptr;
uint32_t collision_layer = 1;
uint32_t collision_mask = 1;
@@ -121,11 +121,12 @@ public:
void set_instance_id(ObjectID p_id) { instance_id = p_id; }
Object *get_instance() const;
JPH::BodyID get_jolt_id() const { return jolt_id; }
JPH::Body *get_jolt_body() const { return jolt_body; }
JPH::BodyID get_jolt_id() const { return jolt_body->GetID(); }
JoltSpace3D *get_space() const { return space; }
void set_space(JoltSpace3D *p_space);
bool in_space() const { return space != nullptr && !jolt_id.IsInvalid(); }
bool in_space() const { return space != nullptr && jolt_body != nullptr; }
uint32_t get_collision_layer() const { return collision_layer; }
void set_collision_layer(uint32_t p_layer);

View File

@@ -178,11 +178,8 @@ void JoltShapedObject3D::_space_changing() {
previous_jolt_shape = nullptr;
if (space != nullptr) {
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
jolt_settings = new JPH::BodyCreationSettings(body->GetBodyCreationSettings());
if (in_space()) {
jolt_settings = new JPH::BodyCreationSettings(jolt_body->GetBodyCreationSettings());
}
}
@@ -208,12 +205,9 @@ JoltShapedObject3D::~JoltShapedObject3D() {
Transform3D JoltShapedObject3D::get_transform_unscaled() const {
if (!in_space()) {
return Transform3D(to_godot(jolt_settings->mRotation), to_godot(jolt_settings->mPosition));
} else {
return Transform3D(to_godot(jolt_body->GetRotation()), to_godot(jolt_body->GetPosition()));
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Transform3D());
return Transform3D(to_godot(body->GetRotation()), to_godot(body->GetPosition()));
}
Transform3D JoltShapedObject3D::get_transform_scaled() const {
@@ -223,32 +217,22 @@ Transform3D JoltShapedObject3D::get_transform_scaled() const {
Basis JoltShapedObject3D::get_basis() const {
if (!in_space()) {
return to_godot(jolt_settings->mRotation);
} else {
return to_godot(jolt_body->GetRotation());
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Basis());
return to_godot(body->GetRotation());
}
Vector3 JoltShapedObject3D::get_position() const {
if (!in_space()) {
return to_godot(jolt_settings->mPosition);
} else {
return to_godot(jolt_body->GetPosition());
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
return to_godot(body->GetPosition());
}
Vector3 JoltShapedObject3D::get_center_of_mass() const {
ERR_FAIL_NULL_V_MSG(space, Vector3(), vformat("Failed to retrieve center-of-mass of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
return to_godot(body->GetCenterOfMassPosition());
ERR_FAIL_COND_V_MSG(!in_space(), Vector3(), vformat("Failed to retrieve center-of-mass of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
return to_godot(jolt_body->GetCenterOfMassPosition());
}
Vector3 JoltShapedObject3D::get_center_of_mass_relative() const {
@@ -264,23 +248,17 @@ Vector3 JoltShapedObject3D::get_center_of_mass_local() const {
Vector3 JoltShapedObject3D::get_linear_velocity() const {
if (!in_space()) {
return to_godot(jolt_settings->mLinearVelocity);
} else {
return to_godot(jolt_body->GetLinearVelocity());
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
return to_godot(body->GetLinearVelocity());
}
Vector3 JoltShapedObject3D::get_angular_velocity() const {
if (!in_space()) {
return to_godot(jolt_settings->mAngularVelocity);
} else {
return to_godot(jolt_body->GetAngularVelocity());
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
return to_godot(body->GetAngularVelocity());
}
AABB JoltShapedObject3D::get_aabb() const {
@@ -321,9 +299,6 @@ void JoltShapedObject3D::commit_shapes(bool p_optimize_compound) {
return;
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::ShapeRefC new_shape = build_shapes(p_optimize_compound);
if (new_shape == jolt_shape) {
return;
@@ -332,7 +307,7 @@ void JoltShapedObject3D::commit_shapes(bool p_optimize_compound) {
previous_jolt_shape = jolt_shape;
jolt_shape = new_shape;
space->get_body_iface().SetShape(jolt_id, jolt_shape, false, JPH::EActivation::DontActivate);
space->get_body_iface().SetShape(jolt_body->GetID(), jolt_shape, false, JPH::EActivation::DontActivate);
_enqueue_shapes_changed();

View File

@@ -78,15 +78,12 @@ JPH::ObjectLayer JoltSoftBody3D::_get_object_layer() const {
void JoltSoftBody3D::_space_changing() {
JoltObject3D::_space_changing();
_deref_shared_data();
if (space != nullptr && !jolt_id.IsInvalid()) {
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
jolt_settings = new JPH::SoftBodyCreationSettings(body->GetSoftBodyCreationSettings());
if (in_space()) {
jolt_settings = new JPH::SoftBodyCreationSettings(jolt_body->GetSoftBodyCreationSettings());
jolt_settings->mSettings = nullptr;
}
_deref_shared_data();
}
void JoltSoftBody3D::_space_changed() {
@@ -117,12 +114,12 @@ void JoltSoftBody3D::_add_to_space() {
jolt_settings->mCollisionGroup = JPH::CollisionGroup(nullptr, group_id, sub_group_id);
jolt_settings->mMaxLinearVelocity = JoltProjectSettings::max_linear_velocity;
const JPH::BodyID new_jolt_id = space->add_soft_body(*this, *jolt_settings);
if (new_jolt_id.IsInvalid()) {
JPH::Body *new_jolt_body = space->add_soft_body(*this, *jolt_settings);
if (new_jolt_body == nullptr) {
return;
}
jolt_id = new_jolt_id;
jolt_body = new_jolt_body;
delete jolt_settings;
jolt_settings = nullptr;
@@ -237,11 +234,7 @@ void JoltSoftBody3D::_update_mass() {
return;
}
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();
const float inverse_vertex_mass = mass == 0.0f ? 1.0f : (float)physics_vertices.size() / mass;
@@ -259,11 +252,7 @@ void JoltSoftBody3D::_update_pressure() {
return;
}
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
motion_properties.SetPressure(pressure);
}
@@ -273,11 +262,7 @@ void JoltSoftBody3D::_update_damping() {
return;
}
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
motion_properties.SetLinearDamping(linear_damping);
}
@@ -287,11 +272,7 @@ void JoltSoftBody3D::_update_simulation_precision() {
return;
}
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
motion_properties.SetNumIterations((JPH::uint32)simulation_precision);
}
@@ -300,13 +281,9 @@ void JoltSoftBody3D::_update_group_filter() {
if (!in_space()) {
jolt_settings->mCollisionGroup.SetGroupFilter(group_filter);
return;
} else {
jolt_body->GetCollisionGroup().SetGroupFilter(group_filter);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->GetCollisionGroup().SetGroupFilter(group_filter);
}
void JoltSoftBody3D::_try_rebuild() {
@@ -407,21 +384,16 @@ void JoltSoftBody3D::set_mesh(const RID &p_mesh) {
}
_deref_shared_data();
mesh = p_mesh;
_mesh_changed();
}
bool JoltSoftBody3D::is_sleeping() const {
if (!in_space()) {
return false;
} else {
return !jolt_body->IsActive();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), false);
return !body->IsActive();
}
void JoltSoftBody3D::set_is_sleeping(bool p_enabled) {
@@ -432,32 +404,26 @@ void JoltSoftBody3D::set_is_sleeping(bool p_enabled) {
JPH::BodyInterface &body_iface = space->get_body_iface();
if (p_enabled) {
body_iface.DeactivateBody(jolt_id);
body_iface.DeactivateBody(jolt_body->GetID());
} else {
body_iface.ActivateBody(jolt_id);
body_iface.ActivateBody(jolt_body->GetID());
}
}
bool JoltSoftBody3D::is_sleep_allowed() const {
if (!in_space()) {
return true;
return jolt_settings->mAllowSleeping;
} else {
return jolt_body->GetAllowSleeping();
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), false);
return body->GetAllowSleeping();
}
void JoltSoftBody3D::set_is_sleep_allowed(bool p_enabled) {
if (!in_space()) {
return;
jolt_settings->mAllowSleeping = p_enabled;
} else {
jolt_body->SetAllowSleeping(p_enabled);
}
const JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
body->SetAllowSleeping(p_enabled);
}
void JoltSoftBody3D::set_simulation_precision(int p_precision) {
@@ -571,9 +537,6 @@ Transform3D JoltSoftBody3D::get_transform() const {
void JoltSoftBody3D::set_transform(const Transform3D &p_transform) {
ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to set transform for '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
// For whatever reason this has to be interpreted as a relative global-space transform rather than an absolute one,
// because `SoftBody3D` will immediately upon entering the scene tree set itself to be top-level and also set its
// transform to be identity, while still expecting to stay in its original position.
@@ -581,7 +544,7 @@ void JoltSoftBody3D::set_transform(const Transform3D &p_transform) {
// We also discard any scaling, since we have no way of scaling the actual edge lengths.
const JPH::Mat44 relative_transform = to_jolt(p_transform.orthonormalized());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();
for (JPH::SoftBodyVertex &vertex : physics_vertices) {
@@ -592,11 +555,7 @@ void JoltSoftBody3D::set_transform(const Transform3D &p_transform) {
AABB JoltSoftBody3D::get_bounds() const {
ERR_FAIL_COND_V_MSG(!in_space(), AABB(), vformat("Failed to retrieve world bounds of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), AABB());
return to_godot(body->GetWorldSpaceBounds());
return to_godot(jolt_body->GetWorldSpaceBounds());
}
void JoltSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) {
@@ -605,10 +564,7 @@ void JoltSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandl
return;
}
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
const JPH::SoftBodyMotionProperties &motion_properties = static_cast<const JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
const JPH::SoftBodyMotionProperties &motion_properties = static_cast<const JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
typedef JPH::SoftBodyMotionProperties::Vertex SoftBodyVertex;
typedef JPH::SoftBodyMotionProperties::Face SoftBodyFace;
@@ -660,14 +616,11 @@ Vector3 JoltSoftBody3D::get_vertex_position(int p_index) {
ERR_FAIL_INDEX_V(p_index, (int)shared->mesh_to_physics.size(), Vector3());
const size_t physics_index = (size_t)shared->mesh_to_physics[p_index];
const JoltReadableBody3D body = space->read_body(jolt_id);
ERR_FAIL_COND_V(body.is_invalid(), Vector3());
const JPH::SoftBodyMotionProperties &motion_properties = static_cast<const JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
const JPH::SoftBodyMotionProperties &motion_properties = static_cast<const JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
const JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();
const JPH::SoftBodyVertex &physics_vertex = physics_vertices[physics_index];
return to_godot(body->GetCenterOfMassPosition() + physics_vertex.mPosition);
return to_godot(jolt_body->GetCenterOfMassPosition() + physics_vertex.mPosition);
}
void JoltSoftBody3D::set_vertex_position(int p_index, const Vector3 &p_position) {
@@ -682,15 +635,11 @@ void JoltSoftBody3D::set_vertex_position(int p_index, const Vector3 &p_position)
return;
}
JoltWritableBody3D body = space->write_body(jolt_id);
ERR_FAIL_COND(body.is_invalid());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*body->GetMotionPropertiesUnchecked());
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();
JPH::SoftBodyVertex &physics_vertex = physics_vertices[physics_index];
const JPH::RVec3 center_of_mass = body->GetCenterOfMassPosition();
const JPH::RVec3 center_of_mass = jolt_body->GetCenterOfMassPosition();
const JPH::Vec3 local_position = JPH::Vec3(to_jolt_r(p_position) - center_of_mass);
const JPH::Vec3 displacement = local_position - physics_vertex.mPosition;
const JPH::Vec3 velocity = displacement / last_step;