Path: blob/master/modules/godot_physics_3d/godot_body_pair_3d.h
10277 views
/**************************************************************************/1/* godot_body_pair_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_body_3d.h"33#include "godot_constraint_3d.h"34#include "godot_soft_body_3d.h"3536#include "core/templates/local_vector.h"3738class GodotBodyContact3D : public GodotConstraint3D {39protected:40struct Contact {41Vector3 position;42Vector3 normal;43int index_A = 0, index_B = 0;44Vector3 local_A, local_B;45Vector3 acc_impulse; // accumulated impulse - only one of the object's impulse is needed as impulse_a == -impulse_b46real_t acc_normal_impulse = 0.0; // accumulated normal impulse (Pn)47Vector3 acc_tangent_impulse; // accumulated tangent impulse (Pt)48real_t acc_bias_impulse = 0.0; // accumulated normal impulse for position bias (Pnb)49real_t acc_bias_impulse_center_of_mass = 0.0; // accumulated normal impulse for position bias applied to com50real_t mass_normal = 0.0;51real_t bias = 0.0;52real_t bounce = 0.0;5354real_t depth = 0.0;55bool active = false;56bool used = false;57Vector3 rA, rB; // Offset in world orientation with respect to center of mass58};5960Vector3 sep_axis;61bool collided = false;62bool check_ccd = false;6364GodotSpace3D *space = nullptr;6566GodotBodyContact3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) :67GodotConstraint3D(p_body_ptr, p_body_count) {68}69};7071class GodotBodyPair3D : public GodotBodyContact3D {72enum {73MAX_CONTACTS = 474};7576union {77struct {78GodotBody3D *A;79GodotBody3D *B;80};8182GodotBody3D *_arr[2] = { nullptr, nullptr };83};8485int shape_A = 0;86int shape_B = 0;8788bool collide_A = false;89bool collide_B = false;9091bool report_contacts_only = false;9293Vector3 offset_B; //use local A coordinates to avoid numerical issues on collision detection9495Contact contacts[MAX_CONTACTS];96int contact_count = 0;9798static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata);99100void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal);101102void validate_contacts();103bool _test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A, const Transform3D &p_xform_A, GodotBody3D *p_B, int p_shape_B, const Transform3D &p_xform_B);104105public:106virtual bool setup(real_t p_step) override;107virtual bool pre_solve(real_t p_step) override;108virtual void solve(real_t p_step) override;109110GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B);111~GodotBodyPair3D();112};113114class GodotBodySoftBodyPair3D : public GodotBodyContact3D {115GodotBody3D *body = nullptr;116GodotSoftBody3D *soft_body = nullptr;117118int body_shape = 0;119120bool body_collides = false;121bool soft_body_collides = false;122123bool report_contacts_only = false;124125LocalVector<Contact> contacts;126127static void _contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata);128129void contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal);130131void validate_contacts();132133public:134virtual bool setup(real_t p_step) override;135virtual bool pre_solve(real_t p_step) override;136virtual void solve(real_t p_step) override;137138virtual GodotSoftBody3D *get_soft_body_ptr(int p_index) const override { return soft_body; }139virtual int get_soft_body_count() const override { return 1; }140141GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B);142~GodotBodySoftBodyPair3D();143};144145146