Exemplo n.º 1
0
cpConstraint *cpSpaceSerializer::createSpringJoint(TiXmlElement *elm)
{
	cpConstraint *constraint;
	
	cpVect anchr1 = createPoint("anchr1", elm);
	cpVect anchr2 = createPoint("anchr2", elm);
	
	cpBody *a;
	cpBody *b;
	createBodies(elm, &a, &b);
	
	cpFloat restLen = createValue<cpFloat>("restLength", elm);
	cpFloat stiffness = createValue<cpFloat>("stiffness", elm);
	cpFloat damping = createValue<cpFloat>("damping", elm);
	
	constraint = cpDampedSpringNew(a, b, anchr1, anchr2, restLen, stiffness, damping);
	
	return constraint;
}
Exemplo n.º 2
0
bool PhysicsJointSpring::createConstraints()
{
    do {
        auto joint = cpDampedSpringNew(_bodyA->getCPBody(),
            _bodyB->getCPBody(),
            PhysicsHelper::point2cpv(_anchr1),
            PhysicsHelper::point2cpv(_anchr2),
            PhysicsHelper::float2cpfloat(_bodyB->local2World(_anchr1).getDistance(_bodyA->local2World(_anchr2))),
            PhysicsHelper::float2cpfloat(_stiffness),
            PhysicsHelper::float2cpfloat(_damping));

        CC_BREAK_IF(joint == nullptr);
        _cpConstraints.push_back(joint);

        return true;
    } while (false);

    return false;
}
Exemplo n.º 3
0
bool PhysicsJointSpring::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
{
    do {
        CC_BREAK_IF(!PhysicsJoint::init(a, b));
        
        auto constraint = cpDampedSpringNew(a->getCPBody(),
                                                b->getCPBody(),
                                                PhysicsHelper::point2cpv(anchr1),
                                                PhysicsHelper::point2cpv(anchr2),
                                                PhysicsHelper::float2cpfloat(_bodyB->local2World(anchr1).getDistance(_bodyA->local2World(anchr2))),
                                                PhysicsHelper::float2cpfloat(stiffness),
                                                PhysicsHelper::float2cpfloat(damping));
        
        CC_BREAK_IF(constraint == nullptr);
        
        _cpConstraints.push_back(constraint);
        
        return true;
    } while (false);
    
    return false;
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
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);
}
Exemplo n.º 6
0
DampedSpring::DampedSpring(cp::Body *a,cp::Body *b,const cp::Vect& anchr1,const cp::Vect& anchr2,cpFloat restLength,cpFloat stiffness,cpFloat damping)
	: Constraint(cpDampedSpringNew(a ? a->get() : 0,b ? b->get() : 0,anchr1,anchr2,restLength,stiffness,damping))
{
		constraint->data = this;
}
Exemplo n.º 7
0
void Spring::setup(cpSpace *space, cpBody *a, cpBody *b, glm::vec2 anchorA, glm::vec2 anchorB, float distance, float stiffness, float damping){
	Constraint::setup(space, cpDampedSpringNew(a, b, toChipmunk(anchorA), toChipmunk(anchorB), distance, stiffness, damping));
}
Exemplo n.º 8
0
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;
}