aboutsummaryrefslogtreecommitdiff
path: root/servers/physics_2d/joints_2d_sw.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--servers/physics_2d/joints_2d_sw.cpp260
1 files changed, 121 insertions, 139 deletions
diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp
index 76adf0642..027758649 100644
--- a/servers/physics_2d/joints_2d_sw.cpp
+++ b/servers/physics_2d/joints_2d_sw.cpp
@@ -52,40 +52,37 @@
* SOFTWARE.
*/
-static inline real_t k_scalar(Body2DSW *a,Body2DSW *b,const Vector2& rA, const Vector2& rB, const Vector2& n) {
-
-
- real_t value=0;
+static inline real_t k_scalar(Body2DSW *a, Body2DSW *b, const Vector2 &rA, const Vector2 &rB, const Vector2 &n) {
+ real_t value = 0;
{
- value+=a->get_inv_mass();
+ value += a->get_inv_mass();
real_t rcn = rA.cross(n);
- value+=a->get_inv_inertia() * rcn * rcn;
+ value += a->get_inv_inertia() * rcn * rcn;
}
if (b) {
- value+=b->get_inv_mass();
+ value += b->get_inv_mass();
real_t rcn = rB.cross(n);
- value+=b->get_inv_inertia() * rcn * rcn;
+ value += b->get_inv_inertia() * rcn * rcn;
}
return value;
-
}
static inline Vector2
-relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB){
- Vector2 sum = a->get_linear_velocity() -rA.tangent() * a->get_angular_velocity();
+relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB) {
+ Vector2 sum = a->get_linear_velocity() - rA.tangent() * a->get_angular_velocity();
if (b)
- return (b->get_linear_velocity() -rB.tangent() * b->get_angular_velocity()) - sum;
+ return (b->get_linear_velocity() - rB.tangent() * b->get_angular_velocity()) - sum;
else
return -sum;
}
static inline real_t
-normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n){
+normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n) {
return relative_velocity(a, b, rA, rB).dot(n);
}
@@ -188,13 +185,12 @@ PinJoint2DSW::~PinJoint2DSW() {
#else
-
bool PinJoint2DSW::setup(real_t p_step) {
Space2DSW *space = A->get_space();
- ERR_FAIL_COND_V(!space,false;)
+ ERR_FAIL_COND_V(!space, false;)
rA = A->get_transform().basis_xform(anchor_A);
- rB = B?B->get_transform().basis_xform(anchor_B):anchor_B;
+ rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B;
#if 0
Vector2 gA = rA+A->get_transform().get_origin();
Vector2 gB = B?rB+B->get_transform().get_origin():rB;
@@ -212,29 +208,34 @@ bool PinJoint2DSW::setup(real_t p_step) {
// = [1/m1+1/m2 0 ] + invI1 * [rA.y*rA.y -rA.x*rA.y] + invI2 * [rA.y*rA.y -rA.x*rA.y]
// [ 0 1/m1+1/m2] [-rA.x*rA.y rA.x*rA.x] [-rA.x*rA.y rA.x*rA.x]
- real_t B_inv_mass = B?B->get_inv_mass():0.0;
-
+ real_t B_inv_mass = B ? B->get_inv_mass() : 0.0;
Transform2D K1;
- 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;
+ 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;
Transform2D K2;
- 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;
+ 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;
Transform2D K;
- K[0]= K1[0] + K2[0];
- K[1]= K1[1] + K2[1];
+ K[0] = K1[0] + K2[0];
+ K[1] = K1[1] + K2[1];
if (B) {
Transform2D K3;
- 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;
+ 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;
- K[0]+=K3[0];
- K[1]+=K3[1];
+ K[0] += K3[0];
+ K[1] += K3[1];
}
K[0].x += softness;
@@ -242,23 +243,22 @@ bool PinJoint2DSW::setup(real_t p_step) {
M = K.affine_inverse();
- Vector2 gA = rA+A->get_transform().get_origin();
- Vector2 gB = B?rB+B->get_transform().get_origin():rB;
+ Vector2 gA = rA + A->get_transform().get_origin();
+ Vector2 gB = B ? rB + B->get_transform().get_origin() : rB;
Vector2 delta = gB - gA;
- bias = delta*-(get_bias()==0?space->get_constraint_bias():get_bias())*(1.0/p_step);
+ bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step);
// apply accumulated impulse
- A->apply_impulse(rA,-P);
+ A->apply_impulse(rA, -P);
if (B)
- B->apply_impulse(rB,P);
+ B->apply_impulse(rB, P);
return true;
}
-void PinJoint2DSW::solve(real_t p_step){
-
+void PinJoint2DSW::solve(real_t p_step) {
// compute relative velocity
Vector2 vA = A->get_linear_velocity() - rA.cross(A->get_angular_velocity());
@@ -269,114 +269,110 @@ void PinJoint2DSW::solve(real_t p_step){
else
rel_vel = -vA;
- Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness,softness) * P);
+ Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P);
- A->apply_impulse(rA,-impulse);
+ A->apply_impulse(rA, -impulse);
if (B)
- B->apply_impulse(rB,impulse);
-
+ B->apply_impulse(rB, impulse);
P += impulse;
}
void PinJoint2DSW::set_param(Physics2DServer::PinJointParam p_param, real_t p_value) {
- if(p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
+ if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
softness = p_value;
}
real_t PinJoint2DSW::get_param(Physics2DServer::PinJointParam p_param) const {
- if(p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
+ if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
return softness;
ERR_FAIL_V(0);
}
-PinJoint2DSW::PinJoint2DSW(const Vector2& p_pos,Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,p_body_b?2:1) {
+PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b)
+ : Joint2DSW(_arr, p_body_b ? 2 : 1) {
- A=p_body_a;
- B=p_body_b;
+ A = p_body_a;
+ B = p_body_b;
anchor_A = p_body_a->get_inv_transform().xform(p_pos);
- anchor_B = p_body_b?p_body_b->get_inv_transform().xform(p_pos):p_pos;
+ anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;
- softness=0;
+ softness = 0;
- p_body_a->add_constraint(this,0);
+ p_body_a->add_constraint(this, 0);
if (p_body_b)
- p_body_b->add_constraint(this,1);
-
+ p_body_b->add_constraint(this, 1);
}
-
-
PinJoint2DSW::~PinJoint2DSW() {
if (A)
A->remove_constraint(this);
if (B)
B->remove_constraint(this);
-
}
-
-
#endif
//////////////////////////////////////////////
//////////////////////////////////////////////
//////////////////////////////////////////////
-
static inline void
-k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2)
-{
+k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) {
// 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
- k11 = m_sum; k12 = 0.0f;
- k21 = 0.0f; k22 = m_sum;
+ k11 = m_sum;
+ k12 = 0.0f;
+ k21 = 0.0f;
+ k22 = m_sum;
// add the influence from r1
real_t a_i_inv = a->get_inv_inertia();
- real_t r1xsq = r1.x * r1.x * a_i_inv;
- real_t r1ysq = r1.y * r1.y * a_i_inv;
+ real_t r1xsq = r1.x * r1.x * a_i_inv;
+ real_t r1ysq = r1.y * r1.y * a_i_inv;
real_t r1nxy = -r1.x * r1.y * a_i_inv;
- k11 += r1ysq; k12 += r1nxy;
- k21 += r1nxy; k22 += r1xsq;
+ k11 += r1ysq;
+ k12 += r1nxy;
+ k21 += r1nxy;
+ k22 += r1xsq;
// add the influnce from r2
real_t b_i_inv = b->get_inv_inertia();
- real_t r2xsq = r2.x * r2.x * b_i_inv;
- real_t r2ysq = r2.y * r2.y * b_i_inv;
+ real_t r2xsq = r2.x * r2.x * b_i_inv;
+ real_t r2ysq = r2.y * r2.y * b_i_inv;
real_t r2nxy = -r2.x * r2.y * b_i_inv;
- k11 += r2ysq; k12 += r2nxy;
- k21 += r2nxy; k22 += r2xsq;
+ k11 += r2ysq;
+ k12 += r2nxy;
+ k21 += r2nxy;
+ k22 += r2xsq;
// invert
- real_t determinant = k11*k22 - k12*k21;
- ERR_FAIL_COND(determinant== 0.0);
+ real_t determinant = k11 * k22 - k12 * k21;
+ ERR_FAIL_COND(determinant == 0.0);
- real_t det_inv = 1.0f/determinant;
- *k1 = Vector2( k22*det_inv, -k12*det_inv);
- *k2 = Vector2(-k21*det_inv, k11*det_inv);
+ real_t det_inv = 1.0f / determinant;
+ *k1 = Vector2(k22 * det_inv, -k12 * det_inv);
+ *k2 = Vector2(-k21 * det_inv, k11 * det_inv);
}
static _FORCE_INLINE_ Vector2
-mult_k(const Vector2& vr, const Vector2 &k1, const Vector2 &k2)
-{
+mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) {
return Vector2(vr.dot(k1), vr.dot(k2));
}
bool GrooveJoint2DSW::setup(real_t p_step) {
-
// calculate endpoints in worldspace
Vector2 ta = A->get_transform().xform(A_groove_1);
Vector2 tb = A->get_transform().xform(A_groove_2);
- Space2DSW *space=A->get_space();
+ Space2DSW *space = A->get_space();
// calculate axis
Vector2 n = -(tb - ta).tangent().normalized();
@@ -388,16 +384,16 @@ bool GrooveJoint2DSW::setup(real_t p_step) {
// calculate tangential distance along the axis of rB
real_t td = (B->get_transform().get_origin() + rB).cross(n);
// calculate clamping factor and rB
- if(td <= ta.cross(n)){
+ if (td <= ta.cross(n)) {
clamp = 1.0f;
rA = ta - A->get_transform().get_origin();
- } else if(td >= tb.cross(n)){
+ } else if (td >= tb.cross(n)) {
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);
- rA = ((-n.tangent() * -td) + n*d) - A->get_transform().get_origin();
+ rA = ((-n.tangent() * -td) + n * d) - A->get_transform().get_origin();
}
// Calculate mass tensor
@@ -410,52 +406,49 @@ bool GrooveJoint2DSW::setup(real_t p_step) {
//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);
-
- Vector2 delta = (B->get_transform().get_origin() +rB) - (A->get_transform().get_origin() + rA);
+ Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
real_t _b = get_bias();
- _b=0.001;
- gbias=(delta*-(_b==0?space->get_constraint_bias():_b)*(1.0/p_step)).clamped(get_max_bias());
+ _b = 0.001;
+ gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).clamped(get_max_bias());
// apply accumulated impulse
- A->apply_impulse(rA,-jn_acc);
- B->apply_impulse(rB,jn_acc);
+ A->apply_impulse(rA, -jn_acc);
+ B->apply_impulse(rB, jn_acc);
- correct=true;
+ correct = true;
return true;
}
-void GrooveJoint2DSW::solve(real_t p_step){
-
+void GrooveJoint2DSW::solve(real_t p_step) {
// compute impulse
- Vector2 vr = relative_velocity(A, B, rA,rB);
+ Vector2 vr = relative_velocity(A, B, rA, rB);
- Vector2 j = mult_k(gbias-vr, k1, k2);
+ Vector2 j = mult_k(gbias - vr, k1, k2);
Vector2 jOld = jn_acc;
- j+=jOld;
+ j += jOld;
jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : xf_normal.project(j)).clamped(jn_max);
j = jn_acc - jOld;
- A->apply_impulse(rA,-j);
- B->apply_impulse(rB,j);
+ A->apply_impulse(rA, -j);
+ B->apply_impulse(rB, j);
}
+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) {
-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) {
-
- A=p_body_a;
- B=p_body_b;
+ A = p_body_a;
+ B = p_body_b;
A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
- B_anchor=B->get_inv_transform().xform(p_b_anchor);
+ B_anchor = B->get_inv_transform().xform(p_b_anchor);
A_groove_normal = -(A_groove_2 - A_groove_1).normalized().tangent();
- A->add_constraint(this,0);
- B->add_constraint(this,1);
-
+ A->add_constraint(this, 0);
+ B->add_constraint(this, 1);
}
GrooveJoint2DSW::~GrooveJoint2DSW() {
@@ -464,38 +457,35 @@ GrooveJoint2DSW::~GrooveJoint2DSW() {
B->remove_constraint(this);
}
-
//////////////////////////////////////////////
//////////////////////////////////////////////
//////////////////////////////////////////////
-
bool DampedSpringJoint2DSW::setup(real_t p_step) {
rA = A->get_transform().basis_xform(anchor_A);
rB = B->get_transform().basis_xform(anchor_B);
- Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA) ;
+ Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
real_t dist = delta.length();
if (dist)
- n=delta/dist;
+ n = delta / dist;
else
- n=Vector2();
+ n = Vector2();
real_t k = k_scalar(A, B, rA, rB, n);
- n_mass = 1.0f/k;
+ n_mass = 1.0f / k;
target_vrn = 0.0f;
- v_coef = 1.0f - Math::exp(-damping*(p_step)*k);
+ v_coef = 1.0f - Math::exp(-damping * (p_step)*k);
// apply spring force
real_t f_spring = (rest_length - dist) * stiffness;
- Vector2 j = n * f_spring*(p_step);
-
- A->apply_impulse(rA,-j);
- B->apply_impulse(rB,j);
+ Vector2 j = n * f_spring * (p_step);
+ A->apply_impulse(rA, -j);
+ B->apply_impulse(rB, j);
return true;
}
@@ -507,38 +497,36 @@ void DampedSpringJoint2DSW::solve(real_t p_step) {
// compute velocity loss from drag
// not 100% certain this is derived correctly, though it makes sense
- real_t v_damp = -vrn*v_coef;
+ real_t v_damp = -vrn * v_coef;
target_vrn = vrn + v_damp;
- Vector2 j=n*v_damp*n_mass;
-
- A->apply_impulse(rA,-j);
- B->apply_impulse(rB,j);
+ Vector2 j = n * v_damp * n_mass;
+ A->apply_impulse(rA, -j);
+ B->apply_impulse(rB, j);
}
void DampedSpringJoint2DSW::set_param(Physics2DServer::DampedStringParam p_param, real_t p_value) {
- switch(p_param) {
+ switch (p_param) {
case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
- rest_length=p_value;
+ rest_length = p_value;
} break;
case Physics2DServer::DAMPED_STRING_DAMPING: {
- damping=p_value;
+ damping = p_value;
} break;
case Physics2DServer::DAMPED_STRING_STIFFNESS: {
- stiffness=p_value;
+ stiffness = p_value;
} break;
}
-
}
-real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const{
+real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const {
- switch(p_param) {
+ switch (p_param) {
case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
@@ -557,30 +545,24 @@ real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_par
ERR_FAIL_V(0);
}
+DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b)
+ : Joint2DSW(_arr, 2) {
-DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2& p_anchor_a,const Vector2& p_anchor_b, Body2DSW* p_body_a,Body2DSW* p_body_b) : Joint2DSW(_arr,2) {
-
-
- A=p_body_a;
- B=p_body_b;
+ A = p_body_a;
+ B = p_body_b;
anchor_A = A->get_inv_transform().xform(p_anchor_a);
anchor_B = B->get_inv_transform().xform(p_anchor_b);
- rest_length=p_anchor_a.distance_to(p_anchor_b);
- stiffness=20;
- damping=1.5;
-
-
- A->add_constraint(this,0);
- B->add_constraint(this,1);
+ rest_length = p_anchor_a.distance_to(p_anchor_b);
+ stiffness = 20;
+ damping = 1.5;
+ A->add_constraint(this, 0);
+ B->add_constraint(this, 1);
}
DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
A->remove_constraint(this);
B->remove_constraint(this);
-
}
-
-