Path: blob/master/modules/godot_physics_3d/godot_soft_body_3d.h
10277 views
/**************************************************************************/1/* godot_soft_body_3d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "godot_area_3d.h"33#include "godot_collision_object_3d.h"3435#include "core/math/aabb.h"36#include "core/math/dynamic_bvh.h"37#include "core/math/vector3.h"38#include "core/templates/hash_set.h"39#include "core/templates/local_vector.h"40#include "core/templates/vset.h"4142class GodotConstraint3D;4344class GodotSoftBody3D : public GodotCollisionObject3D {45RID soft_mesh;4647struct Node {48Vector3 s; // Source position49Vector3 x; // Position50Vector3 q; // Previous step position/Test position51Vector3 f; // Force accumulator52Vector3 v; // Velocity53Vector3 bv; // Biased Velocity54Vector3 n; // Normal55real_t area = 0.0; // Area56real_t im = 0.0; // 1/mass57DynamicBVH::ID leaf; // Leaf data58uint32_t index = 0;59};6061struct Link {62Vector3 c3; // gradient63Node *n[2] = { nullptr, nullptr }; // Node pointers64real_t rl = 0.0; // Rest length65real_t c0 = 0.0; // (ima+imb)*kLST66real_t c1 = 0.0; // rl^267real_t c2 = 0.0; // |gradient|^2/c068};6970struct Face {71Vector3 centroid;72Node *n[3] = { nullptr, nullptr, nullptr }; // Node pointers73Vector3 normal; // Normal74real_t ra = 0.0; // Rest area75DynamicBVH::ID leaf; // Leaf data76uint32_t index = 0;77};7879LocalVector<Node> nodes;80LocalVector<Link> links;81LocalVector<Face> faces;8283DynamicBVH node_tree;84DynamicBVH face_tree;8586LocalVector<uint32_t> map_visual_to_physics;8788AABB bounds;8990real_t collision_margin = 0.05;9192real_t total_mass = 1.0;93real_t inv_total_mass = 1.0;9495int iteration_count = 5;96real_t linear_stiffness = 0.5; // [0,1]97real_t shrinking_factor = 0.0; // [-1,1]98real_t pressure_coefficient = 0.0; // [-inf,+inf]99real_t damping_coefficient = 0.01; // [0,1]100real_t drag_coefficient = 0.0; // [0,1]101LocalVector<int> pinned_vertices;102103SelfList<GodotSoftBody3D> active_list;104105HashSet<GodotConstraint3D *> constraints;106107Vector<AreaCMP> areas;108109VSet<RID> exceptions;110111uint64_t island_step = 0;112113_FORCE_INLINE_ Vector3 _compute_area_windforce(const GodotArea3D *p_area, const Face *p_face);114115public:116GodotSoftBody3D();117118const AABB &get_bounds() const { return bounds; }119120void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant);121Variant get_state(PhysicsServer3D::BodyState p_state) const;122123_FORCE_INLINE_ void add_constraint(GodotConstraint3D *p_constraint) { constraints.insert(p_constraint); }124_FORCE_INLINE_ void remove_constraint(GodotConstraint3D *p_constraint) { constraints.erase(p_constraint); }125_FORCE_INLINE_ const HashSet<GodotConstraint3D *> &get_constraints() const { return constraints; }126_FORCE_INLINE_ void clear_constraints() { constraints.clear(); }127128_FORCE_INLINE_ void add_exception(const RID &p_exception) { exceptions.insert(p_exception); }129_FORCE_INLINE_ void remove_exception(const RID &p_exception) { exceptions.erase(p_exception); }130_FORCE_INLINE_ bool has_exception(const RID &p_exception) const { return exceptions.has(p_exception); }131_FORCE_INLINE_ const VSet<RID> &get_exceptions() const { return exceptions; }132133_FORCE_INLINE_ uint64_t get_island_step() const { return island_step; }134_FORCE_INLINE_ void set_island_step(uint64_t p_step) { island_step = p_step; }135136_FORCE_INLINE_ void add_area(GodotArea3D *p_area) {137int index = areas.find(AreaCMP(p_area));138if (index > -1) {139areas.write[index].refCount += 1;140} else {141areas.ordered_insert(AreaCMP(p_area));142}143}144145_FORCE_INLINE_ void remove_area(GodotArea3D *p_area) {146int index = areas.find(AreaCMP(p_area));147if (index > -1) {148areas.write[index].refCount -= 1;149if (areas[index].refCount < 1) {150areas.remove_at(index);151}152}153}154155virtual void set_space(GodotSpace3D *p_space) override;156157void set_mesh(RID p_mesh);158159void update_rendering_server(PhysicsServer3DRenderingServerHandler *p_rendering_server_handler);160161Vector3 get_vertex_position(int p_index) const;162void set_vertex_position(int p_index, const Vector3 &p_position);163164void pin_vertex(int p_index);165void unpin_vertex(int p_index);166void unpin_all_vertices();167bool is_vertex_pinned(int p_index) const;168169uint32_t get_node_count() const;170real_t get_node_inv_mass(uint32_t p_node_index) const;171Vector3 get_node_position(uint32_t p_node_index) const;172Vector3 get_node_velocity(uint32_t p_node_index) const;173Vector3 get_node_biased_velocity(uint32_t p_node_index) const;174void apply_node_impulse(uint32_t p_node_index, const Vector3 &p_impulse);175void apply_node_force(uint32_t p_node_index, const Vector3 &p_force);176void apply_central_impulse(const Vector3 &p_impulse);177void apply_central_force(const Vector3 &p_force);178void apply_node_bias_impulse(uint32_t p_node_index, const Vector3 &p_impulse);179180uint32_t get_face_count() const;181void get_face_points(uint32_t p_face_index, Vector3 &r_point_1, Vector3 &r_point_2, Vector3 &r_point_3) const;182Vector3 get_face_normal(uint32_t p_face_index) const;183184void set_iteration_count(int p_val);185_FORCE_INLINE_ real_t get_iteration_count() const { return iteration_count; }186187void set_total_mass(real_t p_val);188_FORCE_INLINE_ real_t get_total_mass() const { return total_mass; }189_FORCE_INLINE_ real_t get_total_inv_mass() const { return inv_total_mass; }190191void set_collision_margin(real_t p_val);192_FORCE_INLINE_ real_t get_collision_margin() const { return collision_margin; }193194void set_linear_stiffness(real_t p_val);195_FORCE_INLINE_ real_t get_linear_stiffness() const { return linear_stiffness; }196197void set_shrinking_factor(real_t p_val);198_FORCE_INLINE_ real_t get_shrinking_factor() const { return shrinking_factor; }199200void set_pressure_coefficient(real_t p_val);201_FORCE_INLINE_ real_t get_pressure_coefficient() const { return pressure_coefficient; }202203void set_damping_coefficient(real_t p_val);204_FORCE_INLINE_ real_t get_damping_coefficient() const { return damping_coefficient; }205206void set_drag_coefficient(real_t p_val);207_FORCE_INLINE_ real_t get_drag_coefficient() const { return drag_coefficient; }208209void predict_motion(real_t p_delta);210void solve_constraints(real_t p_delta);211212_FORCE_INLINE_ uint32_t get_node_index(void *p_node) const { return static_cast<Node *>(p_node)->index; }213_FORCE_INLINE_ uint32_t get_face_index(void *p_face) const { return static_cast<Face *>(p_face)->index; }214215// Return true to stop the query.216// p_index is the node index for AABB query, face index for Ray query.217typedef bool (*QueryResultCallback)(uint32_t p_index, void *p_userdata);218219void query_aabb(const AABB &p_aabb, QueryResultCallback p_result_callback, void *p_userdata);220void query_ray(const Vector3 &p_from, const Vector3 &p_to, QueryResultCallback p_result_callback, void *p_userdata);221222protected:223virtual void _shapes_changed() override;224225private:226void update_normals_and_centroids();227void update_bounds();228void update_constants();229void update_area();230void reset_link_rest_lengths();231void update_link_constants();232233void apply_nodes_transform(const Transform3D &p_transform);234235void add_velocity(const Vector3 &p_velocity);236237void apply_forces(const LocalVector<GodotArea3D *> &p_wind_areas);238239bool create_from_trimesh(const Vector<int> &p_indices, const Vector<Vector3> &p_vertices);240void generate_bending_constraints(int p_distance);241void reoptimize_link_order();242void append_link(uint32_t p_node1, uint32_t p_node2);243void append_face(uint32_t p_node1, uint32_t p_node2, uint32_t p_node3);244245void solve_links(real_t kst, real_t ti);246247void initialize_face_tree();248void update_face_tree(real_t p_delta);249250void initialize_shape(bool p_force_move = true);251void deinitialize_shape();252253void destroy();254};255256class GodotSoftBodyShape3D : public GodotShape3D {257GodotSoftBody3D *soft_body = nullptr;258259public:260GodotSoftBody3D *get_soft_body() const { return soft_body; }261262virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_SOFT_BODY; }263virtual void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const override { r_min = r_max = 0.0; }264virtual Vector3 get_support(const Vector3 &p_normal) const override { return Vector3(); }265virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const override { r_amount = 0; }266267virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal, int &r_face_index, bool p_hit_back_faces) const override;268virtual bool intersect_point(const Vector3 &p_point) const override;269virtual Vector3 get_closest_point_to(const Vector3 &p_point) const override;270virtual Vector3 get_moment_of_inertia(real_t p_mass) const override { return Vector3(); }271272virtual void set_data(const Variant &p_data) override {}273virtual Variant get_data() const override { return Variant(); }274275void update_bounds();276277GodotSoftBodyShape3D(GodotSoftBody3D *p_soft_body);278~GodotSoftBodyShape3D() {}279};280281282