static cpBool StickyPreSolve(cpArbiter *arb, cpSpace *space, void *data) { // We want to fudge the collisions a bit to allow shapes to overlap more. // This simulates their squishy sticky surface, and more importantly // keeps them from separating and destroying the joint. // Track the deepest collision point and use that to determine if a rigid collision should occur. cpFloat deepest = INFINITY; // Grab the contact set and iterate over them. cpContactPointSet contacts = cpArbiterGetContactPointSet(arb); for(int i=0; i<contacts.count; i++){ // Increase the distance (negative means overlaping) of the // collision to allow them to overlap more. // This value is used only for fixing the positions of overlapping shapes. cpFloat dist = contacts.points[i].dist + 2.0f*STICK_SENSOR_THICKNESS; contacts.points[i].dist = cpfmin(0.0f, dist); deepest = cpfmin(deepest, dist); } // Set the new contact point data. cpArbiterSetContactPointSet(arb, &contacts); // If the shapes are overlapping enough, then create a // joint that sticks them together at the first contact point. if(!cpArbiterGetUserData(arb) && deepest <= 0.0f){ CP_ARBITER_GET_BODIES(arb, bodyA, bodyB); // Create a joint at the contact point to hold the body in place. cpConstraint *joint = cpPivotJointNew(bodyA, bodyB, contacts.points[0].point); // Give it a finite force for the stickyness. cpConstraintSetMaxForce(joint, 3e3); // Schedule a post-step() callback to add the joint. cpSpaceAddPostStepCallback(space, PostStepAddJoint, joint, NULL); // Store the joint on the arbiter so we can remove it later. cpArbiterSetUserData(arb, joint); } // Position correction and velocity are handled separately so changing // the overlap distance alone won't prevent the collision from occuring. // Explicitly the collision for this frame if the shapes don't overlap using the new distance. return (deepest <= 0.0f); // Lots more that you could improve upon here as well: // * Modify the joint over time to make it plastic. // * Modify the joint in the post-step to make it conditionally plastic (like clay). // * Track a joint for the deepest contact point instead of the first. // * Track a joint for each contact point. (more complicated since you only get one data pointer). }
static cpBool HookCrate(cpArbiter *arb, cpSpace *space, void *data) { if(hookJoint == NULL){ // Get pointers to the two bodies in the collision pair and define local variables for them. // Their order matches the order of the collision types passed // to the collision handler this function was defined for CP_ARBITER_GET_BODIES(arb, hook, crate); // additions and removals can't be done in a normal callback. // Schedule a post step callback to do it. // Use the hook as the key and pass along the arbiter. cpSpaceAddPostStepCallback(space, (cpPostStepFunc)AttachHook, hook, crate); } return cpTrue; // return value is ignored for sensor callbacks anyway }
static cpBool handle_collision(cpArbiter *arbiter, cpSpace *space, cpDataPointer user_data) { CP_ARBITER_GET_BODIES(arbiter, b1, b2); erlmunk_subscriber *subscriber = (erlmunk_subscriber *) user_data; erlmunk_body_data *data1 = cpBodyGetUserData(b1); erlmunk_body_data *data2 = cpBodyGetUserData(b2); // DEBUGF(("collision detected between %d and %d\n", data1->id, data2->id)); ETERM **data1_array = (ETERM **) malloc(sizeof(ETERM) * 2); data1_array[0] = erl_mk_int(data1->id); data1_array[1] = data1->term; ETERM *data1_term = erl_mk_tuple(data1_array, 2); free(data1_array); ETERM **data2_array = (ETERM **) malloc(sizeof(ETERM) * 2); data2_array[0] = erl_mk_int(data2->id); data2_array[1] = data2->term; ETERM *data2_term = erl_mk_tuple(data2_array, 2); free(data2_array); ETERM *a = erl_mk_atom("erlmunk_collision"); ETERM **data_array = (ETERM **) malloc(sizeof(ETERM) * 3); data_array[0] = a; data_array[1] = data1_term; data_array[2] = data2_term; ETERM *data = erl_mk_tuple(data_array, 3); free(data_array); ETERM *gen_cast = erl_mk_gen_cast(data); if (erl_send(subscriber->client->fd, subscriber->from, gen_cast) != 1) { DEBUGF(("failed to send data to subscriber")); } return cpFalse; }