void NaiveRotationAlgorithm::stepSystem(SystemInfo *individual) { MachineSystem *oldSystem = individual->system; individual->system = new MachineSystem(*oldSystem); // copy it to stop all the damn bouncing about delete oldSystem; cpBody *inputBody = individual->system->partAtPosition(individual->system->inputMachinePosition)->body; cpBody *outputBody = individual->system->partAtPosition(individual->system->outputMachinePosition)->body; cpSpace *systemSpace = individual->system->getSpace(); cpConstraint *motor = cpSimpleMotorNew(cpSpaceGetStaticBody(systemSpace), inputBody, M_PI); cpSpaceAddConstraint(systemSpace, motor); cpConstraintSetMaxForce(motor, 50000); for (int i=0; i<simSteps; i++) { individual->inputValues[i] = (cpBodyGetAngle(inputBody)); cpSpaceStep(systemSpace, 0.1); individual->outputValues[i] = (cpBodyGetAngle(outputBody)); } cpSpaceRemoveConstraint(systemSpace, motor); cpBodySetAngVel(outputBody, 0); cpBodySetAngle(inputBody, 0); }
SGPhysicsConstraint* SG_CALL sgPhysicsConstraintCreateMotor(SGPhysicsBody* body1, SGPhysicsBody* body2, float rate) { SGPhysicsConstraint* constr = sgPhysicsConstraintCreate(body1, body2, SG_CONSTRAINT_MOTOR); if(!constr) return NULL; constr->handle = cpSimpleMotorNew(body1->handle, body2->handle, rate); _postCreate(constr); return constr; }
WorldConstraint_t *worldConstr_createSimpleMotorJoint(WorldEntity_t *a, WorldEntity_t *b, GLMFloat aRate) { 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_SimpleMotor; ret->cpConstraint = cpSimpleMotorNew(a->cpBody, b->cpBody, aRate); cpSpaceAddConstraint(ret->world->cpSpace, ret->cpConstraint); return ret; }
cpConstraint *cpSpaceSerializer::createMotorJoint(TiXmlElement *elm) { cpConstraint *constraint; cpBody *a; cpBody *b; createBodies(elm, &a, &b); cpFloat rate = createValue<cpFloat>("rate", elm); constraint = cpSimpleMotorNew(a, b, rate); //((cpSimpleMotor*)constraint)->jMax = createValue<cpFloat>("jMax", elm); return constraint; }
bool PhysicsJointMotor::createConstraints() { do { auto joint = cpSimpleMotorNew(_bodyA->getCPBody(), _bodyB->getCPBody(), PhysicsHelper::float2cpfloat(_rate)); CC_BREAK_IF(joint == nullptr); _cpConstraints.push_back(joint); return true; } while (false); return false; }
bool PhysicsJointMotor::init(PhysicsBody* a, PhysicsBody* b, float rate) { do { CC_BREAK_IF(!PhysicsJoint::init(a, b)); auto constraint = cpSimpleMotorNew(a->getCPBody(), b->getCPBody(), PhysicsHelper::float2cpfloat(rate)); CC_BREAK_IF(constraint == nullptr); _cpConstraints.push_back(constraint); return true; } while (false); return false; }
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); }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Use the arrow keys to control the machine."; space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, -600)); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // beveling all of the line segments slightly helps prevent things from getting stuck on cracks shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); cpBodySetPos(plunger, cpv(-160,-80)); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, 1); // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); cpBodySetPos(smallGear, cpv(-160,-160)); cpBodySetAngle(smallGear, -M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); cpBodySetPos(bigGear, cpv(80,-160)); cpBodySetAngle(bigGear, M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, -M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); cpBodySetPos(feeder, cpv(-224, (bottom + top)/2.0f)); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }
static cpSpace * init(void) { space = cpSpaceNew(); space->gravity = cpv(0, -600); cpBody *staticBody = &space->staticBody; cpShape *shape; // beveling all of the line segments slightly helps prevent things from getting stuck on cracks shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); shape->e = 0.0f; shape->u = 0.0f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); plunger->p = cpv(-160,-80); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); shape->e = 1.0f; shape->u = 0.5f; shape->layers = 1; // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); smallGear->p = cpv(-160,-160); cpBodySetAngle(smallGear, (cpFloat)-M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); bigGear->p = cpv(80,-160); cpBodySetAngle(bigGear, (cpFloat)M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, (cpFloat)-M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); feeder->p = cpv(-224, (bottom + top)/2.0f); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Use the arrow keys to control the machine."; cpSpace *space = cpSpaceNew(); cpSpaceSetIterations(space, 20); cpSpaceSetGravity(space, cpv(0,-500)); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; cpVect a, b; // Create segments around the edge of the screen. shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); cpFloat offset = 30.0f; // make chassis cpFloat chassis_mass = 2.0f; a = cpv(-offset, 0.0f), b = cpv(offset, 0.0f); cpBody *chassis = cpSpaceAddBody(space, cpBodyNew(chassis_mass, cpMomentForSegment(chassis_mass, a, b, 0.0f))); shape = cpSpaceAddShape(space, cpSegmentShapeNew(chassis, a, b, seg_radius)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); // make crank cpFloat crank_mass = 1.0f; cpFloat crank_radius = 13.0f; cpBody *crank = cpSpaceAddBody(space, cpBodyNew(crank_mass, cpMomentForCircle(crank_mass, crank_radius, 0.0f, cpvzero))); shape = cpSpaceAddShape(space, cpCircleShapeNew(crank, crank_radius, cpvzero)); cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, crank, cpvzero, cpvzero)); cpFloat side = 30.0f; int num_legs = 2; for(int i=0; i<num_legs; i++){ make_leg(space, side, offset, chassis, crank, cpvmult(cpvforangle((cpFloat)(2*i+0)/(cpFloat)num_legs*M_PI), crank_radius)); make_leg(space, side, -offset, chassis, crank, cpvmult(cpvforangle((cpFloat)(2*i+1)/(cpFloat)num_legs*M_PI), crank_radius)); } motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(chassis, crank, 6.0f)); return space; }
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; }