1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/* 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.
*/
#include <stdlib.h>
#include <math.h>
#include "chipmunk.h"
// TODO: Comment me!
cpFloat cp_joint_bias_coef = 0.1f;
void cpJointDestroy(cpJoint *joint){}
void
cpJointFree(cpJoint *joint)
{
if(joint) cpJointDestroy(joint);
free(joint);
}
static void
pinJointPreStep(cpJoint *joint, cpFloat dt_inv)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpPinJoint *jnt = (cpPinJoint *)joint;
cpFloat mass_sum = a->m_inv + b->m_inv;
jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
cpFloat dist = cpvlength(delta);
jnt->n = cpvmult(delta, 1.0f/(dist ? dist : INFINITY));
// calculate mass normal
cpFloat r1cn = cpvcross(jnt->r1, jnt->n);
cpFloat r2cn = cpvcross(jnt->r2, jnt->n);
cpFloat kn = mass_sum + a->i_inv*r1cn*r1cn + b->i_inv*r2cn*r2cn;
jnt->nMass = 1.0f/kn;
// calculate bias velocity
jnt->bias = -cp_joint_bias_coef*dt_inv*(dist - jnt->dist);
jnt->jBias = 0.0f;
// apply accumulated impulse
cpVect j = cpvmult(jnt->n, jnt->jnAcc);
cpBodyApplyImpulse(a, cpvneg(j), jnt->r1);
cpBodyApplyImpulse(b, j, jnt->r2);
}
static void
pinJointApplyImpulse(cpJoint *joint)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpPinJoint *jnt = (cpPinJoint *)joint;
cpVect n = jnt->n;
cpVect r1 = jnt->r1;
cpVect r2 = jnt->r2;
//calculate bias impulse
cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias));
cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias));
cpFloat vbn = cpvdot(cpvsub(vb2, vb1), n);
cpFloat jbn = (jnt->bias - vbn)*jnt->nMass;
jnt->jBias += jbn;
cpVect jb = cpvmult(n, jbn);
cpBodyApplyBiasImpulse(a, cpvneg(jb), r1);
cpBodyApplyBiasImpulse(b, jb, r2);
// compute relative velocity
cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
cpFloat vrn = cpvdot(cpvsub(v2, v1), n);
// compute normal impulse
cpFloat jn = -vrn*jnt->nMass;
jnt->jnAcc =+ jn;
// apply impulse
cpVect j = cpvmult(n, jn);
cpBodyApplyImpulse(a, cpvneg(j), r1);
cpBodyApplyImpulse(b, j, r2);
}
cpPinJoint *
cpPinJointAlloc(void)
{
return (cpPinJoint *)malloc(sizeof(cpPinJoint));
}
cpPinJoint *
cpPinJointInit(cpPinJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
{
joint->joint.preStep = &pinJointPreStep;
joint->joint.applyImpulse = &pinJointApplyImpulse;
joint->joint.a = a;
joint->joint.b = b;
((cpJoint*)joint)->type = CP_PIN_JOINT;
joint->anchr1 = anchr1;
joint->anchr2 = anchr2;
cpVect p1 = cpvadd(a->p, cpvrotate(anchr1, a->rot));
cpVect p2 = cpvadd(b->p, cpvrotate(anchr2, b->rot));
joint->dist = cpvlength(cpvsub(p2, p1));
joint->jnAcc = 0.0;
return joint;
}
cpJoint *
cpPinJointNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2)
{
return (cpJoint *)cpPinJointInit(cpPinJointAlloc(), a, b, anchr1, anchr2);
}
static void
SlideJointPreStep(cpJoint *joint, cpFloat dt_inv)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpSlideJoint *jnt = (cpSlideJoint *)joint;
cpFloat mass_sum = a->m_inv + b->m_inv;
jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
cpFloat dist = cpvlength(delta);
cpFloat pdist = 0.0;
if(dist > jnt->max) {
pdist = dist - jnt->max;
} else if(dist < jnt->min) {
pdist = jnt->min - dist;
dist = -dist;
}
jnt->n = cpvmult(delta, 1.0f/(dist ? dist : INFINITY));
// calculate mass normal
cpFloat r1cn = cpvcross(jnt->r1, jnt->n);
cpFloat r2cn = cpvcross(jnt->r2, jnt->n);
cpFloat kn = mass_sum + a->i_inv*r1cn*r1cn + b->i_inv*r2cn*r2cn;
jnt->nMass = 1.0f/kn;
// calculate bias velocity
jnt->bias = -cp_joint_bias_coef*dt_inv*(pdist);
jnt->jBias = 0.0f;
// apply accumulated impulse
if(!jnt->bias) jnt->jnAcc = 0.0f;
cpVect j = cpvmult(jnt->n, jnt->jnAcc);
cpBodyApplyImpulse(a, cpvneg(j), jnt->r1);
cpBodyApplyImpulse(b, j, jnt->r2);
}
static void
SlideJointApplyImpulse(cpJoint *joint)
{
cpSlideJoint *jnt = (cpSlideJoint *)joint;
if(!jnt->bias) return;
cpBody *a = joint->a;
cpBody *b = joint->b;
cpVect n = jnt->n;
cpVect r1 = jnt->r1;
cpVect r2 = jnt->r2;
//calculate bias impulse
cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias));
cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias));
cpFloat vbn = cpvdot(cpvsub(vb2, vb1), n);
cpFloat jbn = (jnt->bias - vbn)*jnt->nMass;
cpFloat jbnOld = jnt->jBias;
jnt->jBias = cpfmin(jbnOld + jbn, 0.0f);
jbn = jnt->jBias - jbnOld;
cpVect jb = cpvmult(n, jbn);
cpBodyApplyBiasImpulse(a, cpvneg(jb), r1);
cpBodyApplyBiasImpulse(b, jb, r2);
// compute relative velocity
cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
cpFloat vrn = cpvdot(cpvsub(v2, v1), n);
// compute normal impulse
cpFloat jn = -vrn*jnt->nMass;
cpFloat jnOld = jnt->jnAcc;
jnt->jnAcc = cpfmin(jnOld + jn, 0.0f);
jn = jnt->jnAcc - jnOld;
// apply impulse
cpVect j = cpvmult(n, jn);
cpBodyApplyImpulse(a, cpvneg(j), r1);
cpBodyApplyImpulse(b, j, r2);
}
cpSlideJoint *
cpSlideJointAlloc(void)
{
return (cpSlideJoint *)malloc(sizeof(cpSlideJoint));
}
cpSlideJoint *
cpSlideJointInit(cpSlideJoint *joint, cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max)
{
joint->joint.preStep = &SlideJointPreStep;
joint->joint.applyImpulse = &SlideJointApplyImpulse;
joint->joint.a = a;
joint->joint.b = b;
((cpJoint*)joint)->type = CP_SLIDE_JOINT;
joint->anchr1 = anchr1;
joint->anchr2 = anchr2;
joint->min = min;
joint->max = max;
joint->jnAcc = 0.0;
return joint;
}
cpJoint *
cpSlideJointNew(cpBody *a, cpBody *b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max)
{
return (cpJoint *)cpSlideJointInit(cpSlideJointAlloc(), a, b, anchr1, anchr2, min, max);
}
static void
pivotJointPreStep(cpJoint *joint, cpFloat dt_inv)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpPivotJoint *jnt = (cpPivotJoint *)joint;
jnt->r1 = cpvrotate(jnt->anchr1, a->rot);
jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
// calculate mass matrix
// If I wasn't lazy, this wouldn't be so gross...
cpFloat k11, k12, k21, k22;
cpFloat m_sum = a->m_inv + b->m_inv;
k11 = m_sum; k12 = 0.0f;
k21 = 0.0f; k22 = m_sum;
cpFloat r1xsq = jnt->r1.x * jnt->r1.x * a->i_inv;
cpFloat r1ysq = jnt->r1.y * jnt->r1.y * a->i_inv;
cpFloat r1nxy = -jnt->r1.x * jnt->r1.y * a->i_inv;
k11 += r1ysq; k12 += r1nxy;
k21 += r1nxy; k22 += r1xsq;
cpFloat r2xsq = jnt->r2.x * jnt->r2.x * b->i_inv;
cpFloat r2ysq = jnt->r2.y * jnt->r2.y * b->i_inv;
cpFloat r2nxy = -jnt->r2.x * jnt->r2.y * b->i_inv;
k11 += r2ysq; k12 += r2nxy;
k21 += r2nxy; k22 += r2xsq;
cpFloat det_inv = 1.0f/(k11*k22 - k12*k21);
jnt->k1 = cpv( k22*det_inv, -k12*det_inv);
jnt->k2 = cpv(-k21*det_inv, k11*det_inv);
// calculate bias velocity
cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
jnt->bias = cpvmult(delta, -cp_joint_bias_coef*dt_inv);
jnt->jBias = cpvzero;
// apply accumulated impulse
cpBodyApplyImpulse(a, cpvneg(jnt->jAcc), jnt->r1);
cpBodyApplyImpulse(b, jnt->jAcc, jnt->r2);
}
static void
pivotJointApplyImpulse(cpJoint *joint)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpPivotJoint *jnt = (cpPivotJoint *)joint;
cpVect r1 = jnt->r1;
cpVect r2 = jnt->r2;
cpVect k1 = jnt->k1;
cpVect k2 = jnt->k2;
//calculate bias impulse
cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias));
cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias));
cpVect vbr = cpvsub(jnt->bias, cpvsub(vb2, vb1));
cpVect jb = cpv(cpvdot(vbr, k1), cpvdot(vbr, k2));
jnt->jBias = cpvadd(jnt->jBias, jb);
cpBodyApplyBiasImpulse(a, cpvneg(jb), r1);
cpBodyApplyBiasImpulse(b, jb, r2);
// compute relative velocity
cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
cpVect vr = cpvsub(v2, v1);
// compute normal impulse
cpVect j = cpv(-cpvdot(vr, k1), -cpvdot(vr, k2));
jnt->jAcc = cpvadd(jnt->jAcc, j);
// apply impulse
cpBodyApplyImpulse(a, cpvneg(j), r1);
cpBodyApplyImpulse(b, j, r2);
}
cpPivotJoint *
cpPivotJointAlloc(void)
{
return (cpPivotJoint *)malloc(sizeof(cpPivotJoint));
}
cpPivotJoint *
cpPivotJointInit(cpPivotJoint *joint, cpBody *a, cpBody *b, cpVect pivot)
{
joint->joint.preStep = &pivotJointPreStep;
joint->joint.applyImpulse = &pivotJointApplyImpulse;
joint->joint.a = a;
joint->joint.b = b;
((cpJoint*)joint)->type = CP_PIVOT_JOINT;
joint->anchr1 = cpvunrotate(cpvsub(pivot, a->p), a->rot);
joint->anchr2 = cpvunrotate(cpvsub(pivot, b->p), b->rot);
joint->jAcc = cpvzero;
return joint;
}
cpJoint *
cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot)
{
return (cpJoint *)cpPivotJointInit(cpPivotJointAlloc(), a, b, pivot);
}
static void
grooveJointPreStep(cpJoint *joint, cpFloat dt_inv)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpGrooveJoint *jnt = (cpGrooveJoint *)joint;
// calculate endpoints in worldspace
cpVect ta = cpBodyLocal2World(a, jnt->grv_a);
cpVect tb = cpBodyLocal2World(a, jnt->grv_b);
// calculate axis
cpVect n = cpvrotate(jnt->grv_n, a->rot);
cpFloat d = cpvdot(ta, n);
jnt->grv_tn = n;
jnt->r2 = cpvrotate(jnt->anchr2, b->rot);
// calculate tangential distance along the axis of r2
cpFloat td = cpvcross(cpvadd(b->p, jnt->r2), n);
// calculate clamping factor and r2
if(td < cpvcross(ta, n)){
jnt->clamp = 1.0f;
jnt->r1 = cpvsub(ta, a->p);
} else if(td > cpvcross(tb, n)){
jnt->clamp = -1.0f;
jnt->r1 = cpvsub(tb, a->p);
} else {
jnt->clamp = 0.0f;
jnt->r1 = cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d));
}
// calculate mass matrix
// If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
cpFloat k11, k12, k21, k22;
cpFloat m_sum = a->m_inv + b->m_inv;
// start with I*m_sum
k11 = m_sum; k12 = 0.0f;
k21 = 0.0f; k22 = m_sum;
// add the influence from r1
cpFloat r1xsq = jnt->r1.x * jnt->r1.x * a->i_inv;
cpFloat r1ysq = jnt->r1.y * jnt->r1.y * a->i_inv;
cpFloat r1nxy = -jnt->r1.x * jnt->r1.y * a->i_inv;
k11 += r1ysq; k12 += r1nxy;
k21 += r1nxy; k22 += r1xsq;
// add the influnce from r2
cpFloat r2xsq = jnt->r2.x * jnt->r2.x * b->i_inv;
cpFloat r2ysq = jnt->r2.y * jnt->r2.y * b->i_inv;
cpFloat r2nxy = -jnt->r2.x * jnt->r2.y * b->i_inv;
k11 += r2ysq; k12 += r2nxy;
k21 += r2nxy; k22 += r2xsq;
// invert
cpFloat det_inv = 1.0f/(k11*k22 - k12*k21);
jnt->k1 = cpv( k22*det_inv, -k12*det_inv);
jnt->k2 = cpv(-k21*det_inv, k11*det_inv);
// calculate bias velocity
cpVect delta = cpvsub(cpvadd(b->p, jnt->r2), cpvadd(a->p, jnt->r1));
jnt->bias = cpvmult(delta, -cp_joint_bias_coef*dt_inv);
jnt->jBias = cpvzero;
// apply accumulated impulse
cpBodyApplyImpulse(a, cpvneg(jnt->jAcc), jnt->r1);
cpBodyApplyImpulse(b, jnt->jAcc, jnt->r2);
}
static inline cpVect
grooveConstrain(cpGrooveJoint *jnt, cpVect j){
cpVect n = jnt->grv_tn;
cpVect jn = cpvmult(n, cpvdot(j, n));
cpVect t = cpvperp(n);
cpFloat coef = (jnt->clamp*cpvcross(j, n) > 0.0f) ? 1.0f : 0.0f;
cpVect jt = cpvmult(t, cpvdot(j, t)*coef);
return cpvadd(jn, jt);
}
static void
grooveJointApplyImpulse(cpJoint *joint)
{
cpBody *a = joint->a;
cpBody *b = joint->b;
cpGrooveJoint *jnt = (cpGrooveJoint *)joint;
cpVect r1 = jnt->r1;
cpVect r2 = jnt->r2;
cpVect k1 = jnt->k1;
cpVect k2 = jnt->k2;
//calculate bias impulse
cpVect vb1 = cpvadd(a->v_bias, cpvmult(cpvperp(r1), a->w_bias));
cpVect vb2 = cpvadd(b->v_bias, cpvmult(cpvperp(r2), b->w_bias));
cpVect vbr = cpvsub(jnt->bias, cpvsub(vb2, vb1));
cpVect jb = cpv(cpvdot(vbr, k1), cpvdot(vbr, k2));
cpVect jbOld = jnt->jBias;
jnt->jBias = grooveConstrain(jnt, cpvadd(jbOld, jb));
jb = cpvsub(jnt->jBias, jbOld);
cpBodyApplyBiasImpulse(a, cpvneg(jb), r1);
cpBodyApplyBiasImpulse(b, jb, r2);
// compute relative velocity
cpVect v1 = cpvadd(a->v, cpvmult(cpvperp(r1), a->w));
cpVect v2 = cpvadd(b->v, cpvmult(cpvperp(r2), b->w));
cpVect vr = cpvsub(v2, v1);
// compute impulse
cpVect j = cpv(-cpvdot(vr, k1), -cpvdot(vr, k2));
cpVect jOld = jnt->jAcc;
jnt->jAcc = grooveConstrain(jnt, cpvadd(jOld, j));
j = cpvsub(jnt->jAcc, jOld);
// apply impulse
cpBodyApplyImpulse(a, cpvneg(j), r1);
cpBodyApplyImpulse(b, j, r2);
}
cpGrooveJoint *
cpGrooveJointAlloc(void)
{
return (cpGrooveJoint *)malloc(sizeof(cpGrooveJoint));
}
cpGrooveJoint *
cpGrooveJointInit(cpGrooveJoint *joint, cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
{
joint->joint.preStep = &grooveJointPreStep;
joint->joint.applyImpulse = &grooveJointApplyImpulse;
joint->joint.a = a;
joint->joint.b = b;
((cpJoint*)joint)->type = CP_GROOVE_JOINT;
joint->grv_a = groove_a;
joint->grv_b = groove_b;
joint->grv_n = cpvperp(cpvnormalize(cpvsub(groove_b, groove_a)));
joint->anchr2 = anchr2;
joint->jAcc = cpvzero;
return joint;
}
cpJoint *
cpGrooveJointNew(cpBody *a, cpBody *b, cpVect groove_a, cpVect groove_b, cpVect anchr2)
{
return (cpJoint *)cpGrooveJointInit(cpGrooveJointAlloc(), a, b, groove_a, groove_b, anchr2);
}
|