Character3D now uses the standard CharacterSkeleton3D build.

This commit is contained in:
2026-09-19 02:07:00 -04:00
parent 38f539e432
commit 205ab4357a
2 changed files with 123 additions and 52 deletions
@@ -1,5 +1,5 @@
/* /*
* ©2023 Batty Bovine Productions, LLC. All Rights Reserved. * ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
* *
* If this source code makes it to the public internet, this software can be * If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it. * considered to be protected by the MIT licence. Have fun with it.
@@ -9,26 +9,51 @@
#include "orng_macros.h" #include "orng_macros.h"
#include "nodes/debug_draw3d.h"
#include "nodes/mesh_editing_library.h" #include "nodes/mesh_editing_library.h"
#include <godot_cpp/classes/engine.hpp> #include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/immediate_mesh.hpp>
#include <godot_cpp/classes/physical_bone3d.hpp> #include <godot_cpp/classes/physical_bone3d.hpp>
#include <godot_cpp/classes/physical_bone_simulator3d.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
#include <godot_cpp/classes/skin.hpp>
#include <godot_cpp/variant/utility_functions.hpp> #include <godot_cpp/variant/utility_functions.hpp>
using namespace godot; using namespace godot;
void Character3D::_ready() void Character3D::_ready()
{ {
if (this->skeleton) if (this->character_skeleton)
{ {
TypedArray<Node> child_mesh_candidates = this->skeleton->get_children(); if (const Skeleton3D *skeleton = this->character_skeleton->get_skeleton())
const uint32_t num_children = child_mesh_candidates.size();
for (uint32_t i = 0; i < num_children; i++)
{ {
if (MeshInstance3D *mesh = cast_to<MeshInstance3D>(child_mesh_candidates[i])) TypedArray<Node> child_mesh_candidates = skeleton->get_children();
const uint32_t num_children = child_mesh_candidates.size();
const Vector3 &full_aabb_position = this->full_aabb.get_position();
const Vector3 &full_aabb_size = this->full_aabb.get_size();
for (uint32_t i = 0; i < num_children; i++)
{ {
this->meshes.emplace_back(mesh); if (MeshInstance3D *mesh_instance = cast_to<MeshInstance3D>(child_mesh_candidates[i]))
{
this->meshes.emplace_back(mesh_instance);
const AABB &aabb = mesh_instance->get_aabb();
const Vector3 &aabb_position = aabb.get_position();
const Vector3 &aabb_size = aabb.get_size();
this->full_aabb.set_position(Vector3(
UtilityFunctions::minf(full_aabb_position.x, aabb_position.x),
UtilityFunctions::minf(full_aabb_position.y, aabb_position.y),
UtilityFunctions::minf(full_aabb_position.z, aabb_position.z)
));
this->full_aabb.set_size(Vector3(
UtilityFunctions::maxf(full_aabb_position.x, aabb_size.x),
UtilityFunctions::maxf(full_aabb_position.y, aabb_size.y),
UtilityFunctions::maxf(full_aabb_position.z, aabb_size.z)
));
}
} }
} }
} }
@@ -43,39 +68,91 @@ void Character3D::_ready()
void Character3D::take_damage(const Dictionary &hit_data) void Character3D::take_damage(const Dictionary &hit_data)
{ {
PRINT_LOG(CHARACTER3D_TAG, "Taking damage: ", hit_data["hit_points"]); if (this->character_skeleton)
PRINT_LOG(CHARACTER3D_TAG, "Hit bones: ", hit_data["bone_hits"]);
const Transform3D &global_hit_transform = (Transform3D)hit_data["hit_transform"];
const Plane &slice_plane = this->skeleton->get_global_transform().inverse().xform(Plane(global_hit_transform.get_basis().get_column(1), global_hit_transform.origin));
const uint32_t num_meshes = this->meshes.size();
for (uint32_t i = 0; i < num_meshes; i++)
{ {
MeshInstance3D *original_mesh_instance = this->meshes[i]; if (const Skeleton3D *skeleton = this->character_skeleton->get_skeleton())
Ref<ArrayMesh> first_half; first_half.instantiate(); {
Ref<ArrayMesh> other_half; other_half.instantiate(); PRINT_LOG(CHARACTER3D_TAG, "Taking damage: ", hit_data["hit_points"]);
PackedVector3Array &impact_points = MeshEditingLibrary::slice_mesh(original_mesh_instance, this->skeleton, slice_plane, first_half, other_half, MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS, original_mesh_instance->get_active_material(0)); PRINT_LOG(CHARACTER3D_TAG, "Hit bones: ", hit_data["bone_hits"]);
if (other_half.is_valid()) const Transform3D &global_hit_transform = (Transform3D)hit_data["hit_transform"];
original_mesh_instance->set_mesh(other_half); const Plane &slice_plane = skeleton->get_global_transform().inverse().xform(Plane(global_hit_transform.get_basis().get_column(1), global_hit_transform.origin));
const uint32_t num_meshes = this->meshes.size();
std::vector<MeshInstance3D*> meshes_to_add;
std::vector<uint32_t> meshes_to_remove;
for (uint32_t i = 0; i < num_meshes; i++)
{
MeshInstance3D *original_mesh_instance = this->meshes[i];
Ref<ArrayMesh> first_half; first_half.instantiate();
Ref<ArrayMesh> other_half; other_half.instantiate();
PackedVector3Array &impact_points = MeshEditingLibrary::slice_mesh(original_mesh_instance, skeleton, this->full_aabb, slice_plane, first_half, other_half, MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS, this->inner_materials);
if (first_half.is_valid())
{
original_mesh_instance->set_mesh(first_half);
meshes_to_add.emplace_back(original_mesh_instance);
}
if (other_half.is_valid())
{
RandomNumberGenerator *rng = memnew(RandomNumberGenerator);
const float random_x = rng->randf_range(-0.5f, 0.5f);
const float random_z = rng->randf_range(-0.5f, 0.5f);
CharacterSkeleton3D *new_character_skeleton = cast_to<CharacterSkeleton3D>(this->character_skeleton->duplicate());
this->character_skeleton->add_sibling(new_character_skeleton);
new_character_skeleton->set_position(Vector3(random_x, 0.0f, random_z));
Skeleton3D *new_skeleton = new_character_skeleton->get_skeleton();
const uint16_t child_count = new_skeleton->get_child_count();
TypedArray<Node> children = new_skeleton->get_children();
for (uint16_t child = 0; child < child_count; child++)
{
if (MeshInstance3D *new_mesh_instance = cast_to<MeshInstance3D>(children[child]))
{
new_mesh_instance->set_skeleton_path(new_mesh_instance->get_path_to(new_skeleton));
new_mesh_instance->set_skin(original_mesh_instance->get_skin());
new_mesh_instance->set_mesh(other_half);
meshes_to_add.emplace_back(new_mesh_instance);
break;
}
}
}
meshes_to_remove.emplace(meshes_to_remove.begin(), i);
}
for (const uint32_t i : meshes_to_remove)
{
this->meshes.erase(this->meshes.begin() + i);
}
for (MeshInstance3D *mesh : meshes_to_add)
{
this->meshes.emplace_back(mesh);
}
}
} }
} }
void Character3D::_activate_ragdoll() void Character3D::_activate_ragdoll()
{ {
if (this->physical_bone_simulator) if (this->character_skeleton)
{ {
const TypedArray<Node> &physical_bones = this->physical_bone_simulator->get_children(); if (PhysicalBoneSimulator3D *physical_bone_simulator = this->character_skeleton->get_physical_bone_simulator())
const uint32_t num_physical_bones = physical_bones.size();
for (uint32_t i = 0; i < num_physical_bones; i++)
{ {
if (PhysicalBone3D *bone = cast_to<PhysicalBone3D>(physical_bones[i])) const TypedArray<Node> &physical_bones = physical_bone_simulator->get_children();
const uint32_t num_physical_bones = physical_bones.size();
for (uint32_t i = 0; i < num_physical_bones; i++)
{ {
bone->set_collision_layer(0); if (PhysicalBone3D *bone = cast_to<PhysicalBone3D>(physical_bones[i]))
{
bone->set_collision_layer(0);
}
} }
physical_bone_simulator->physical_bones_start_simulation();
} }
this->physical_bone_simulator->physical_bones_start_simulation();
} }
} }
@@ -1,5 +1,5 @@
/* /*
* ©2023 Batty Bovine Productions, LLC. All Rights Reserved. * ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
* *
* If this source code makes it to the public internet, this software can be * If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it. * considered to be protected by the MIT licence. Have fun with it.
@@ -10,13 +10,12 @@
#include "orng_macros.h" #include "orng_macros.h"
#include "interfaces/i_damageable.h" #include "interfaces/i_damageable.h"
#include "nodes/character_nodes/character_skeleton3d.h"
#include <map> #include <map>
#include <godot_cpp/classes/animation_tree.hpp>
#include <godot_cpp/classes/character_body3d.hpp> #include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/material.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp> #include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/physical_bone_simulator3d.hpp>
#include <godot_cpp/classes/skeleton3d.hpp>
#include <godot_cpp/variant/dictionary.hpp> #include <godot_cpp/variant/dictionary.hpp>
using namespace godot; using namespace godot;
@@ -32,23 +31,24 @@ public:
virtual void take_damage(const Dictionary &hit_data) override; virtual void take_damage(const Dictionary &hit_data) override;
Skeleton3D *get_skeleton() const { return this->skeleton; } CharacterSkeleton3D *get_character_skeleton() const { return this->character_skeleton; }
PhysicalBoneSimulator3D *get_physical_bone_simulator() const { return this->physical_bone_simulator; }
AnimationTree *get_animation_tree() const { return this->animation_tree; } TypedArray<Material> get_inner_materials() const { return this->inner_materials; }
virtual void _on_fell_out_of_world(); virtual void _on_fell_out_of_world();
protected: protected:
void set_skeleton(Skeleton3D *s) { this->skeleton = s; } void set_character_skeleton(CharacterSkeleton3D *cs) { this->character_skeleton = cs; }
void set_physical_bone_simulator(PhysicalBoneSimulator3D *pbs) { this->physical_bone_simulator = pbs; }
void set_animation_tree(AnimationTree *at) { this->animation_tree = at; } void set_inner_materials(TypedArray<Material> materials) { this->inner_materials = materials; }
private: private:
void _activate_ragdoll(); void _activate_ragdoll();
Skeleton3D *skeleton = nullptr; CharacterSkeleton3D *character_skeleton = nullptr;
PhysicalBoneSimulator3D *physical_bone_simulator = nullptr;
AnimationTree *animation_tree = nullptr; AABB full_aabb;
TypedArray<Material> inner_materials;
std::vector<MeshInstance3D*> meshes; std::vector<MeshInstance3D*> meshes;
@@ -60,16 +60,10 @@ protected:
ADD_SIGNAL(MethodInfo("fell_out_of_world")); ADD_SIGNAL(MethodInfo("fell_out_of_world"));
ClassDB::bind_method(D_METHOD("get_skeleton"), &Character3D::get_skeleton); ClassDB::bind_method(D_METHOD("get_character_skeleton"), &Character3D::get_character_skeleton);
ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Character3D::set_skeleton); ClassDB::bind_method(D_METHOD("set_character_skeleton", "character_skeleton"), &Character3D::set_character_skeleton);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skeleton", PROPERTY_HINT_NODE_TYPE, "Skeleton3D"), "set_skeleton", "get_skeleton"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "character_skeleton", PROPERTY_HINT_NODE_TYPE, "CharacterSkeleton3D"), "set_character_skeleton", "get_character_skeleton");
ClassDB::bind_method(D_METHOD("get_physical_bone_simulator"), &Character3D::get_physical_bone_simulator); ADD_GETTER_SETTER_HINTED(Character3D, inner_materials, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "Material");
ClassDB::bind_method(D_METHOD("set_physical_bone_simulator", "physical_bone_simulator"), &Character3D::set_physical_bone_simulator);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physical_bone_simulator", PROPERTY_HINT_NODE_TYPE, "PhysicalBoneSimulator3D"), "set_physical_bone_simulator", "get_physical_bone_simulator");
ClassDB::bind_method(D_METHOD("get_animation_tree"), &Character3D::get_animation_tree);
ClassDB::bind_method(D_METHOD("set_animation_tree", "animation_tree"), &Character3D::set_animation_tree);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "animation_tree", PROPERTY_HINT_NODE_TYPE, "AnimationTree"), "set_animation_tree", "get_animation_tree");
} }
}; };