Path: blob/master/modules/godot_physics_2d/godot_joints_2d.h
10277 views
/**************************************************************************/1/* godot_joints_2d.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_2d.h"33#include "godot_constraint_2d.h"3435class GodotJoint2D : public GodotConstraint2D {36real_t bias = 0;37real_t max_bias = 3.40282e+38;38real_t max_force = 3.40282e+38;3940protected:41bool dynamic_A = false;42bool dynamic_B = false;4344public:45_FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }46_FORCE_INLINE_ real_t get_max_force() const { return max_force; }4748_FORCE_INLINE_ void set_bias(real_t p_bias) { bias = p_bias; }49_FORCE_INLINE_ real_t get_bias() const { return bias; }5051_FORCE_INLINE_ void set_max_bias(real_t p_bias) { max_bias = p_bias; }52_FORCE_INLINE_ real_t get_max_bias() const { return max_bias; }5354virtual bool setup(real_t p_step) override { return false; }55virtual bool pre_solve(real_t p_step) override { return false; }56virtual void solve(real_t p_step) override {}5758void copy_settings_from(GodotJoint2D *p_joint);5960virtual PhysicsServer2D::JointType get_type() const { return PhysicsServer2D::JOINT_TYPE_MAX; }61GodotJoint2D(GodotBody2D **p_body_ptr = nullptr, int p_body_count = 0) :62GodotConstraint2D(p_body_ptr, p_body_count) {}6364virtual ~GodotJoint2D() {65for (int i = 0; i < get_body_count(); i++) {66GodotBody2D *body = get_body_ptr()[i];67if (body) {68body->remove_constraint(this, i);69}70}71}72};7374class GodotPinJoint2D : public GodotJoint2D {75union {76struct {77GodotBody2D *A;78GodotBody2D *B;79};8081GodotBody2D *_arr[2] = { nullptr, nullptr };82};8384Transform2D M;85Vector2 rA, rB;86Vector2 anchor_A;87Vector2 anchor_B;88Vector2 bias;89real_t initial_angle = 0.0;90real_t bias_velocity = 0.0;91real_t jn_max = 0.0;92real_t j_acc = 0.0;93real_t i_sum = 0.0;94Vector2 P;95real_t softness = 0.0;96real_t angular_limit_lower = 0.0;97real_t angular_limit_upper = 0.0;98real_t motor_target_velocity = 0.0;99bool is_joint_at_limit = false;100bool motor_enabled = false;101bool angular_limit_enabled = false;102103public:104virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_PIN; }105106virtual bool setup(real_t p_step) override;107virtual bool pre_solve(real_t p_step) override;108virtual void solve(real_t p_step) override;109110void set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value);111real_t get_param(PhysicsServer2D::PinJointParam p_param) const;112113void set_flag(PhysicsServer2D::PinJointFlag p_flag, bool p_enabled);114bool get_flag(PhysicsServer2D::PinJointFlag p_flag) const;115116GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b = nullptr);117};118119class GodotGrooveJoint2D : public GodotJoint2D {120union {121struct {122GodotBody2D *A;123GodotBody2D *B;124};125126GodotBody2D *_arr[2] = { nullptr, nullptr };127};128129Vector2 A_groove_1;130Vector2 A_groove_2;131Vector2 A_groove_normal;132Vector2 B_anchor;133Vector2 jn_acc;134Vector2 gbias;135real_t jn_max = 0.0;136real_t clamp = 0.0;137Vector2 xf_normal;138Vector2 rA, rB;139Vector2 k1, k2;140141bool correct = false;142143public:144virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_GROOVE; }145146virtual bool setup(real_t p_step) override;147virtual bool pre_solve(real_t p_step) override;148virtual void solve(real_t p_step) override;149150GodotGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, GodotBody2D *p_body_a, GodotBody2D *p_body_b);151};152153class GodotDampedSpringJoint2D : public GodotJoint2D {154union {155struct {156GodotBody2D *A;157GodotBody2D *B;158};159160GodotBody2D *_arr[2] = { nullptr, nullptr };161};162163Vector2 anchor_A;164Vector2 anchor_B;165166real_t rest_length = 0.0;167real_t damping = 1.5;168real_t stiffness = 20.0;169170Vector2 rA, rB;171Vector2 n;172Vector2 j;173real_t n_mass = 0.0;174real_t target_vrn = 0.0;175real_t v_coef = 0.0;176177public:178virtual PhysicsServer2D::JointType get_type() const override { return PhysicsServer2D::JOINT_TYPE_DAMPED_SPRING; }179180virtual bool setup(real_t p_step) override;181virtual bool pre_solve(real_t p_step) override;182virtual void solve(real_t p_step) override;183184void set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value);185real_t get_param(PhysicsServer2D::DampedSpringParam p_param) const;186187GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b);188};189190191