Path: blob/master/modules/godot_physics_3d/godot_joint_3d.h
10277 views
/**************************************************************************/1/* godot_joint_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"3435class GodotJoint3D : public GodotConstraint3D {36protected:37bool dynamic_A = false;38bool dynamic_B = false;3940void plane_space(const Vector3 &n, Vector3 &p, Vector3 &q) {41if (Math::abs(n.z) > Math::SQRT12) {42// choose p in y-z plane43real_t a = n[1] * n[1] + n[2] * n[2];44real_t k = 1.0 / Math::sqrt(a);45p = Vector3(0, -n[2] * k, n[1] * k);46// set q = n x p47q = Vector3(a * k, -n[0] * p[2], n[0] * p[1]);48} else {49// choose p in x-y plane50real_t a = n.x * n.x + n.y * n.y;51real_t k = 1.0 / Math::sqrt(a);52p = Vector3(-n.y * k, n.x * k, 0);53// set q = n x p54q = Vector3(-n.z * p.y, n.z * p.x, a * k);55}56}5758_FORCE_INLINE_ real_t atan2fast(real_t y, real_t x) {59real_t coeff_1 = Math::PI / 4.0f;60real_t coeff_2 = 3.0f * coeff_1;61real_t abs_y = Math::abs(y);62real_t angle;63if (x >= 0.0f) {64real_t r = (x - abs_y) / (x + abs_y);65angle = coeff_1 - coeff_1 * r;66} else {67real_t r = (x + abs_y) / (abs_y - x);68angle = coeff_2 - coeff_1 * r;69}70return (y < 0.0f) ? -angle : angle;71}7273public:74virtual bool setup(real_t p_step) override { return false; }75virtual bool pre_solve(real_t p_step) override { return true; }76virtual void solve(real_t p_step) override {}7778void copy_settings_from(GodotJoint3D *p_joint) {79set_self(p_joint->get_self());80set_priority(p_joint->get_priority());81disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies());82}8384virtual PhysicsServer3D::JointType get_type() const { return PhysicsServer3D::JOINT_TYPE_MAX; }85_FORCE_INLINE_ GodotJoint3D(GodotBody3D **p_body_ptr = nullptr, int p_body_count = 0) :86GodotConstraint3D(p_body_ptr, p_body_count) {87}8889virtual ~GodotJoint3D() {90for (int i = 0; i < get_body_count(); i++) {91GodotBody3D *body = get_body_ptr()[i];92if (body) {93body->remove_constraint(this);94}95}96}97};9899100