public override void OnEnter() { base.OnEnter(); SetSubTitle("Use the mouse to drive the tank, it will follow the cursor."); //Position = new CCPoint(240, 170); space.SetIterations(10); space.SetSleepTimeThreshold(0.5f); cpBody staticBody = space.GetStaticBody(); cpShape shape; // Create segments around the edge of the screen. shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(-320, 240), 0)); shape.SetElasticity(1); shape.SetFriction(1); shape.SetFilter(NOT_GRABBABLE_FILTER); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(320, -240), new cpVect(320, 240), 0)); shape.SetElasticity(1); shape.SetFriction(1); shape.SetFilter(NOT_GRABBABLE_FILTER); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(320, -240), 0)); shape.SetElasticity(1); shape.SetFriction(1); shape.SetFilter(NOT_GRABBABLE_FILTER); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, 240), new cpVect(320, 240), 0)); shape.SetElasticity(1); shape.SetFriction(1); shape.SetFilter(NOT_GRABBABLE_FILTER); for (int i = 0; i < 50; i++) { cpBody body = add_box(20, 1); cpConstraint pivot = space.AddConstraint(new cpPivotJoint(staticBody, body, cpVect.Zero, cpVect.Zero)); pivot.SetMaxBias(0); // disable joint correction pivot.SetMaxForce(1000); // emulate linear friction cpConstraint gear = space.AddConstraint(new cpGearJoint(staticBody, body, 0, 1)); gear.SetMaxBias(0); // disable joint correction gear.SetMaxForce(5000); // emulate linear friction } // We joint the tank to the control body and control the tank indirectly by modifying the control body. tankControlBody = space.AddBody(cpBody.NewKinematic()); tankBody = add_box(30, 10); cpConstraint pivot2 = space.AddConstraint(new cpPivotJoint(tankControlBody, tankBody, cpVect.Zero, cpVect.Zero)); pivot2.SetMaxBias(0); // disable joint correction pivot2.SetMaxForce(10000); // emulate linear friction cpConstraint gears = space.AddConstraint(new cpGearJoint(tankControlBody, tankBody, 0, 1)); gears.SetErrorBias(0); // attempt to fully correct the joint each step gears.SetMaxBias(1.2f); // but limit it's angular correction rate gears.SetMaxForce(5000); // emulate angular friction Schedule(); }
public override void OnEnter() { base.OnEnter(); SetSubTitle("Control the crane by moving the mouse. Right click to release."); space.SetIterations(30); space.SetGravity(new cpVect(0, -100)); space.SetDamping(0.8f); cpBody staticBody = space.GetStaticBody(); cpShape shape; shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(320, -240), 0.0f)); shape.SetElasticity(1.0f); shape.SetFriction(1.0f); shape.SetFilter(NOT_GRABBABLE_FILTER); // Add a body for the dolly. dollyBody = space.AddBody(new cpBody(10, cp.Infinity)); dollyBody.SetPosition(new cpVect(0, 100)); // Add a block so you can see it. space.AddShape(cpPolyShape.BoxShape(dollyBody, 30, 30, 0.0f)); // Add a groove joint for it to move back and forth on. space.AddConstraint(new cpGrooveJoint(staticBody, dollyBody, new cpVect(-250, 100), new cpVect(250, 100), cpVect.Zero)); // 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 = space.AddConstraint(new cpPivotJoint(staticBody, dollyBody, dollyBody.GetPosition())); // Max force the dolly servo can generate. dollyServo.SetMaxForce(10000); // Max speed of the dolly servo dollyServo.SetMaxBias(100); // You can also change the error bias to control how it slows down. dollyServo.SetErrorBias(0.2f); // Add the crane hook. cpBody hookBody = space.AddBody(new cpBody(1, cp.Infinity)); hookBody.SetPosition(new cpVect(0, 50)); // Add a sensor shape for it. This will be used to figure out when the hook touches a box. shape = space.AddShape(new cpCircleShape(hookBody, 10, cpVect.Zero)); shape.SetSensor(true);// cpTrue); shape.SetCollisionType(((int)COLLISION_TYPES.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 = space.AddConstraint(new cpSlideJoint(dollyBody, hookBody, cpVect.Zero, cpVect.Zero, 0, cp.Infinity)); // Max force the dolly servo can generate. winchServo.SetMaxForce(30000); // Max speed of the dolly servo winchServo.SetMaxBias(60); // TODO: cleanup // Finally a box to play with cpBody boxBody = space.AddBody(new cpBody(30, cp.MomentForBox(30, 50, 50))); boxBody.SetPosition(new cpVect(200, -200)); // Add a block so you can see it. shape = space.AddShape(cpPolyShape.BoxShape(boxBody, 50, 50, 0)); shape.SetFriction(0.7f); shape.SetCollisionType(((int)COLLISION_TYPES.CRATE)); var handler = space.AddCollisionHandler( (int)COLLISION_TYPES.HOOK_SENSOR, (int)COLLISION_TYPES.CRATE); handler.beginFunc = HookCrate; Schedule(); }