Skip to content

Commit

Permalink
Changes based on review feedback:
Browse files Browse the repository at this point in the history
* Changed documentation wording
* Removed the deprecated length functions from Bone2D and added compatibility through _set and _get
* Changed parameters in functions to use p_parameter
* (Maybe other changes? I don't remember. All changes are based on review feedback)
  • Loading branch information
TwistedTwigleg committed Mar 26, 2021
1 parent 022d1a3 commit 6db3741
Show file tree
Hide file tree
Showing 28 changed files with 83 additions and 95 deletions.
4 changes: 2 additions & 2 deletions core/math/transform_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
return elements[0].is_equal_approx(p_transform.elements[0]) && elements[1].is_equal_approx(p_transform.elements[1]) && elements[2].is_equal_approx(p_transform.elements[2]);
}

Transform2D Transform2D::looking_at(const Vector2 target) const {
Transform2D Transform2D::looking_at(const Vector2 p_target) const {
Transform2D return_trans = Transform2D(get_rotation(), get_origin());
Vector2 target_position = affine_inverse().xform(target);
Vector2 target_position = affine_inverse().xform(p_target);
return_trans.set_rotation(return_trans.get_rotation() + (target_position * get_scale()).angle());
return return_trans;
}
Expand Down
2 changes: 1 addition & 1 deletion core/math/transform_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Transform2D {
Transform2D orthonormalized() const;
bool is_equal_approx(const Transform2D &p_transform) const;

Transform2D looking_at(const Vector2 target) const;
Transform2D looking_at(const Vector2 p_target) const;

bool operator==(const Transform2D &p_transform) const;
bool operator!=(const Transform2D &p_transform) const;
Expand Down
22 changes: 3 additions & 19 deletions doc/classes/Bone2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@
<return type="bool">
</return>
<description>
Returns whether this [code]Bone2D[/code] node is going to autocalculate it's length and bone angle using its first [code]Bone2D[/code] child node, if one exists. If there are no [code]Bone2D[/code] children, then it cannot autocalculate these values and will print a warning.
Returns whether this [code]Bone2D[/code] node is going to autocalculate its length and bone angle using its first [code]Bone2D[/code] child node, if one exists. If there are no [code]Bone2D[/code] children, then it cannot autocalculate these values and will print a warning.
</description>
</method>
<method name="get_bone_angle" qualifiers="const">
<return type="float">
</return>
<description>
Returns the angle of the bone in the [code]Bone2D[/code] node.
[b]Note[/b]: This is different from the [code]Bone2D[/code]'s rotation. The bone angle is the rotation of the bone shown by the [code]Bone2D[/code] gizmo, and because [code]Bone2D[/code] bones are based on positions, this can vary from the actual rotation of the [code]Bone2D[/code] node.
</description>
</method>
<method name="get_default_length" qualifiers="const">
<return type="float">
</return>
<description>
[i]Deprecated soon.[/i] Please use [code]get_length[/code] instead.
[b]Note:[/b] This is different from the [code]Bone2D[/code]'s rotation. The bone angle is the rotation of the bone shown by the [code]Bone2D[/code] gizmo, and because [code]Bone2D[/code] bones are based on positions, this can vary from the actual rotation of the [code]Bone2D[/code] node.
</description>
</method>
<method name="get_index_in_skeleton" qualifiers="const">
Expand Down Expand Up @@ -78,16 +71,7 @@
</argument>
<description>
Sets the bone angle for the [code]Bone2D[/code] node. This is typically set to the rotation from the [code]Bone2D[/code] node to a child [code]Bone2D[/code] node.
[b]Note[/b]: This is different from the [code]Bone2D[/code]'s rotation. The bone angle is the rotation of the bone shown by the [code]Bone2D[/code] gizmo, and because [code]Bone2D[/code] bones are based on positions, this can vary from the actual rotation of the [code]Bone2D[/code] node.
</description>
</method>
<method name="set_default_length">
<return type="void">
</return>
<argument index="0" name="default_length" type="float">
</argument>
<description>
[i]Deprecated soon.[/i] Please use [code]set_length[/code] instead.
[b]Note:[/b] This is different from the [code]Bone2D[/code]'s rotation. The bone angle is the rotation of the bone shown by the [code]Bone2D[/code] gizmo, and because [code]Bone2D[/code] bones are based on positions, this can vary from the actual rotation of the [code]Bone2D[/code] node.
</description>
</method>
<method name="set_length">
Expand Down
12 changes: 6 additions & 6 deletions doc/classes/PhysicalBone2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</brief_description>
<description>
The [code]PhysicalBone2D[/code] node is a [RigidBody2D]-based node that can be used to make [Bone2D] nodes in a [Skeleton2D] react to physics. This node is very similar to the [PhysicalBone3D] node, just for 2D instead of 3D.
[b]Note[/b]: To have the Bone2D nodes visually follow the [code]PhysicalBone2D[/code] node, use a [SkeletonModification2DPhysicalBones] modification on the [Skeleton2D] node with the [Bone2D] nodes.
[b]Note[/b]: The PhysicalBone2D node does not automatically create a [Joint2D] node to keep [code]PhysicalBone2D[/code] nodes together. You will need to create these manually. For most cases, you want to use a [PinJoint2D] node. The [code]PhysicalBone2D[/code] node can automatically configure the [Joint2D] node once it's been created as a child node.
[b]Note:[/b] To have the Bone2D nodes visually follow the [code]PhysicalBone2D[/code] node, use a [SkeletonModification2DPhysicalBones] modification on the [Skeleton2D] node with the [Bone2D] nodes.
[b]Note:[/b] The PhysicalBone2D node does not automatically create a [Joint2D] node to keep [code]PhysicalBone2D[/code] nodes together. You will need to create these manually. For most cases, you want to use a [PinJoint2D] node. The [code]PhysicalBone2D[/code] node can automatically configure the [Joint2D] node once it's been created as a child node.
</description>
<tutorials>
</tutorials>
Expand All @@ -28,7 +28,7 @@
</methods>
<members>
<member name="auto_configure_joint" type="bool" setter="set_auto_configure_joint" getter="get_auto_configure_joint" default="true">
When true, the [code]PhysicalBone2D[/code] node will automatically configure the first [Joint2D] child node. The automatic configuration is limited to setting up the node properties and positioning the [Joint2D].
If [code]true[/code], the [code]PhysicalBone2D[/code] node will automatically configure the first [Joint2D] child node. The automatic configuration is limited to setting up the node properties and positioning the [Joint2D].
</member>
<member name="bone2d_index" type="int" setter="set_bone2d_index" getter="get_bone2d_index" default="-1">
The index of the [Bone2D] node that this [code]PhysicalBone2D[/code] node is supposed to be simulating.
Expand All @@ -37,11 +37,11 @@
The [NodePath] to the [Bone2D] node that this [code]PhysicalBone2D[/code] node is supposed to be simulating.
</member>
<member name="follow_bone_when_simulating" type="bool" setter="set_follow_bone_when_simulating" getter="get_follow_bone_when_simulating" default="false">
When [code]true[/code], the [code]PhysicalBone2D[/code] will keep the transform of the bone it is bound to when simulating physics.
If [code]true[/code], the [code]PhysicalBone2D[/code] will keep the transform of the bone it is bound to when simulating physics.
</member>
<member name="simulate_physics" type="bool" setter="set_simulate_physics" getter="get_simulate_physics" default="false">
When [code]true[/code], the [code]PhysicalBone2D[/code] will start simulating using physics. When false, the [code]PhysicalBone2D[/code] will follow the transform of the [Bone2D] node.
[b]Note[/b]: To have the Bone2D nodes visually follow the [code]PhysicalBone2D[/code] node, use a [SkeletonModification2DPhysicalBones] modification on the [Skeleton2D] node with the [Bone2D] nodes.
If [code]true[/code], the [code]PhysicalBone2D[/code] will start simulating using physics. If [code]false[/code], the [code]PhysicalBone2D[/code] will follow the transform of the [Bone2D] node.
[b]Note:[/b] To have the Bone2D nodes visually follow the [code]PhysicalBone2D[/code] node, use a [SkeletonModification2DPhysicalBones] modification on the [Skeleton2D] node with the [Bone2D] nodes.
</member>
</members>
<constants>
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/SkeletonModification2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</return>
<description>
Used for drawing [b]editor-only[/b] modification gizmos. This function will only be called in the Godot editor and can be overriden to draw custom gizmos.
[b]Note[/b]: You will need to use the Skeleton2D from [method SkeletonModificationStack2D.get_skeleton] and it's draw functions, as the [SkeletonModification2D] resource cannot draw on its own.
[b]Note:[/b] You will need to use the Skeleton2D from [method SkeletonModificationStack2D.get_skeleton] and it's draw functions, as the [SkeletonModification2D] resource cannot draw on its own.
</description>
</method>
<method name="execute" qualifiers="virtual">
Expand Down Expand Up @@ -92,7 +92,7 @@
</methods>
<members>
<member name="enabled" type="bool" setter="set_enabled" getter="get_enabled" default="true">
When true, the modification's [method execute] function will be called by the [SkeletonModificationStack2D].
If [code]true[/code], the modification's [method execute] function will be called by the [SkeletonModificationStack2D].
</member>
<member name="execution_mode" type="int" setter="set_execution_mode" getter="get_execution_mode" default="0">
The execution mode for the modification. This tells the modification stack when to execute the modification. Some modifications have settings that are only availible in certain execution modes.
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/SkeletonModification2DJiggle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<argument index="0" name="use_colliders" type="bool">
</argument>
<description>
When [code]true[/code], the Jiggle modifier will take colliders into account, keeping them from entering into these collision objects.
If [code]true[/code], the Jiggle modifier will take colliders into account, keeping them from entering into these collision objects.
</description>
</method>
</methods>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/SkeletonModification2DTwoBoneIK.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
</methods>
<members>
<member name="flip_bend_direction" type="bool" setter="set_flip_bend_direction" getter="get_flip_bend_direction" default="false">
When [code]true[/code], the bones in the modification will blend outward as opposed to inwards when contracting. When [code]false[/code], the bones will bend inwards when contracting.
If [code]true[/code], the bones in the modification will blend outward as opposed to inwards when contracting. If [code]false[/code], the bones will bend inwards when contracting.
</member>
<member name="target_maximum_distance" type="float" setter="set_target_maximum_distance" getter="get_target_maximum_distance" default="0.0">
The maximum distance the target can be at. If the target is farther than this distance, the modification will solve as if it's at this maximum distance. When set to [code]0[/code], the modification will solve without distance constraints.
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/SkeletonModificationStack2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</methods>
<members>
<member name="enabled" type="bool" setter="set_enabled" getter="get_enabled" default="false">
When [code]true[/code], the modification's in the stack will be called. This is handled automatically through the [Skeleton2D] node.
If [code]true[/code], the modification's in the stack will be called. This is handled automatically through the [Skeleton2D] node.
</member>
<member name="modification_count" type="int" setter="set_modification_count" getter="get_modification_count" default="0">
The number of modifications in the stack.
Expand Down
4 changes: 4 additions & 0 deletions scene/2d/skeleton_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ bool Bone2D::_set(const StringName &p_path, const Variant &p_value) {
set_length(p_value);
} else if (path.begins_with("bone_angle")) {
set_bone_angle(Math::deg2rad(float(p_value)));
} else if (path.begins_with("default_length")) {
set_length(p_value);
}

#ifdef TOOLS_ENABLED
Expand All @@ -66,6 +68,8 @@ bool Bone2D::_get(const StringName &p_path, Variant &r_ret) const {
r_ret = get_length();
} else if (path.begins_with("bone_angle")) {
r_ret = Math::rad2deg(get_bone_angle());
} else if (path.begins_with("default_length")) {
r_ret = get_length();
}

#ifdef TOOLS_ENABLED
Expand Down
3 changes: 1 addition & 2 deletions scene/main/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ class Node : public Object {

// Editor specific node notifications
NOTIFICATION_EDITOR_PRE_SAVE = 9001,
NOTIFICATION_EDITOR_POST_SAVE = 9002

NOTIFICATION_EDITOR_POST_SAVE = 9002,
};

/* NODE/TREE */
Expand Down
57 changes: 29 additions & 28 deletions scene/resources/skeleton_modification_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
// Modification2D
///////////////////////////////////////

void SkeletonModification2D::_execute(float delta) {
call("_execute", delta);
void SkeletonModification2D::_execute(float p_delta) {
call("_execute", p_delta);

if (!enabled)
if (!enabled) {
return;
}
}

void SkeletonModification2D::_setup_modification(SkeletonModificationStack2D *p_stack) {
Expand Down Expand Up @@ -81,52 +82,52 @@ bool SkeletonModification2D::get_enabled() {
return enabled;
}

float SkeletonModification2D::clamp_angle(float angle, float min_bound, float max_bound, bool invert) {
float SkeletonModification2D::clamp_angle(float p_angle, float p_min_bound, float p_max_bound, bool p_invert) {
// Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range.
if (angle < 0) {
angle = Math_TAU + angle;
if (p_angle < 0) {
p_angle = Math_TAU + p_angle;
}

// Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order
if (min_bound < 0) {
min_bound = Math_TAU + min_bound;
if (p_min_bound < 0) {
p_min_bound = Math_TAU + p_min_bound;
}
if (max_bound < 0) {
max_bound = Math_TAU + max_bound;
if (p_max_bound < 0) {
p_max_bound = Math_TAU + p_max_bound;
}
if (min_bound > max_bound) {
float tmp = min_bound;
min_bound = max_bound;
max_bound = tmp;
if (p_min_bound > p_max_bound) {
float tmp = p_min_bound;
p_min_bound = p_max_bound;
p_max_bound = tmp;
}

// Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.
if (invert == false) {
if (angle < min_bound || angle > max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(min_bound), Math::sin(min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(max_bound), Math::sin(max_bound));
Vector2 angle_vec = Vector2(Math::cos(angle), Math::sin(angle));
if (p_invert == false) {
if (p_angle < p_min_bound || p_angle > p_max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));

if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
angle = min_bound;
p_angle = p_min_bound;
} else {
angle = max_bound;
p_angle = p_max_bound;
}
}
} else {
if (angle > min_bound && angle < max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(min_bound), Math::sin(min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(max_bound), Math::sin(max_bound));
Vector2 angle_vec = Vector2(Math::cos(angle), Math::sin(angle));
if (p_angle > p_min_bound && p_angle < p_max_bound) {
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));

if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
angle = min_bound;
p_angle = p_min_bound;
} else {
angle = max_bound;
p_angle = p_max_bound;
}
}
}
return angle;
return p_angle;
}

void SkeletonModification2D::editor_draw_angle_constraints(Bone2D *operation_bone, float min_bound, float max_bound,
Expand Down
6 changes: 3 additions & 3 deletions scene/resources/skeleton_modification_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SkeletonModification2D : public Resource {
bool _print_execution_error(bool p_condition, String p_message);

public:
virtual void _execute(float delta);
virtual void _execute(float _delta);
virtual void _setup_modification(SkeletonModificationStack2D *p_stack);
virtual void _draw_editor_gizmo();

Expand All @@ -76,8 +76,8 @@ class SkeletonModification2D : public Resource {
void set_execution_mode(int p_mode);
int get_execution_mode() const;

float clamp_angle(float angle, float min_bound, float max_bound, bool invert_clamp = false);
void editor_draw_angle_constraints(Bone2D *operation_bone, float min_bound, float max_bound, bool constraint_enabled, bool constraint_in_localspace, bool constraint_inverted);
float clamp_angle(float p_angle, float p_min_bound, float p_max_bound, bool p_invert_clamp = false);
void editor_draw_angle_constraints(Bone2D *p_operation_bone, float p_min_bound, float p_max_bound, bool p_constraint_enabled, bool p_constraint_in_localspace, bool p_constraint_inverted);

SkeletonModification2D();
};
Expand Down
10 changes: 5 additions & 5 deletions scene/resources/skeleton_modification_2d_ccdik.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void SkeletonModification2DCCDIK::_get_property_list(List<PropertyInfo> *p_list)
#endif // TOOLS_ENABLED
}

void SkeletonModification2DCCDIK::_execute(float delta) {
void SkeletonModification2DCCDIK::_execute(float p_delta) {
ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
"Modification is not setup and therefore cannot execute!");
if (!enabled) {
Expand Down Expand Up @@ -188,7 +188,7 @@ void SkeletonModification2DCCDIK::_execute(float delta) {
}
}

void SkeletonModification2DCCDIK::_execute_ccdik_joint(int p_joint_idx, Node2D *target, Node2D *tip) {
void SkeletonModification2DCCDIK::_execute_ccdik_joint(int p_joint_idx, Node2D *p_target, Node2D *p_tip) {
CCDIK_Joint_Data2D ccdik_data = ccdik_data_chain[p_joint_idx];
if (ccdik_data.bone_idx < 0 || ccdik_data.bone_idx > stack->skeleton->get_bone_count()) {
ERR_PRINT_ONCE("2D CCDIK joint: bone index not found!");
Expand All @@ -201,12 +201,12 @@ void SkeletonModification2DCCDIK::_execute_ccdik_joint(int p_joint_idx, Node2D *
if (ccdik_data.rotate_from_joint) {
// To rotate from the joint, simply look at the target!
operation_transform.set_rotation(
operation_transform.looking_at(target->get_global_transform().get_origin()).get_rotation() - operation_bone->get_bone_angle());
operation_transform.looking_at(p_target->get_global_transform().get_origin()).get_rotation() - operation_bone->get_bone_angle());
} else {
// How to rotate from the tip: get the difference of rotation needed from the tip to the target, from the perspective of the joint.
// Because we are only using the offset, we do not need to account for the bone angle of the Bone2D node.
float joint_to_tip = operation_transform.get_origin().angle_to_point(tip->get_global_transform().get_origin());
float joint_to_target = operation_transform.get_origin().angle_to_point(target->get_global_transform().get_origin());
float joint_to_tip = operation_transform.get_origin().angle_to_point(p_tip->get_global_transform().get_origin());
float joint_to_target = operation_transform.get_origin().angle_to_point(p_target->get_global_transform().get_origin());
operation_transform.set_rotation(
operation_transform.get_rotation() + (joint_to_target - joint_to_tip));
}
Expand Down
4 changes: 2 additions & 2 deletions scene/resources/skeleton_modification_2d_ccdik.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SkeletonModification2DCCDIK : public SkeletonModification2D {
void update_tip_cache();

void ccdik_joint_update_bone2d_cache(int p_joint_idx);
void _execute_ccdik_joint(int p_joint_idx, Node2D *target, Node2D *tip);
void _execute_ccdik_joint(int p_joint_idx, Node2D *p_target, Node2D *p_tip);

protected:
static void _bind_methods();
Expand All @@ -77,7 +77,7 @@ class SkeletonModification2DCCDIK : public SkeletonModification2D {
void _get_property_list(List<PropertyInfo> *p_list) const;

public:
void _execute(float delta) override;
void _execute(float p_delta) override;
void _setup_modification(SkeletonModificationStack2D *p_stack) override;
void _draw_editor_gizmo() override;

Expand Down
2 changes: 1 addition & 1 deletion scene/resources/skeleton_modification_2d_fabrik.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void SkeletonModification2DFABRIK::_get_property_list(List<PropertyInfo> *p_list
}
}

void SkeletonModification2DFABRIK::_execute(float delta) {
void SkeletonModification2DFABRIK::_execute(float p_delta) {
ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
"Modification is not setup and therefore cannot execute!");
if (!enabled) {
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/skeleton_modification_2d_fabrik.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SkeletonModification2DFABRIK : public SkeletonModification2D {
void _get_property_list(List<PropertyInfo> *p_list) const;

public:
void _execute(float delta) override;
void _execute(float p_delta) override;
void _setup_modification(SkeletonModificationStack2D *p_stack) override;

void set_target_node(const NodePath &p_target_node);
Expand Down
Loading

0 comments on commit 6db3741

Please sign in to comment.