forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Jolts FixedConstraint joint
- Loading branch information
1 parent
4ce466d
commit 5d383ee
Showing
16 changed files
with
349 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="FixedJoint3D" inherits="Joint3D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> | ||
<brief_description> | ||
A physics joint that attaches two 3D physics bodies at a single point, with no allowed movement between the two bodies. | ||
</brief_description> | ||
<description> | ||
A physics joint that attaches two 3D physics bodies at a single point, with no allowed movement between the two bodies. | ||
[b]Note:[/b] This joint is only implemented for the Jolt Physics Engine. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/**************************************************************************/ | ||
/* jolt_pin_joint_3d.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "jolt_fixed_joint_3d.h" | ||
|
||
#include "../misc/jolt_type_conversions.h" | ||
#include "../objects/jolt_body_3d.h" | ||
#include "../spaces/jolt_space_3d.h" | ||
|
||
#include "Jolt/Physics/Constraints/FixedConstraint.h" | ||
|
||
JPH::Constraint *JoltFixedJoint3D::_build_fixed(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b) { | ||
JPH::FixedConstraintSettings constraint_settings; | ||
constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM; | ||
constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin); | ||
constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin); | ||
|
||
if (p_jolt_body_a == nullptr) { | ||
return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b); | ||
} else if (p_jolt_body_b == nullptr) { | ||
return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld); | ||
} else { | ||
return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b); | ||
} | ||
} | ||
|
||
void JoltFixedJoint3D::_points_changed() { | ||
rebuild(); | ||
_wake_up_bodies(); | ||
} | ||
|
||
JoltFixedJoint3D::JoltFixedJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Vector3 &p_local_a, const Vector3 &p_local_b) : | ||
JoltJoint3D(p_old_joint, p_body_a, p_body_b, Transform3D({}, p_local_a), Transform3D({}, p_local_b)) { | ||
rebuild(); | ||
} | ||
|
||
void JoltFixedJoint3D::set_local_a(const Vector3 &p_local_a) { | ||
local_ref_a = Transform3D({}, p_local_a); | ||
_points_changed(); | ||
} | ||
|
||
void JoltFixedJoint3D::set_local_b(const Vector3 &p_local_b) { | ||
local_ref_b = Transform3D({}, p_local_b); | ||
_points_changed(); | ||
} | ||
|
||
float JoltFixedJoint3D::get_applied_force() const { | ||
JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr()); | ||
ERR_FAIL_NULL_V(constraint, 0.0f); | ||
|
||
JoltSpace3D *space = get_space(); | ||
ERR_FAIL_NULL_V(space, 0.0f); | ||
|
||
const float last_step = space->get_last_step(); | ||
if (unlikely(last_step == 0.0f)) { | ||
return 0.0f; | ||
} | ||
|
||
return constraint->GetTotalLambdaPosition().Length() / last_step; | ||
} | ||
|
||
void JoltFixedJoint3D::rebuild() { | ||
destroy(); | ||
|
||
JoltSpace3D *space = get_space(); | ||
|
||
if (space == nullptr) { | ||
return; | ||
} | ||
|
||
const JPH::BodyID body_ids[2] = { | ||
body_a != nullptr ? body_a->get_jolt_id() : JPH::BodyID(), | ||
body_b != nullptr ? body_b->get_jolt_id() : JPH::BodyID() | ||
}; | ||
|
||
const JoltWritableBodies3D jolt_bodies = space->write_bodies(body_ids, 2); | ||
|
||
JPH::Body *jolt_body_a = static_cast<JPH::Body *>(jolt_bodies[0]); | ||
JPH::Body *jolt_body_b = static_cast<JPH::Body *>(jolt_bodies[1]); | ||
|
||
ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr); | ||
|
||
Transform3D shifted_ref_a; | ||
Transform3D shifted_ref_b; | ||
|
||
_shift_reference_frames(Vector3(), Vector3(), shifted_ref_a, shifted_ref_b); | ||
|
||
jolt_ref = _build_fixed(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b); | ||
|
||
space->add_joint(this); | ||
|
||
_update_enabled(); | ||
_update_iterations(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/**************************************************************************/ | ||
/* jolt_fixed_joint_3d.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef JOLT_FIXED_JOINT_3D_H | ||
#define JOLT_FIXED_JOINT_3D_H | ||
|
||
#include "jolt_joint_3d.h" | ||
|
||
#include "Jolt/Jolt.h" | ||
|
||
#include "Jolt/Physics/Constraints/FixedConstraint.h" | ||
|
||
class JoltFixedJoint3D final : public JoltJoint3D { | ||
static JPH::Constraint *_build_fixed(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b); | ||
|
||
void _points_changed(); | ||
|
||
public: | ||
JoltFixedJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Vector3 &p_local_a, const Vector3 &p_local_b); | ||
|
||
virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_FIXED; } | ||
|
||
Vector3 get_local_a() const { return local_ref_a.origin; } | ||
void set_local_a(const Vector3 &p_local_a); | ||
|
||
Vector3 get_local_b() const { return local_ref_b.origin; } | ||
void set_local_b(const Vector3 &p_local_b); | ||
|
||
float get_applied_force() const; | ||
|
||
virtual void rebuild() override; | ||
}; | ||
|
||
#endif // JOLT_FIXED_JOINT_3D_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/**************************************************************************/ | ||
/* fixed_joint_3d.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "fixed_joint_3d.h" | ||
|
||
void FixedJoint3D::_bind_methods() { | ||
} | ||
|
||
void FixedJoint3D::_configure_joint(RID p_joint, PhysicsBody3D *body_a, PhysicsBody3D *body_b) { | ||
Vector3 fixedpos = get_global_transform().origin; | ||
Vector3 local_a = body_a->to_local(fixedpos); | ||
Vector3 local_b; | ||
|
||
if (body_b) { | ||
local_b = body_b->to_local(fixedpos); | ||
} else { | ||
local_b = fixedpos; | ||
} | ||
|
||
PhysicsServer3D::get_singleton()->joint_make_fixed(p_joint, body_a->get_rid(), local_a, body_b ? body_b->get_rid() : RID(), local_b); | ||
} | ||
|
||
FixedJoint3D::FixedJoint3D() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/**************************************************************************/ | ||
/* fixed_joint_3d.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef FIXED_JOINT_3D_H | ||
#define FIXED_JOINT_3D_H | ||
|
||
#include "scene/3d/physics/joints/joint_3d.h" | ||
|
||
class FixedJoint3D : public Joint3D { | ||
GDCLASS(FixedJoint3D, Joint3D); | ||
|
||
public: | ||
protected: | ||
virtual void _configure_joint(RID p_joint, PhysicsBody3D *body_a, PhysicsBody3D *body_b) override; | ||
static void _bind_methods(); | ||
|
||
public: | ||
FixedJoint3D(); | ||
}; | ||
|
||
#endif // FIXED_JOINT_3D_H |
Oops, something went wrong.