| 385 | } |
| 386 | |
| 387 | cpBool stickyPreSolve( Arbiter* arb, Space* space, void* ) { |
| 388 | // We want to fudge the collisions a bit to allow shapes to overlap more. |
| 389 | // This simulates their squishy sticky surface, and more importantly |
| 390 | // keeps them from separating and destroying the joint. |
| 391 | |
| 392 | // Track the deepest collision point and use that to determine if a rigid collision should |
| 393 | // occur. |
| 394 | cpFloat deepest = INFINITY; |
| 395 | |
| 396 | // Grab the contact set and iterate over them. |
| 397 | cpContactPointSet contacts = arb->getContactPointSet(); |
| 398 | |
| 399 | for ( int i = 0; i < contacts.count; i++ ) { |
| 400 | // Increase the distance (negative means overlapping) of the |
| 401 | // collision to allow them to overlap more. |
| 402 | // This value is used only for fixing the positions of overlapping shapes. |
| 403 | cpFloat dist = contacts.points[i].dist + 2.0f * STICK_SENSOR_THICKNESS; |
| 404 | contacts.points[i].dist = eemin<Float>( 0.0f, dist ); |
| 405 | deepest = eemin<Float>( deepest, dist ); |
| 406 | } |
| 407 | |
| 408 | // Set the new contact point data. |
| 409 | arb->setContactPointSet( &contacts ); |
| 410 | |
| 411 | // If the shapes are overlapping enough, then create a |
| 412 | // joint that sticks them together at the first contact point. |
| 413 | |
| 414 | if ( !arb->getUserData() && deepest <= 0.0f ) { |
| 415 | Body *bodyA, *bodyB; |
| 416 | arb->getBodies( &bodyA, &bodyB ); |
| 417 | |
| 418 | // Create a joint at the contact point to hold the body in place. |
| 419 | PivotJoint* joint = |
| 420 | eeNew( PivotJoint, ( bodyA, bodyB, tovect( contacts.points[0].point ) ) ); |
| 421 | |
| 422 | // Dont draw the constraint |
| 423 | joint->setDrawPointSize( 0 ); |
| 424 | |
| 425 | // Give it a finite force for the stickiness. |
| 426 | joint->setMaxForce( 3e3 ); |
| 427 | |
| 428 | // Schedule a post-step() callback to add the joint. |
| 429 | space->addPostStepCallback( &postStepAddJoint, joint, NULL ); |
| 430 | |
| 431 | // Store the joint on the arbiter so we can remove it later. |
| 432 | arb->setUserData( joint ); |
| 433 | } |
| 434 | |
| 435 | // Position correction and velocity are handled separately so changing |
| 436 | // the overlap distance alone won't prevent the collision from occurring. |
| 437 | // Explicitly the collision for this frame if the shapes don't overlap using the new distance. |
| 438 | return ( deepest <= 0.0f ); |
| 439 | |
| 440 | // Lots more that you could improve upon here as well: |
| 441 | // * Modify the joint over time to make it plastic. |
| 442 | // * Modify the joint in the post-step to make it conditionally plastic (like clay). |
| 443 | // * Track a joint for the deepest contact point instead of the first. |
| 444 | // * Track a joint for each contact point. (more complicated since you only get one data |
nothing calls this directly
no test coverage detected