| 1 | /**************************************************************************/ |
| 2 | /* godot_joints_2d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "godot_joints_2d.h" |
| 32 | |
| 33 | #include "godot_space_2d.h" |
| 34 | |
| 35 | //based on chipmunk joint constraints |
| 36 | |
| 37 | /* Copyright (c) 2007 Scott Lembcke |
| 38 | * |
| 39 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 40 | * of this software and associated documentation files (the "Software"), to deal |
| 41 | * in the Software without restriction, including without limitation the rights |
| 42 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 43 | * copies of the Software, and to permit persons to whom the Software is |
| 44 | * furnished to do so, subject to the following conditions: |
| 45 | * |
| 46 | * The above copyright notice and this permission notice shall be included in |
| 47 | * all copies or substantial portions of the Software. |
| 48 | * |
| 49 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 50 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 51 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 52 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 53 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 54 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 55 | * SOFTWARE. |
| 56 | */ |
| 57 | |
| 58 | void GodotJoint2D::copy_settings_from(GodotJoint2D *p_joint) { |
| 59 | set_self(p_joint->get_self()); |
| 60 | set_max_force(p_joint->get_max_force()); |
| 61 | set_bias(p_joint->get_bias()); |
| 62 | set_max_bias(p_joint->get_max_bias()); |
| 63 | disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies()); |
| 64 | } |
| 65 | |
| 66 | static inline real_t k_scalar(GodotBody2D *a, GodotBody2D *b, const Vector2 &rA, const Vector2 &rB, const Vector2 &n) { |
| 67 | real_t value = 0.0; |
| 68 | |
| 69 | { |
| 70 | value += a->get_inv_mass(); |
| 71 | real_t rcn = (rA - a->get_center_of_mass()).cross(n); |
| 72 | value += a->get_inv_inertia() * rcn * rcn; |
| 73 | } |
| 74 | |
| 75 | if (b) { |
| 76 | value += b->get_inv_mass(); |
| 77 | real_t rcn = (rB - b->get_center_of_mass()).cross(n); |
| 78 | value += b->get_inv_inertia() * rcn * rcn; |
| 79 | } |
| 80 | |
| 81 | return value; |
| 82 | } |
| 83 | |
| 84 | static inline Vector2 |
| 85 | relative_velocity(GodotBody2D *a, GodotBody2D *b, Vector2 rA, Vector2 rB) { |
| 86 | Vector2 sum = a->get_linear_velocity() - (rA - a->get_center_of_mass()).orthogonal() * a->get_angular_velocity(); |
| 87 | if (b) { |
| 88 | return (b->get_linear_velocity() - (rB - b->get_center_of_mass()).orthogonal() * b->get_angular_velocity()) - sum; |
| 89 | } else { |
| 90 | return -sum; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static inline real_t |
| 95 | normal_relative_velocity(GodotBody2D *a, GodotBody2D *b, Vector2 rA, Vector2 rB, Vector2 n) { |
| 96 | return relative_velocity(a, b, rA, rB).dot(n); |
| 97 | } |
| 98 | |
| 99 | bool GodotPinJoint2D::setup(real_t p_step) { |
| 100 | dynamic_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 101 | dynamic_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 102 | |
| 103 | if (!dynamic_A && !dynamic_B) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | GodotSpace2D *space = A->get_space(); |
| 108 | ERR_FAIL_NULL_V(space, false); |
| 109 | |
| 110 | rA = A->get_transform().basis_xform(anchor_A); |
| 111 | rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B; |
| 112 | |
| 113 | real_t B_inv_mass = B ? B->get_inv_mass() : 0.0; |
| 114 | |
| 115 | Transform2D K1; |
| 116 | K1[0].x = A->get_inv_mass() + B_inv_mass; |
| 117 | K1[1].x = 0.0f; |
| 118 | K1[0].y = 0.0f; |
| 119 | K1[1].y = A->get_inv_mass() + B_inv_mass; |
| 120 | |
| 121 | Vector2 r1 = rA - A->get_center_of_mass(); |
| 122 | |
| 123 | Transform2D K2; |
| 124 | K2[0].x = A->get_inv_inertia() * r1.y * r1.y; |
| 125 | K2[1].x = -A->get_inv_inertia() * r1.x * r1.y; |
| 126 | K2[0].y = -A->get_inv_inertia() * r1.x * r1.y; |
| 127 | K2[1].y = A->get_inv_inertia() * r1.x * r1.x; |
| 128 | |
| 129 | Transform2D K; |
| 130 | K[0] = K1[0] + K2[0]; |
| 131 | K[1] = K1[1] + K2[1]; |
| 132 | |
| 133 | if (B) { |
| 134 | Vector2 r2 = rB - B->get_center_of_mass(); |
| 135 | |
| 136 | Transform2D K3; |
| 137 | K3[0].x = B->get_inv_inertia() * r2.y * r2.y; |
| 138 | K3[1].x = -B->get_inv_inertia() * r2.x * r2.y; |
| 139 | K3[0].y = -B->get_inv_inertia() * r2.x * r2.y; |
| 140 | K3[1].y = B->get_inv_inertia() * r2.x * r2.x; |
| 141 | |
| 142 | K[0] += K3[0]; |
| 143 | K[1] += K3[1]; |
| 144 | } |
| 145 | |
| 146 | K[0].x += softness; |
| 147 | K[1].y += softness; |
| 148 | |
| 149 | M = K.affine_inverse(); |
| 150 | |
| 151 | Vector2 gA = rA + A->get_transform().get_origin(); |
| 152 | Vector2 gB = B ? rB + B->get_transform().get_origin() : rB; |
| 153 | |
| 154 | Vector2 delta = gB - gA; |
| 155 | |
| 156 | bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step); |
| 157 | |
| 158 | // Compute max impulse. |
| 159 | jn_max = get_max_force() * p_step; |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | inline Vector2 custom_cross(const Vector2 &p_vec, real_t p_other) { |
| 165 | return Vector2(p_other * p_vec.y, -p_other * p_vec.x); |
| 166 | } |
| 167 | |
| 168 | bool GodotPinJoint2D::pre_solve(real_t p_step) { |
| 169 | // Apply accumulated impulse. |
| 170 | if (dynamic_A) { |
| 171 | A->apply_impulse(-P, rA); |
| 172 | } |
| 173 | if (B && dynamic_B) { |
| 174 | B->apply_impulse(P, rB); |
| 175 | } |
| 176 | // Angle limits joint pre_solve step taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpRotaryLimitJoint.c |
| 177 | real_t i_sum_local = A->get_inv_inertia(); |
| 178 | if (B) { |
| 179 | i_sum_local += B->get_inv_inertia(); |
| 180 | } |
| 181 | i_sum = 1.0 / (i_sum_local); |
| 182 | if (angular_limit_enabled && B) { |
| 183 | Vector2 diff_vector = B->get_transform().get_origin() - A->get_transform().get_origin(); |
| 184 | diff_vector = diff_vector.rotated(-initial_angle); |
| 185 | real_t dist = diff_vector.angle(); |
| 186 | real_t pdist = 0.0; |
| 187 | if (dist > angular_limit_upper) { |
| 188 | pdist = dist - angular_limit_upper; |
| 189 | } else if (dist < angular_limit_lower) { |
| 190 | pdist = dist - angular_limit_lower; |
| 191 | } |
| 192 | real_t error_bias = Math::pow(1.0 - 0.15, 60.0); |
| 193 | // Calculate bias velocity. |
| 194 | bias_velocity = -CLAMP((-1.0 - Math::pow(error_bias, p_step)) * pdist / p_step, -get_max_bias(), get_max_bias()); |
| 195 | // If the bias velocity is 0, the joint is not at a limit. |
| 196 | if (bias_velocity >= -CMP_EPSILON && bias_velocity <= CMP_EPSILON) { |
| 197 | j_acc = 0; |
| 198 | is_joint_at_limit = false; |
| 199 | } else { |
| 200 | is_joint_at_limit = true; |
| 201 | } |
| 202 | } else { |
| 203 | bias_velocity = 0.0; |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | void GodotPinJoint2D::solve(real_t p_step) { |
| 210 | // Compute relative velocity. |
| 211 | Vector2 vA = A->get_linear_velocity() - custom_cross(rA - A->get_center_of_mass(), A->get_angular_velocity()); |
| 212 | |
| 213 | Vector2 rel_vel; |
| 214 | if (B) { |
| 215 | rel_vel = B->get_linear_velocity() - custom_cross(rB - B->get_center_of_mass(), B->get_angular_velocity()) - vA; |
| 216 | } else { |
| 217 | rel_vel = -vA; |
| 218 | } |
| 219 | // Angle limits joint solve step taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpRotaryLimitJoint.c |
| 220 | if ((angular_limit_enabled || motor_enabled) && B) { |
| 221 | // Compute relative rotational velocity. |
| 222 | real_t wr = B->get_angular_velocity() - A->get_angular_velocity(); |
| 223 | // Motor solve part taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpSimpleMotor.c |
| 224 | if (motor_enabled) { |
| 225 | wr -= motor_target_velocity; |
| 226 | } |
| 227 | real_t j_max = jn_max; |
| 228 | |
| 229 | // Compute normal impulse. |
| 230 | real_t j = -(bias_velocity + wr) * i_sum; |
| 231 | real_t j_old = j_acc; |
| 232 | // Only enable the limits if we have to. |
| 233 | if (angular_limit_enabled && is_joint_at_limit) { |
| 234 | if (bias_velocity < 0.0) { |
| 235 | j_acc = CLAMP(j_old + j, 0.0, j_max); |
| 236 | } else { |
| 237 | j_acc = CLAMP(j_old + j, -j_max, 0.0); |
| 238 | } |
| 239 | } else { |
| 240 | j_acc = CLAMP(j_old + j, -j_max, j_max); |
| 241 | } |
| 242 | j = j_acc - j_old; |
| 243 | A->apply_torque_impulse(-j * A->get_inv_inertia()); |
| 244 | B->apply_torque_impulse(j * B->get_inv_inertia()); |
| 245 | } |
| 246 | |
| 247 | Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P); |
| 248 | |
| 249 | if (dynamic_A) { |
| 250 | A->apply_impulse(-impulse, rA); |
| 251 | } |
| 252 | if (B && dynamic_B) { |
| 253 | B->apply_impulse(impulse, rB); |
| 254 | } |
| 255 | |
| 256 | P += impulse; |
| 257 | } |
| 258 | |
| 259 | void GodotPinJoint2D::set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value) { |
| 260 | switch (p_param) { |
| 261 | case PhysicsServer2D::PIN_JOINT_SOFTNESS: { |
| 262 | softness = p_value; |
| 263 | } break; |
| 264 | case PhysicsServer2D::PIN_JOINT_LIMIT_UPPER: { |
| 265 | angular_limit_upper = p_value; |
| 266 | } break; |
| 267 | case PhysicsServer2D::PIN_JOINT_LIMIT_LOWER: { |
| 268 | angular_limit_lower = p_value; |
| 269 | } break; |
| 270 | case PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY: { |
| 271 | motor_target_velocity = p_value; |
| 272 | } break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | real_t GodotPinJoint2D::get_param(PhysicsServer2D::PinJointParam p_param) const { |
| 277 | switch (p_param) { |
| 278 | case PhysicsServer2D::PIN_JOINT_SOFTNESS: { |
| 279 | return softness; |
| 280 | } |
| 281 | case PhysicsServer2D::PIN_JOINT_LIMIT_UPPER: { |
| 282 | return angular_limit_upper; |
| 283 | } |
| 284 | case PhysicsServer2D::PIN_JOINT_LIMIT_LOWER: { |
| 285 | return angular_limit_lower; |
| 286 | } |
| 287 | case PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY: { |
| 288 | return motor_target_velocity; |
| 289 | } |
| 290 | } |
| 291 | ERR_FAIL_V(0); |
| 292 | } |
| 293 | |
| 294 | void GodotPinJoint2D::set_flag(PhysicsServer2D::PinJointFlag p_flag, bool p_enabled) { |
| 295 | switch (p_flag) { |
| 296 | case PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED: { |
| 297 | angular_limit_enabled = p_enabled; |
| 298 | } break; |
| 299 | case PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED: { |
| 300 | motor_enabled = p_enabled; |
| 301 | } break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | bool GodotPinJoint2D::get_flag(PhysicsServer2D::PinJointFlag p_flag) const { |
| 306 | switch (p_flag) { |
| 307 | case PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED: { |
| 308 | return angular_limit_enabled; |
| 309 | } |
| 310 | case PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED: { |
| 311 | return motor_enabled; |
| 312 | } |
| 313 | } |
| 314 | ERR_FAIL_V(false); |
| 315 | } |
| 316 | |
| 317 | GodotPinJoint2D::GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b) : |
| 318 | GodotJoint2D(_arr, p_body_b ? 2 : 1) { |
| 319 | A = p_body_a; |
| 320 | B = p_body_b; |
| 321 | anchor_A = p_body_a->get_inv_transform().xform(p_pos); |
| 322 | anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos; |
| 323 | |
| 324 | p_body_a->add_constraint(this, 0); |
| 325 | if (p_body_b) { |
| 326 | p_body_b->add_constraint(this, 1); |
| 327 | initial_angle = A->get_transform().get_origin().angle_to_point(B->get_transform().get_origin()); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | ////////////////////////////////////////////// |
| 332 | ////////////////////////////////////////////// |
| 333 | ////////////////////////////////////////////// |
| 334 | |
| 335 | static inline void |
| 336 | k_tensor(GodotBody2D *a, GodotBody2D *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) { |
| 337 | // calculate mass matrix |
| 338 | // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross... |
| 339 | real_t k11, k12, k21, k22; |
| 340 | real_t m_sum = a->get_inv_mass() + b->get_inv_mass(); |
| 341 | |
| 342 | // start with I*m_sum |
| 343 | k11 = m_sum; |
| 344 | k12 = 0.0f; |
| 345 | k21 = 0.0f; |
| 346 | k22 = m_sum; |
| 347 | |
| 348 | r1 -= a->get_center_of_mass(); |
| 349 | r2 -= b->get_center_of_mass(); |
| 350 | |
| 351 | // add the influence from r1 |
| 352 | real_t a_i_inv = a->get_inv_inertia(); |
| 353 | real_t r1xsq = r1.x * r1.x * a_i_inv; |
| 354 | real_t r1ysq = r1.y * r1.y * a_i_inv; |
| 355 | real_t r1nxy = -r1.x * r1.y * a_i_inv; |
| 356 | k11 += r1ysq; |
| 357 | k12 += r1nxy; |
| 358 | k21 += r1nxy; |
| 359 | k22 += r1xsq; |
| 360 | |
| 361 | // add the influence from r2 |
| 362 | real_t b_i_inv = b->get_inv_inertia(); |
| 363 | real_t r2xsq = r2.x * r2.x * b_i_inv; |
| 364 | real_t r2ysq = r2.y * r2.y * b_i_inv; |
| 365 | real_t r2nxy = -r2.x * r2.y * b_i_inv; |
| 366 | k11 += r2ysq; |
| 367 | k12 += r2nxy; |
| 368 | k21 += r2nxy; |
| 369 | k22 += r2xsq; |
| 370 | |
| 371 | // invert |
| 372 | real_t determinant = k11 * k22 - k12 * k21; |
| 373 | ERR_FAIL_COND(determinant == 0.0); |
| 374 | |
| 375 | real_t det_inv = 1.0f / determinant; |
| 376 | *k1 = Vector2(k22 * det_inv, -k12 * det_inv); |
| 377 | *k2 = Vector2(-k21 * det_inv, k11 * det_inv); |
| 378 | } |
| 379 | |
| 380 | static _FORCE_INLINE_ Vector2 |
| 381 | mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) { |
| 382 | return Vector2(vr.dot(k1), vr.dot(k2)); |
| 383 | } |
| 384 | |
| 385 | bool GodotGrooveJoint2D::setup(real_t p_step) { |
| 386 | dynamic_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 387 | dynamic_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 388 | |
| 389 | if (!dynamic_A && !dynamic_B) { |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | GodotSpace2D *space = A->get_space(); |
| 394 | ERR_FAIL_NULL_V(space, false); |
| 395 | |
| 396 | // calculate endpoints in worldspace |
| 397 | Vector2 ta = A->get_transform().xform(A_groove_1); |
| 398 | Vector2 tb = A->get_transform().xform(A_groove_2); |
| 399 | |
| 400 | // calculate axis |
| 401 | Vector2 n = -(tb - ta).orthogonal().normalized(); |
| 402 | real_t d = ta.dot(n); |
| 403 | |
| 404 | xf_normal = n; |
| 405 | rB = B->get_transform().basis_xform(B_anchor); |
| 406 | |
| 407 | // calculate tangential distance along the axis of rB |
| 408 | real_t td = (B->get_transform().get_origin() + rB).cross(n); |
| 409 | // calculate clamping factor and rB |
| 410 | if (td <= ta.cross(n)) { |
| 411 | clamp = 1.0f; |
| 412 | rA = ta - A->get_transform().get_origin(); |
| 413 | } else if (td >= tb.cross(n)) { |
| 414 | clamp = -1.0f; |
| 415 | rA = tb - A->get_transform().get_origin(); |
| 416 | } else { |
| 417 | clamp = 0.0f; |
| 418 | //joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p); |
| 419 | rA = ((-n.orthogonal() * -td) + n * d) - A->get_transform().get_origin(); |
| 420 | } |
| 421 | |
| 422 | // Calculate mass tensor |
| 423 | k_tensor(A, B, rA, rB, &k1, &k2); |
| 424 | |
| 425 | // compute max impulse |
| 426 | jn_max = get_max_force() * p_step; |
| 427 | |
| 428 | // calculate bias velocity |
| 429 | //cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1)); |
| 430 | //joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias); |
| 431 | |
| 432 | Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA); |
| 433 | |
| 434 | real_t _b = get_bias(); |
| 435 | gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).limit_length(get_max_bias()); |
| 436 | |
| 437 | correct = true; |
| 438 | return true; |
| 439 | } |
| 440 | |
| 441 | bool GodotGrooveJoint2D::pre_solve(real_t p_step) { |
| 442 | // Apply accumulated impulse. |
| 443 | if (dynamic_A) { |
| 444 | A->apply_impulse(-jn_acc, rA); |
| 445 | } |
| 446 | if (dynamic_B) { |
| 447 | B->apply_impulse(jn_acc, rB); |
| 448 | } |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | void GodotGrooveJoint2D::solve(real_t p_step) { |
| 454 | // compute impulse |
| 455 | Vector2 vr = relative_velocity(A, B, rA, rB); |
| 456 | |
| 457 | Vector2 j = mult_k(gbias - vr, k1, k2); |
| 458 | Vector2 jOld = jn_acc; |
| 459 | j += jOld; |
| 460 | |
| 461 | jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).limit_length(jn_max); |
| 462 | |
| 463 | j = jn_acc - jOld; |
| 464 | |
| 465 | if (dynamic_A) { |
| 466 | A->apply_impulse(-j, rA); |
| 467 | } |
| 468 | if (dynamic_B) { |
| 469 | B->apply_impulse(j, rB); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | GodotGrooveJoint2D::GodotGrooveJoint2D(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, GodotBody2D *p_body_a, GodotBody2D *p_body_b) : |
| 474 | GodotJoint2D(_arr, 2) { |
| 475 | A = p_body_a; |
| 476 | B = p_body_b; |
| 477 | |
| 478 | A_groove_1 = A->get_inv_transform().xform(p_a_groove1); |
| 479 | A_groove_2 = A->get_inv_transform().xform(p_a_groove2); |
| 480 | B_anchor = B->get_inv_transform().xform(p_b_anchor); |
| 481 | A_groove_normal = -(A_groove_2 - A_groove_1).normalized().orthogonal(); |
| 482 | |
| 483 | A->add_constraint(this, 0); |
| 484 | B->add_constraint(this, 1); |
| 485 | } |
| 486 | |
| 487 | ////////////////////////////////////////////// |
| 488 | ////////////////////////////////////////////// |
| 489 | ////////////////////////////////////////////// |
| 490 | |
| 491 | bool GodotDampedSpringJoint2D::setup(real_t p_step) { |
| 492 | dynamic_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 493 | dynamic_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC); |
| 494 | |
| 495 | if (!dynamic_A && !dynamic_B) { |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | rA = A->get_transform().basis_xform(anchor_A); |
| 500 | rB = B->get_transform().basis_xform(anchor_B); |
| 501 | |
| 502 | Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA); |
| 503 | real_t dist = delta.length(); |
| 504 | |
| 505 | if (dist) { |
| 506 | n = delta / dist; |
| 507 | } else { |
| 508 | n = Vector2(); |
| 509 | } |
| 510 | |
| 511 | real_t k = k_scalar(A, B, rA, rB, n); |
| 512 | n_mass = 1.0f / k; |
| 513 | |
| 514 | target_vrn = 0.0f; |
| 515 | v_coef = 1.0f - Math::exp(-damping * (p_step)*k); |
| 516 | |
| 517 | // Calculate spring force. |
| 518 | real_t f_spring = (rest_length - dist) * stiffness; |
| 519 | j = n * f_spring * (p_step); |
| 520 | |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | bool GodotDampedSpringJoint2D::pre_solve(real_t p_step) { |
| 525 | // Apply spring force. |
| 526 | if (dynamic_A) { |
| 527 | A->apply_impulse(-j, rA); |
| 528 | } |
| 529 | if (dynamic_B) { |
| 530 | B->apply_impulse(j, rB); |
| 531 | } |
| 532 | |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | void GodotDampedSpringJoint2D::solve(real_t p_step) { |
| 537 | // compute relative velocity |
| 538 | real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn; |
| 539 | |
| 540 | // compute velocity loss from drag |
| 541 | // not 100% certain this is derived correctly, though it makes sense |
| 542 | real_t v_damp = -vrn * v_coef; |
| 543 | target_vrn = vrn + v_damp; |
| 544 | Vector2 j_new = n * v_damp * n_mass; |
| 545 | |
| 546 | if (dynamic_A) { |
| 547 | A->apply_impulse(-j_new, rA); |
| 548 | } |
| 549 | if (dynamic_B) { |
| 550 | B->apply_impulse(j_new, rB); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void GodotDampedSpringJoint2D::set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value) { |
| 555 | switch (p_param) { |
| 556 | case PhysicsServer2D::DAMPED_SPRING_REST_LENGTH: { |
| 557 | rest_length = p_value; |
| 558 | } break; |
| 559 | case PhysicsServer2D::DAMPED_SPRING_DAMPING: { |
| 560 | damping = p_value; |
| 561 | } break; |
| 562 | case PhysicsServer2D::DAMPED_SPRING_STIFFNESS: { |
| 563 | stiffness = p_value; |
| 564 | } break; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | real_t GodotDampedSpringJoint2D::get_param(PhysicsServer2D::DampedSpringParam p_param) const { |
| 569 | switch (p_param) { |
| 570 | case PhysicsServer2D::DAMPED_SPRING_REST_LENGTH: { |
| 571 | return rest_length; |
| 572 | } break; |
| 573 | case PhysicsServer2D::DAMPED_SPRING_DAMPING: { |
| 574 | return damping; |
| 575 | } break; |
| 576 | case PhysicsServer2D::DAMPED_SPRING_STIFFNESS: { |
| 577 | return stiffness; |
| 578 | } break; |
| 579 | } |
| 580 | |
| 581 | ERR_FAIL_V(0); |
| 582 | } |
| 583 | |
| 584 | GodotDampedSpringJoint2D::GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b) : |
| 585 | GodotJoint2D(_arr, 2) { |
| 586 | A = p_body_a; |
| 587 | B = p_body_b; |
| 588 | anchor_A = A->get_inv_transform().xform(p_anchor_a); |
| 589 | anchor_B = B->get_inv_transform().xform(p_anchor_b); |
| 590 | |
| 591 | rest_length = p_anchor_a.distance_to(p_anchor_b); |
| 592 | |
| 593 | A->add_constraint(this, 0); |
| 594 | B->add_constraint(this, 1); |
| 595 | } |