/*
 * Chipmunk2d::SlideJoint#initialize(a, b, anchor_a, anchor_b, min, max)
 * @param [Chipmunk2d::Body] a
 * @param [Chipmunk2d::Body] b
 * @param [Chipmunk2d::Vect] anchor_a
 * @param [Chipmunk2d::Vect] anchor_b
 * @param [Float] min
 * @param [Float] max
 */
static mrb_value
slide_joint_initialize(mrb_state* mrb, mrb_value self)
{
  cpConstraint* constraint;
  cpBody* a;
  cpBody* b;
  cpVect* anchor_a;
  cpVect* anchor_b;
  mrb_value a_obj;
  mrb_value b_obj;
  mrb_float min;
  mrb_float max;
  mrb_get_args(mrb, "ooddff",
               &a_obj,
               &b_obj,
               &anchor_a, &mrb_cp_vect_type,
               &anchor_b, &mrb_cp_vect_type,
               &min,
               &max);
  a = mrb_cp_get_body_ptr(mrb, a_obj);
  b = mrb_cp_get_body_ptr(mrb, b_obj);
  mrb_cp_constraint_cleanup(mrb, self);
  constraint = cpSlideJointNew(a, b, *anchor_a, *anchor_b, (cpFloat)min, (cpFloat)max);
  mrb_cp_constraint_init_bind(mrb, self, constraint);
  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "body_a"), a_obj);
  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "body_b"), b_obj);
  return self;
}
CCSlideJoint::CCSlideJoint(CCPhysicsWorld *world,
	CCPhysicsBody *bodyA, CCPhysicsBody *bodyB, cpVect archA, cpVect archB,
	cpFloat min, cpFloat max) : CCJoint(world, bodyA, bodyB, SLIDE_JOINT)
{
	this->m_constraint = cpSpaceAddConstraint(world->getSpace(),
		cpSlideJointNew(bodyA->getBody(), bodyB->getBody(), archA, archB, min, max));
}
Example #3
0
 SlideJoint::SlideJoint(std::shared_ptr<Body> bodyA,
                        std::shared_ptr<Body> bodyB,
                        cpVect anchorA,
                        cpVect anchorB,
                        cpFloat min,
                        cpFloat max) :
 Constraint(cpSlideJointNew(*bodyA, *bodyB, anchorA, anchorB, min, max),
            bodyA, bodyB)
 {}
