2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* joints_2d_sw.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "joints_2d_sw.h"
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "space_2d_sw.h"
|
|
|
|
|
|
|
|
//based on chipmunk joint constraints
|
|
|
|
|
|
|
|
/* Copyright (c) 2007 Scott Lembcke
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static inline real_t k_scalar(Body2DSW *a, Body2DSW *b, const Vector2 &rA, const Vector2 &rB, const Vector2 &n) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t value = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
value += a->get_inv_mass();
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t rcn = rA.cross(n);
|
2017-03-05 16:44:50 +01:00
|
|
|
value += a->get_inv_inertia() * rcn * rcn;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (b) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
value += b->get_inv_mass();
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t rcn = rB.cross(n);
|
2017-03-05 16:44:50 +01:00
|
|
|
value += b->get_inv_inertia() * rcn * rcn;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Vector2
|
2017-03-05 16:44:50 +01:00
|
|
|
relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB) {
|
|
|
|
Vector2 sum = a->get_linear_velocity() - rA.tangent() * a->get_angular_velocity();
|
2014-02-10 02:10:30 +01:00
|
|
|
if (b)
|
2017-03-05 16:44:50 +01:00
|
|
|
return (b->get_linear_velocity() - rB.tangent() * b->get_angular_velocity()) - sum;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
|
|
|
return -sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline real_t
|
2017-03-05 16:44:50 +01:00
|
|
|
normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return relative_velocity(a, b, rA, rB).dot(n);
|
|
|
|
}
|
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
bool PinJoint2DSW::setup(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Space2DSW *space = A->get_space();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!space, false;)
|
2014-02-10 02:10:30 +01:00
|
|
|
rA = A->get_transform().basis_xform(anchor_A);
|
2017-03-05 16:44:50 +01:00
|
|
|
rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t B_inv_mass = B ? B->get_inv_mass() : 0.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D K1;
|
2017-03-05 16:44:50 +01:00
|
|
|
K1[0].x = A->get_inv_mass() + B_inv_mass;
|
|
|
|
K1[1].x = 0.0f;
|
|
|
|
K1[0].y = 0.0f;
|
|
|
|
K1[1].y = A->get_inv_mass() + B_inv_mass;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D K2;
|
2017-03-05 16:44:50 +01:00
|
|
|
K2[0].x = A->get_inv_inertia() * rA.y * rA.y;
|
|
|
|
K2[1].x = -A->get_inv_inertia() * rA.x * rA.y;
|
|
|
|
K2[0].y = -A->get_inv_inertia() * rA.x * rA.y;
|
|
|
|
K2[1].y = A->get_inv_inertia() * rA.x * rA.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D K;
|
2017-03-05 16:44:50 +01:00
|
|
|
K[0] = K1[0] + K2[0];
|
|
|
|
K[1] = K1[1] + K2[1];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (B) {
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D K3;
|
2017-03-05 16:44:50 +01:00
|
|
|
K3[0].x = B->get_inv_inertia() * rB.y * rB.y;
|
|
|
|
K3[1].x = -B->get_inv_inertia() * rB.x * rB.y;
|
|
|
|
K3[0].y = -B->get_inv_inertia() * rB.x * rB.y;
|
|
|
|
K3[1].y = B->get_inv_inertia() * rB.x * rB.x;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
K[0] += K3[0];
|
|
|
|
K[1] += K3[1];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
K[0].x += softness;
|
|
|
|
K[1].y += softness;
|
|
|
|
|
|
|
|
M = K.affine_inverse();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 gA = rA + A->get_transform().get_origin();
|
|
|
|
Vector2 gB = B ? rB + B->get_transform().get_origin() : rB;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector2 delta = gB - gA;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// apply accumulated impulse
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -P);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (B)
|
2017-03-05 16:44:50 +01:00
|
|
|
B->apply_impulse(rB, P);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void PinJoint2DSW::solve(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// compute relative velocity
|
|
|
|
Vector2 vA = A->get_linear_velocity() - rA.cross(A->get_angular_velocity());
|
|
|
|
|
|
|
|
Vector2 rel_vel;
|
|
|
|
if (B)
|
|
|
|
rel_vel = B->get_linear_velocity() - rB.cross(B->get_angular_velocity()) - vA;
|
|
|
|
else
|
|
|
|
rel_vel = -vA;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -impulse);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (B)
|
2017-03-05 16:44:50 +01:00
|
|
|
B->apply_impulse(rB, impulse);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
P += impulse;
|
|
|
|
}
|
|
|
|
|
2015-10-10 22:28:05 +02:00
|
|
|
void PinJoint2DSW::set_param(Physics2DServer::PinJointParam p_param, real_t p_value) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
|
2015-10-10 22:28:05 +02:00
|
|
|
softness = p_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t PinJoint2DSW::get_param(Physics2DServer::PinJointParam p_param) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
|
2015-10-10 22:28:05 +02:00
|
|
|
return softness;
|
|
|
|
ERR_FAIL_V(0);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b)
|
|
|
|
: Joint2DSW(_arr, p_body_b ? 2 : 1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A = p_body_a;
|
|
|
|
B = p_body_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
anchor_A = p_body_a->get_inv_transform().xform(p_pos);
|
2017-03-05 16:44:50 +01:00
|
|
|
anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
softness = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_body_a->add_constraint(this, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_body_b)
|
2017-03-05 16:44:50 +01:00
|
|
|
p_body_b->add_constraint(this, 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PinJoint2DSW::~PinJoint2DSW() {
|
|
|
|
|
|
|
|
if (A)
|
|
|
|
A->remove_constraint(this);
|
|
|
|
if (B)
|
|
|
|
B->remove_constraint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
|
|
|
|
static inline void
|
2017-03-05 16:44:50 +01:00
|
|
|
k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// calculate mass matrix
|
|
|
|
// If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
|
|
|
|
real_t k11, k12, k21, k22;
|
|
|
|
real_t m_sum = a->get_inv_mass() + b->get_inv_mass();
|
|
|
|
|
|
|
|
// start with I*m_sum
|
2017-03-05 16:44:50 +01:00
|
|
|
k11 = m_sum;
|
|
|
|
k12 = 0.0f;
|
|
|
|
k21 = 0.0f;
|
|
|
|
k22 = m_sum;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// add the influence from r1
|
|
|
|
real_t a_i_inv = a->get_inv_inertia();
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t r1xsq = r1.x * r1.x * a_i_inv;
|
|
|
|
real_t r1ysq = r1.y * r1.y * a_i_inv;
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t r1nxy = -r1.x * r1.y * a_i_inv;
|
2017-03-05 16:44:50 +01:00
|
|
|
k11 += r1ysq;
|
|
|
|
k12 += r1nxy;
|
|
|
|
k21 += r1nxy;
|
|
|
|
k22 += r1xsq;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// add the influnce from r2
|
|
|
|
real_t b_i_inv = b->get_inv_inertia();
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t r2xsq = r2.x * r2.x * b_i_inv;
|
|
|
|
real_t r2ysq = r2.y * r2.y * b_i_inv;
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t r2nxy = -r2.x * r2.y * b_i_inv;
|
2017-03-05 16:44:50 +01:00
|
|
|
k11 += r2ysq;
|
|
|
|
k12 += r2nxy;
|
|
|
|
k21 += r2nxy;
|
|
|
|
k22 += r2xsq;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// invert
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t determinant = k11 * k22 - k12 * k21;
|
|
|
|
ERR_FAIL_COND(determinant == 0.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t det_inv = 1.0f / determinant;
|
|
|
|
*k1 = Vector2(k22 * det_inv, -k12 * det_inv);
|
|
|
|
*k2 = Vector2(-k21 * det_inv, k11 * det_inv);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ Vector2
|
2017-03-05 16:44:50 +01:00
|
|
|
mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Vector2(vr.dot(k1), vr.dot(k2));
|
|
|
|
}
|
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
bool GrooveJoint2DSW::setup(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// calculate endpoints in worldspace
|
|
|
|
Vector2 ta = A->get_transform().xform(A_groove_1);
|
|
|
|
Vector2 tb = A->get_transform().xform(A_groove_2);
|
2017-03-05 16:44:50 +01:00
|
|
|
Space2DSW *space = A->get_space();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// calculate axis
|
|
|
|
Vector2 n = -(tb - ta).tangent().normalized();
|
|
|
|
real_t d = ta.dot(n);
|
|
|
|
|
|
|
|
xf_normal = n;
|
|
|
|
rB = B->get_transform().basis_xform(B_anchor);
|
|
|
|
|
|
|
|
// calculate tangential distance along the axis of rB
|
|
|
|
real_t td = (B->get_transform().get_origin() + rB).cross(n);
|
|
|
|
// calculate clamping factor and rB
|
2017-03-05 16:44:50 +01:00
|
|
|
if (td <= ta.cross(n)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
clamp = 1.0f;
|
|
|
|
rA = ta - A->get_transform().get_origin();
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (td >= tb.cross(n)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
clamp = -1.0f;
|
|
|
|
rA = tb - A->get_transform().get_origin();
|
|
|
|
} else {
|
|
|
|
clamp = 0.0f;
|
|
|
|
//joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
|
2017-03-05 16:44:50 +01:00
|
|
|
rA = ((-n.tangent() * -td) + n * d) - A->get_transform().get_origin();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate mass tensor
|
|
|
|
k_tensor(A, B, rA, rB, &k1, &k2);
|
|
|
|
|
|
|
|
// compute max impulse
|
|
|
|
jn_max = get_max_force() * p_step;
|
|
|
|
|
|
|
|
// calculate bias velocity
|
2017-01-14 12:26:56 +01:00
|
|
|
//cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
|
|
|
|
//joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
|
2017-08-21 21:15:36 +02:00
|
|
|
|
|
|
|
// FIXME: We used to do this assignment and then override it with 0.001 right after. Investigate why.
|
|
|
|
//real_t _b = get_bias();
|
|
|
|
real_t _b = 0.001;
|
2017-03-05 16:44:50 +01:00
|
|
|
gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).clamped(get_max_bias());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// apply accumulated impulse
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -jn_acc);
|
|
|
|
B->apply_impulse(rB, jn_acc);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
correct = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void GrooveJoint2DSW::solve(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// compute impulse
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 vr = relative_velocity(A, B, rA, rB);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 j = mult_k(gbias - vr, k1, k2);
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector2 jOld = jn_acc;
|
2017-03-05 16:44:50 +01:00
|
|
|
j += jOld;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : xf_normal.project(j)).clamped(jn_max);
|
|
|
|
|
|
|
|
j = jn_acc - jOld;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -j);
|
|
|
|
B->apply_impulse(rB, j);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GrooveJoint2DSW::GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b)
|
|
|
|
: Joint2DSW(_arr, 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A = p_body_a;
|
|
|
|
B = p_body_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
|
|
|
|
A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
|
2017-03-05 16:44:50 +01:00
|
|
|
B_anchor = B->get_inv_transform().xform(p_b_anchor);
|
2014-02-10 02:10:30 +01:00
|
|
|
A_groove_normal = -(A_groove_2 - A_groove_1).normalized().tangent();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->add_constraint(this, 0);
|
|
|
|
B->add_constraint(this, 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GrooveJoint2DSW::~GrooveJoint2DSW() {
|
|
|
|
|
|
|
|
A->remove_constraint(this);
|
|
|
|
B->remove_constraint(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
bool DampedSpringJoint2DSW::setup(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
rA = A->get_transform().basis_xform(anchor_A);
|
|
|
|
rB = B->get_transform().basis_xform(anchor_B);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t dist = delta.length();
|
|
|
|
|
|
|
|
if (dist)
|
2017-03-05 16:44:50 +01:00
|
|
|
n = delta / dist;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
n = Vector2();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
real_t k = k_scalar(A, B, rA, rB, n);
|
2017-03-05 16:44:50 +01:00
|
|
|
n_mass = 1.0f / k;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
target_vrn = 0.0f;
|
2017-03-05 16:44:50 +01:00
|
|
|
v_coef = 1.0f - Math::exp(-damping * (p_step)*k);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// apply spring force
|
|
|
|
real_t f_spring = (rest_length - dist) * stiffness;
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 j = n * f_spring * (p_step);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -j);
|
|
|
|
B->apply_impulse(rB, j);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-14 00:25:05 +01:00
|
|
|
void DampedSpringJoint2DSW::solve(real_t p_step) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// compute relative velocity
|
|
|
|
real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn;
|
|
|
|
|
|
|
|
// compute velocity loss from drag
|
|
|
|
// not 100% certain this is derived correctly, though it makes sense
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t v_damp = -vrn * v_coef;
|
2014-02-10 02:10:30 +01:00
|
|
|
target_vrn = vrn + v_damp;
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector2 j = n * v_damp * n_mass;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->apply_impulse(rA, -j);
|
|
|
|
B->apply_impulse(rB, j);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DampedSpringJoint2DSW::set_param(Physics2DServer::DampedStringParam p_param, real_t p_value) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rest_length = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Physics2DServer::DAMPED_STRING_DAMPING: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
damping = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Physics2DServer::DAMPED_STRING_STIFFNESS: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stiffness = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_param) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
|
|
|
|
|
|
|
|
return rest_length;
|
|
|
|
} break;
|
|
|
|
case Physics2DServer::DAMPED_STRING_DAMPING: {
|
|
|
|
|
|
|
|
return damping;
|
|
|
|
} break;
|
|
|
|
case Physics2DServer::DAMPED_STRING_STIFFNESS: {
|
|
|
|
|
|
|
|
return stiffness;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_V(0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b)
|
|
|
|
: Joint2DSW(_arr, 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A = p_body_a;
|
|
|
|
B = p_body_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
anchor_A = A->get_inv_transform().xform(p_anchor_a);
|
|
|
|
anchor_B = B->get_inv_transform().xform(p_anchor_b);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
rest_length = p_anchor_a.distance_to(p_anchor_b);
|
|
|
|
stiffness = 20;
|
|
|
|
damping = 1.5;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
A->add_constraint(this, 0);
|
|
|
|
B->add_constraint(this, 1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
|
|
|
|
|
|
|
|
A->remove_constraint(this);
|
|
|
|
B->remove_constraint(this);
|
|
|
|
}
|