/* Simple test of cpConstraintIsGearJoint(). */ void test_cpConstraintIsGearJoint(void) { cpConstraint *isGear = cpGearJointNew(body1, body2, 1, 1); cpConstraint *isNotGear = cpGrooveJointNew(body1, body2, cpv(0, 0), cpv(0, 0), cpv(3, 3)); CU_ASSERT(cpConstraintIsGearJoint(isGear)); CU_ASSERT_FALSE(cpConstraintIsGearJoint(isNotGear)); }
/* Simple test of cpConstraintIsDampedSpring(). */ void test_cpConstraintIsDampedSpring(void) { cpConstraint *isDampedSpring = cpDampedSpringNew(body1, body2, cpv(0,0), cpv(0,0), 1, 1, 1); cpConstraint *isNotDampedSpring = cpGrooveJointNew(body1, body2, cpv(0, 0), cpv(0, 0), cpv(3, 3)); CU_ASSERT(cpConstraintIsDampedSpring(isDampedSpring)); CU_ASSERT_FALSE(cpConstraintIsDampedSpring(isNotDampedSpring)); }
SGPhysicsConstraint* SG_CALL sgPhysicsConstraintCreateGroove(SGPhysicsBody* body1, SGPhysicsBody* body2, float x1, float y1, float x2, float y2, float xa, float ya) { SGPhysicsConstraint* constr = sgPhysicsConstraintCreate(body1, body2, SG_CONSTRAINT_GROOVE); if(!constr) return NULL; constr->handle = cpGrooveJointNew(body1->handle, body2->handle, cpv(x1, y1), cpv(x2, y2), cpv(xa, ya)); _postCreate(constr); return constr; }
WorldConstraint_t *worldConstr_createGrooveJoint(WorldEntity_t *a, WorldEntity_t *b, vec2_t aGrooveStart, vec2_t aGrooveEnd, vec2_t aAnchorB) { dynamo_assert(a->world == b->world, "Entities are not in the same world"); WorldConstraint_t *ret = obj_create_autoreleased(&Class_WorldConstraint); ret->world = a->world; ret->a = obj_retain(a); ret->b = obj_retain(b); ret->type = kWorldJointType_Groove; ret->cpConstraint = cpGrooveJointNew(a->cpBody, b->cpBody, VEC2_TO_CPV(aGrooveStart), VEC2_TO_CPV(aGrooveEnd), VEC2_TO_CPV(aAnchorB)); cpSpaceAddConstraint(ret->world->cpSpace, ret->cpConstraint); return ret; }
bool PhysicsJointGroove::createConstraints() { do { auto joint = cpGrooveJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::point2cpv(_grooveA), PhysicsHelper::point2cpv(_grooveB), PhysicsHelper::point2cpv(_anchr2)); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); return true; } while (false); return false; }
cpConstraint *cpSpaceSerializer::createGrooveJoint(TiXmlElement *elm) { cpConstraint *constraint; cpVect grv_a = createPoint("grv_a", elm); cpVect grv_b = createPoint("grv_b", elm); cpVect anchr2 = createPoint("anchr2", elm); cpBody *a; cpBody *b; createBodies(elm, &a, &b); constraint = cpGrooveJointNew(a, b, grv_a, grv_b, anchr2); //((cpGrooveJoint*)constraint)->jAcc = createPoint("jAcc", elm); //((cpGrooveJoint*)constraint)->jMaxLen = createValue<cpFloat>("jMaxLen", elm); return constraint; }
bool PhysicsJointGroove::init(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); auto constraint = cpGrooveJointNew(a->getCPBody(), b->getCPBody(), PhysicsHelper::point2cpv(grooveA), PhysicsHelper::point2cpv(grooveB), PhysicsHelper::point2cpv(anchr2)); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); return true; } while (false); return false; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Control the crane by moving the mouse. Press the down arrow to release."; space = cpSpaceNew(); cpSpaceSetIterations(space, 30); cpSpaceSetGravity(space, cpv(0, -100)); cpSpaceSetDamping(space, 0.8); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); // Add a body for the dolly. dollyBody = cpSpaceAddBody(space, cpBodyNew(10, INFINITY)); cpBodySetPos(dollyBody, cpv(0, 100)); // Add a block so you can see it. cpSpaceAddShape(space, cpBoxShapeNew(dollyBody, 30, 30)); // Add a groove joint for it to move back and forth on. cpSpaceAddConstraint(space, cpGrooveJointNew(staticBody, dollyBody, cpv(-250, 100), cpv(250, 100), cpvzero)); // Add a pivot joint to act as a servo motor controlling it's position // By updating the anchor points of the pivot joint, you can move the dolly. dollyServo = cpSpaceAddConstraint(space, cpPivotJointNew(staticBody, dollyBody, cpBodyGetPos(dollyBody))); // Max force the dolly servo can generate. cpConstraintSetMaxForce(dollyServo, 10000); // Max speed of the dolly servo cpConstraintSetMaxBias(dollyServo, 100); // You can also change the error bias to control how it slows down. //cpConstraintSetErrorBias(dollyServo, 0.2); // Add the crane hook. cpBody *hookBody = cpSpaceAddBody(space, cpBodyNew(1, INFINITY)); cpBodySetPos(hookBody, cpv(0, 50)); // Add a sensor shape for it. This will be used to figure out when the hook touches a box. shape = cpSpaceAddShape(space, cpCircleShapeNew(hookBody, 10, cpvzero)); cpShapeSetSensor(shape, cpTrue); cpShapeSetCollisionType(shape, HOOK_SENSOR); // Add a slide joint to act as a winch motor // By updating the max length of the joint you can make it pull up the load. winchServo = cpSpaceAddConstraint(space, cpSlideJointNew(dollyBody, hookBody, cpvzero, cpvzero, 0, INFINITY)); // Max force the dolly servo can generate. cpConstraintSetMaxForce(winchServo, 30000); // Max speed of the dolly servo cpConstraintSetMaxBias(winchServo, 60); // TODO cleanup // Finally a box to play with cpBody *boxBody = cpSpaceAddBody(space, cpBodyNew(30, cpMomentForBox(30, 50, 50))); cpBodySetPos(boxBody, cpv(200, -200)); // Add a block so you can see it. shape = cpSpaceAddShape(space, cpBoxShapeNew(boxBody, 50, 50)); cpShapeSetFriction(shape, 0.7); cpShapeSetCollisionType(shape, CRATE); cpSpaceAddCollisionHandler(space, HOOK_SENSOR, CRATE, (cpCollisionBeginFunc)HookCrate, NULL, NULL, NULL, NULL); return space; }
void Truck::setupChipmunk1() { DoodleTruck * doodleTruck = DoodleTruck::sharedDoodleTruck(); cpSpace *space = doodleTruck->getSpace(); Vec2 s_p = Vec2(doodleTruck->getScaleY(), doodleTruck->getScaleY()); double mass = TRUCK_MASS; //body Vec2 wd_ht_half = Vec2(128, 64); cpBody *body = cpBodyNew(mass, cpMomentForBox(mass, wd_ht_half.x * 2 * doodleTruck->getScaleX(), wd_ht_half.y * 2 * doodleTruck->getScaleY())); int num = 6; cpVect body_verts1[] = { cpv(-115.4f * doodleTruck->getScaleY(), -26.0f * doodleTruck->getScaleY()), cpv(-114.9f * doodleTruck->getScaleY(), 14.0f * doodleTruck->getScaleY()), cpv(68.9f * doodleTruck->getScaleY(), 14.5f * doodleTruck->getScaleY()), cpv(124.8f * doodleTruck->getScaleY(), 1.0f * doodleTruck->getScaleY()), cpv(125.8f * doodleTruck->getScaleY(), -46.5f * doodleTruck->getScaleY()), cpv(111.8f * doodleTruck->getScaleY(), -57.4f * doodleTruck->getScaleY()), cpv(40.0f * doodleTruck->getScaleY(), -57.4f * doodleTruck->getScaleY()) }; cpShape *shape = cpPolyShapeNew(body, num, body_verts1, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = bodySprite; cpSpaceAddShape(space, shape); num = 5; cpVect body_verts2[] = { cpv(46.2f * doodleTruck->getScaleY(), 42.3f * doodleTruck->getScaleY()), cpv(69.1f * doodleTruck->getScaleY(), 12.0f * doodleTruck->getScaleY()), cpv(53.9f * doodleTruck->getScaleY(), 11.3f * doodleTruck->getScaleY()), cpv(37.0f * doodleTruck->getScaleY(), 34.9f * doodleTruck->getScaleY()), cpv(39.1f * doodleTruck->getScaleY(), 41.6f * doodleTruck->getScaleY()) }; shape = cpPolyShapeNew(body, num, body_verts2, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; cpSpaceAddShape(space, shape); body->p.x = bodySprite->getPosition().x; body->p.y = bodySprite->getPosition().y; cpSpaceAddBody(space, body); // ------------ driver body------------ mass = 0.5; cpBody * driver_body = cpBodyNew(mass, cpMomentForBox(mass, 27 * doodleTruck->getScaleX(), 60 * doodleTruck->getScaleY())); num = 8; cpVect body_verts4[] = { cpv(-23.0f, 31.2f), cpv(-12.0f, 31.2f), cpv(-3.5f, 14.0f), cpv(-1.5f, -24.0f), cpv(-8.0f, -31.0f), cpv(-27.5f, -30.5f), cpv(-31.0f, -25.2f), cpv(-30.2f, 19.2f) }; shape = cpPolyShapeNew(driver_body, num, body_verts4, cpvzero); shape->e = 0; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = driver_Body; cpSpaceAddShape(space, shape); num = 4; cpVect body_verts5[] = { cpv(-2.7f, 8.3f), cpv(17.1f, 8.8f), cpv(18.5f, -1.6f), cpv(-3.0f, -4.1f) }; shape = cpPolyShapeNew(driver_body, num, body_verts5, cpvzero); shape->e = 0; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; // shape->data = driver_Body; cpSpaceAddShape(space, shape); num = 5; cpVect body_verts6[] = { cpv(16.9f, 8.5f), cpv(26.1f, 13.1f), cpv(32.0f, 6.7f), cpv(26.8f, -1.4f), cpv(18.0f, -0.7f) }; shape = cpPolyShapeNew(driver_body, num, body_verts6, cpvzero); shape->e = 0; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = driver_Body; cpSpaceAddShape(space, shape); driver_body->p.x = driver_Body->getPosition().x; driver_body->p.y = driver_Body->getPosition().y; cpSpaceAddBody(space, driver_body); // ------- driver-head------------ mass = 0.3; cpBody * driver_head = cpBodyNew(mass, cpMomentForCircle(mass, 20 * doodleTruck->getScaleY(), 20 * doodleTruck->getScaleY(), cpvzero)); num = 4; cpVect body_verts7[] = { cpv(9.9f, -15.6f), cpv(8.3f, -24.0f), cpv(-6.7f, -23.0f), cpv(-6.0f, -11.3f) }; shape = cpPolyShapeNew(driver_head, num, body_verts7, cpvzero); shape->e = 0; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = driver_Head; cpSpaceAddShape(space, shape); num = 7; cpVect body_verts8[] = { cpv(3.2f, 30.7f), cpv(26.5f, 8.0f), cpv(19.7f, -12.0f), cpv(9.5f, -16.2f), cpv(-13.5f, -9.5f), cpv(-20.7f, 17.7f), cpv(-12.2f, 29.2f) }; shape = cpPolyShapeNew(driver_head, num, body_verts8, cpvzero); shape->e = 0; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = driver_Body; cpSpaceAddShape(space, shape); driver_head->p.x = driver_Head->getPosition().x; driver_head->p.y = driver_Head->getPosition().y; cpSpaceAddBody(space, driver_head); /* */ mass = TRUCK_LEFT_WHEELMASS; //left wheel cpBody * l_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero)); shape = cpCircleShapeNew(l_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero); shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = lWheelSprite; cpSpaceAddShape(space, shape); l_wheel->p.x = lWheelSprite->getPosition().x; l_wheel->p.y = lWheelSprite->getPosition().y; cpSpaceAddBody(space, l_wheel); mass = TRUCK_RIGHT_WHEELMASS; //right wheel cpBody * r_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero)); shape = cpCircleShapeNew(r_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero); shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = rWheelSprite; cpSpaceAddShape(space, shape); r_wheel->p.x = rWheelSprite->getPosition().x; r_wheel->p.y = rWheelSprite->getPosition().y; cpSpaceAddBody(space, r_wheel); cpVect l_wheel_offset = cpv(-80 * doodleTruck->getScaleY(), -70 * doodleTruck->getScaleY()); cpVect r_wheel_offset = cpv(80 * doodleTruck->getScaleY(), -70 * doodleTruck->getScaleY()); cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero)); cpVect pt_reverse = cpv(1.0, 0); cpSpaceAddConstraint(space, cpDampedSpringNew(body, l_wheel, cpv(l_wheel_offset.x * pt_reverse.x, l_wheel_offset.y * pt_reverse.y), cpvzero, fabs(l_wheel_offset.y), TRUCK_LEFT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING)); cpSpaceAddConstraint(space, cpDampedSpringNew(body, r_wheel, cpv(r_wheel_offset.x * pt_reverse.x, r_wheel_offset.y * pt_reverse.y), cpvzero, fabs(r_wheel_offset.y), TRUCK_RIGHT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING)); //cpVect l_wheel_offset = cpv(-80 * doodleTruck->getScaleY(), -60 * doodleTruck->getScaleY()); // cpVect r_wheel_offset = cpv(80 * doodleTruck->getScaleY(), -60 * doodleTruck->getScaleY()); // cpSpaceAddConstraint(space, cpGrooveJointNew(driver_body, driver_head, cpv(-18, 31), cpv(1.5, -25), cpvzero)); cpSpaceAddConstraint(space, cpSlideJointNew(driver_body, driver_head, cpv(-17.7, 30.7), cpv(1.2, -13.3), 0.3, 1)); // cpSpaceAddConstraint(space, cpGrooveJointNew(body, driver_body, cpv(0, -5), cpv(0, 35), cpvzero)); //cpSpaceAddConstraint(space, cpGrooveJointNew(body, driver_body, cpv(10, 5), cpv(15, 5), cpvzero)); /* cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero)); */ /* cpSpaceAddConstraint(space, cpDampedSpringNew(driver_body, driver_head, cpv(0, -5), cpv(0, 30), fabs(10), 2000, 3000)); cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body, driver_body, -0.2, 0.3)); cpSpaceAddConstraint(space, cpRotaryLimitJointNew(driver_body, driver_head, -0.2, 0.3)); */ cpConstraint * motor = cpSimpleMotorNew(body, l_wheel, 0); doodleTruck->setMotor((cpSimpleMotor *)motor); cpSpaceAddConstraint(doodleTruck->getSpace(), motor); doodleTruck->setBody(body); doodleTruck->setLeftW (l_wheel); doodleTruck->setRightW(r_wheel); }
void Truck::setupChipmunk3() { DoodleTruck * doodleTruck = DoodleTruck::sharedDoodleTruck(); cpSpace *space = doodleTruck->getSpace(); cpVect s_p = cpv(doodleTruck->getScaleY(), doodleTruck->getScaleY()); double mass = TRUCK_MASS; //body cpVect wd_ht_half = cpv(248, 84); cpBody *body = cpBodyNew(mass, cpMomentForBox(mass, wd_ht_half.x * 2 * doodleTruck->getScaleX(), wd_ht_half.y * 2 * doodleTruck->getScaleY())); int num = 4; cpVect body_verts1[] = { cpv((286 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 5) * s_p.y), cpv((430 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 5) * s_p.y), cpv((430 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y), cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y) }; cpShape *shape = cpPolyShapeNew(body, num, body_verts1, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; shape->data = bodySprite; cpSpaceAddShape(space, shape); num = 5; cpVect body_verts2[] = { cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y), cpv((490 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 70) * s_p.y), cpv((490 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 165) * s_p.y), cpv((455 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 165) * s_p.y), cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 110) * s_p.y) }; shape = cpPolyShapeNew(body, num, body_verts2, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; cpSpaceAddShape(space, shape); num = 4; cpVect body_verts3[] = { cpv((255 - wd_ht_half.x) * s_p.x , (wd_ht_half.y - 85) * s_p.y), cpv((255 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 130) * s_p.y), cpv((70 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 130) * s_p.y), cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y) }; shape = cpPolyShapeNew(body, num, body_verts3, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; cpSpaceAddShape(space, shape); cpVect body_verts4[] = { cpv((0 - wd_ht_half.x) * s_p.x , (wd_ht_half.y - 48) * s_p.y), cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 48) * s_p.y), cpv((40 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y), cpv((20 - wd_ht_half.x) * s_p.x, (wd_ht_half.y - 85) * s_p.y) }; shape = cpPolyShapeNew(body, num, body_verts4, cpvzero); shape->e = TRUCK_BODY_E; shape->u = TRUCK_BODY_U; shape->group = BOX2D_TRUCK_GROUP; shape->collision_type = BODY_COLLISION; cpSpaceAddShape(space, shape); body->p = cpv(bodySprite->getPosition().x, bodySprite->getPosition().y); cpSpaceAddBody(space, body); mass = TRUCK_LEFT_WHEELMASS; //left wheel cpBody * l_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero)); shape = cpCircleShapeNew(l_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero); shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U; shape->group = BOX2D_TRUCK_GROUP; shape->data = lWheelSprite; cpSpaceAddShape(space, shape); l_wheel->p = cpv(lWheelSprite->getPosition().x, lWheelSprite->getPosition().y); cpSpaceAddBody(space, l_wheel); mass = TRUCK_RIGHT_WHEELMASS; //right wheel cpBody * r_wheel = cpBodyNew(mass, cpMomentForCircle(mass, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero)); shape = cpCircleShapeNew(r_wheel, TRUCK_WHEEL_RADIUS * doodleTruck->getScaleY(), cpvzero); shape->e = TRUCK_WHEEL_E; shape->u = TRUCK_WHEEL_U; shape->group = BOX2D_TRUCK_GROUP; shape->data = rWheelSprite; cpSpaceAddShape(space, shape); r_wheel->p = cpv(rWheelSprite->getPosition().x, rWheelSprite->getPosition().y); cpSpaceAddBody(space, r_wheel); cpVect l_wheel_offset = cpv(-100 * doodleTruck->getScaleY(), -50 * doodleTruck->getScaleY()); cpVect r_wheel_offset = cpv(130 * doodleTruck->getScaleY(), -50 * doodleTruck->getScaleY()); cpSpaceAddConstraint(space, cpGrooveJointNew(body, l_wheel, l_wheel_offset, cpv(l_wheel_offset.x, 0), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(body, r_wheel, r_wheel_offset, cpv(r_wheel_offset.x, 0), cpvzero)); cpVect pt_reverse = cpv(1, 0); cpSpaceAddConstraint(space, cpDampedSpringNew(body, l_wheel, cpv(l_wheel_offset.x * pt_reverse.x, l_wheel_offset.y * pt_reverse.y), cpvzero, fabs(l_wheel_offset.y), TRUCK_LEFT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING)); cpSpaceAddConstraint(space, cpDampedSpringNew(body, r_wheel, cpv(r_wheel_offset.x * pt_reverse.x, r_wheel_offset.y * pt_reverse.y), cpvzero, fabs(r_wheel_offset.y), TRUCK_RIGHT_SPRING_STIFFNESS, TRUCK_SPRING_DAMPING)); cpConstraint * motor = cpSimpleMotorNew(body, l_wheel, 0); doodleTruck->setMotor((cpSimpleMotor *)motor); cpSpaceAddConstraint(doodleTruck->getSpace(), motor); doodleTruck->setBody(body); doodleTruck->setLeftW(l_wheel); doodleTruck->setRightW(r_wheel); }
cpJoint * bmx_cpgroovejoint_create(BBObject * handle, cpBody * bodyA, cpBody * bodyB, cpVect * grooveA, cpVect * grooveB, cpVect * anchor) { cpJoint * joint = cpGrooveJointNew(bodyA, bodyB, *grooveA, *grooveB, *anchor); cpbind(joint, handle); return joint; }
static cpSpace * init(void) { space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -100)); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect boxOffset; cpBody *body1, *body2; cpVect posA = cpv( 50, 60); cpVect posB = cpv(110, 60); #define POS_A cpvadd(boxOffset, posA) #define POS_B cpvadd(boxOffset, posB) // Pin Joints - Link shapes with a solid bar or pin. // Keeps the anchor points the same distance apart from when the joint was created. boxOffset = cpv(-320, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPinJointNew(body1, body2, cpv(15,0), cpv(-15,0))); // Slide Joints - Like pin joints but with a min/max distance. // Can be used for a cheap approximation of a rope. boxOffset = cpv(-160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpSlideJointNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 40.0f)); // Pivot Joints - Holds the two anchor points together. Like a swivel. boxOffset = cpv(0, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPivotJointNew(body1, body2, cpvadd(boxOffset, cpv(80,60)))); // cpPivotJointNew() takes it's anchor parameter in world coordinates. The anchors are calculated from that // cpPivotJointNew2() lets you specify the two anchor points explicitly // Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in boxOffset = cpv(160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(body1, body2, cpv(30,30), cpv(30,-30), cpv(-30,0))); // Damped Springs boxOffset = cpv(-320, -120); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpDampedSpringNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 5.0f, 0.3f)); // Damped Rotary Springs boxOffset = cpv(-160, -120); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); cpSpaceAddConstraint(space, cpDampedRotarySpringNew(body1, body2, 0.0f, 3000.0f, 60.0f)); // Rotary Limit Joint boxOffset = cpv(0, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Hold their rotation within 90 degrees of each other. cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, -M_PI_2, M_PI_2)); // Ratchet Joint - A rotary ratchet, like a socket wrench boxOffset = cpv(160, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Ratchet every 90 degrees cpSpaceAddConstraint(space, cpRatchetJointNew(body1, body2, 0.0f, M_PI_2)); // Gear Joint - Maintain a specific angular velocity ratio boxOffset = cpv(-320, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Force one to sping 2x as fast as the other cpSpaceAddConstraint(space, cpGearJointNew(body1, body2, 0.0f, 2.0f)); // Simple Motor - Maintain a specific angular relative velocity boxOffset = cpv(-160, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Make them spin at 1/2 revolution per second in relation to each other. cpSpaceAddConstraint(space, cpSimpleMotorNew(body1, body2, M_PI)); // Make a car with some nice soft suspension boxOffset = cpv(0, 0); cpBody *wheel1 = addWheel(posA, boxOffset); cpBody *wheel2 = addWheel(posB, boxOffset); cpBody *chassis = addChassis(cpv(80, 100), boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel1, cpv(-30, -10), cpv(-30, -40), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel2, cpv( 30, -10), cpv( 30, -40), cpvzero)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel1, cpv(-30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel2, cpv( 30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); return space; }