Example #4
0
SGPhysicsConstraint* SG_CALL sgPhysicsConstraintCreateSlide(SGPhysicsBody* body1, SGPhysicsBody* body2, float x1, float y1, float x2, float y2, float min, float max)
{
    SGPhysicsConstraint* constr = sgPhysicsConstraintCreate(body1, body2, SG_CONSTRAINT_SLIDE);
    if(!constr) return NULL;

    constr->handle = cpSlideJointNew(body1->handle, body2->handle, cpv(x1, y1), cpv(x2, y2), min, max);
    _postCreate(constr);

    return constr;
}
Example #5
0
WorldConstraint_t *worldConstr_createSlideJoint(WorldEntity_t *a, WorldEntity_t *b, vec2_t aAnchorA, vec2_t aAnchorB,
        GLMFloat aMinDist, GLMFloat aMaxDist)
{
    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_Slide;
    ret->cpConstraint = cpSlideJointNew(a->cpBody, b->cpBody, VEC2_TO_CPV(aAnchorA), VEC2_TO_CPV(aAnchorB), aMinDist, aMaxDist);
    cpSpaceAddConstraint(ret->world->cpSpace, ret->cpConstraint);
    return ret;
}
Example #6
0
bool PhysicsJointLimit::createConstraints()
{
    do
    {
        auto joint = cpSlideJointNew(_bodyA->getCPBody(), _bodyB->getCPBody(),
            PhysicsHelper::point2cpv(_anchr1),
            PhysicsHelper::point2cpv(_anchr2),
            PhysicsHelper::float2cpfloat(_min),
            PhysicsHelper::float2cpfloat(_max));

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

        return true;
    } while (false);

    return false;
}
cpConstraint *cpSpaceSerializer::createSlideJoint(TiXmlElement *elm)
{
	cpConstraint *constraint;
	
	cpVect anchr1 = createPoint("anchr1", elm);
	cpVect anchr2 = createPoint("anchr2", elm);
	
	cpBody *a;
	cpBody *b;
	createBodies(elm, &a, &b);
	
	cpFloat min = createValue<cpFloat>("min", elm);
	cpFloat max = createValue<cpFloat>("max", elm);
	
	constraint = cpSlideJointNew(a, b, anchr1, anchr2, min, max);
	
	//((cpSlideJoint*)constraint)->jnAcc = createValue<cpFloat>("jnAcc", elm);
	
	return constraint;
}
bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max)
{
    do
    {
        CC_BREAK_IF(!PhysicsJoint::init(a, b));
        
        auto constraint = cpSlideJointNew(a->getCPBody(), b->getCPBody(),
                                       PhysicsHelper::point2cpv(anchr1),
                                       PhysicsHelper::point2cpv(anchr2),
                                       PhysicsHelper::float2cpfloat(min),
                                       PhysicsHelper::float2cpfloat(max));
        
        CC_BREAK_IF(constraint == nullptr);
        
        _cpConstraints.push_back(constraint);
        
        return true;
    } while (false);
    
    return false;
}
bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
{
    do
    {
        CC_BREAK_IF(!PhysicsJoint::init(a, b));
        
        cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
                                       PhysicsHelper::point2cpv(anchr1),
                                       PhysicsHelper::point2cpv(anchr2),
                                       0,
                                       PhysicsHelper::float2cpfloat(m_pBodyA->local2World(anchr1).getDistance(m_pBodyB->local2World(anchr2))));
        
        CC_BREAK_IF(joint == nullptr);
        
        m_pInfo->add(joint);
        
        return true;
    } while (false);
    
    return false;
}
Example #10
0
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;
}
Example #11
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);
}
Example #12
0
cpJoint * bmx_cpslidejoint_create(BBObject * handle, cpBody * bodyA, cpBody * bodyB, cpVect * anchor1, cpVect * anchor2, cpFloat minDist, cpFloat maxDist) {
	cpJoint * joint = cpSlideJointNew(bodyA, bodyB, *anchor1, *anchor2, minDist, maxDist);
	cpbind(joint, handle);
	return joint;
}
Example #13
0
cpSpace *Chains::Init()
{
    ChipmunkDemo::Init();

    space = cpSpaceNew();
    cpSpaceSetIterations(space, 30);
    cpSpaceSetGravity(space, cpv(0, -100));
    cpSpaceSetSleepTimeThreshold(space, 0.5f);

    cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
    cpShape *shape;

    // 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);

    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 mass = 1;
    cpFloat width = 20;
    cpFloat height = 30;

    cpFloat spacing = width*0.3;

    // Add lots of boxes.
    for(int i=0; i<CHAIN_COUNT; i++){
        cpBody *prev = NULL;

        for(int j=0; j<LINK_COUNT; j++){
            cpVect pos = cpv(40*(i - (CHAIN_COUNT - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing);

            body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height)));
            cpBodySetPosition(body, pos);

            shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(0, (height - width)/2.0), cpv(0, (width - height)/2.0), width/2.0));
            cpShapeSetFriction(shape, 0.8f);

            cpFloat breakingForce = 80000;

            cpConstraint *constraint = NULL;
            if(prev == NULL){
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, staticBody, cpv(0, height/2), cpv(pos.x, 240), 0, spacing));
            } else {
                constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, prev, cpv(0, height/2), cpv(0, -height/2), 0, spacing));
            }

            cpConstraintSetMaxForce(constraint, breakingForce);
            cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve);
            cpConstraintSetCollideBodies(constraint, cpFalse);

            prev = body;
        }
    }

    cpFloat radius = 15.0f;
    body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
    cpBodySetPosition(body, cpv(0, -240 + radius+5));
    cpBodySetVelocity(body, cpv(0, 300));

    shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
    cpShapeSetElasticity(shape, 0.0f);
    cpShapeSetFriction(shape, 0.9f);
	
	return space;
}
Example #14
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;
}