Example #1
0
 public cpPivotJoint(cpBody a, cpBody b, cpVect pivot)
     : this(a, b,
            (a != null ? a.WorldToLocal(pivot) : pivot),
            (b != null ? b.WorldToLocal(pivot) : pivot))
 {
 }
Example #2
0
        public override void OnEnter()
        {
            base.OnEnter();

            SetSubTitle("This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it.");

            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -500));

            {
                cpShape shape      = null;
                cpBody  staticBody = space.GetStaticBody();

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-3200, -240), new cpVect(3200, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(0, -200), new cpVect(240, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-240, -240), new cpVect(0, -200), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);
            }


            {
                float radius = 20.0f;
                float mass   = 1.0f;

                float moment = cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero);

                wheel_body = space.AddBody(new cpBody(mass, moment));
                wheel_body.SetPosition(new cpVect(0.0f, -160.0f + radius));

                cpShape shape = space.AddShape(new cpCircleShape(wheel_body, radius, cpVect.Zero));
                shape.SetFriction(0.7f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            {
                float cog_offset = 30.0f;

                cpBB bb1 = new cpBB(-5.0f, 0.0f - cog_offset, 5.0f, cog_offset * 1.2f - cog_offset);
                cpBB bb2 = new cpBB(-25.0f, bb1.t, 25.0f, bb1.t + 10.0f);

                float mass   = 3.0f;
                float moment = cp.MomentForBox2(mass, bb1) + cp.MomentForBox2(mass, bb2);

                balance_body = space.AddBody(new cpBody(mass, moment));
                balance_body.SetPosition(new cpVect(0.0f, wheel_body.GetPosition().y + cog_offset));

                cpShape shape = null;

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb1, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb2, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            cpVect anchorA  = balance_body.WorldToLocal(wheel_body.GetPosition());
            cpVect groove_a = cpVect.cpvadd(anchorA, new cpVect(0.0f, 30.0f));
            cpVect groove_b = cpVect.cpvadd(anchorA, new cpVect(0.0f, -10.0f));

            space.AddConstraint(new cpGrooveJoint(balance_body, wheel_body, groove_a, groove_b, cpVect.Zero));
            space.AddConstraint(new cpDampedSpring(balance_body, wheel_body, anchorA, cpVect.Zero, 0.0f, 6.0e2f, 30.0f));

            motor = space.AddConstraint(new cpSimpleMotor(wheel_body, balance_body, 0.0f));
            motor.SetPreSolveFunc((s) => motor_preSolve(motor, s));

            {
                float width  = 100.0f;
                float height = 20.0f;
                float mass   = 3.0f;

                cpBody boxBody = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height)));
                boxBody.SetPosition(new cpVect(200, -100));

                cpShape shape = space.AddShape(cpPolyShape.BoxShape(boxBody, width, height, 0.0f));
                shape.SetFriction(0.7f);
            }


            Schedule();
        }
Example #3
0
        public override void OnEnter()
        {
            base.OnEnter();

            SetSubTitle("Use the arrow keys to control the machine.");


            space.SetGravity(new cpVect(0, -600));

            cpBody  staticBody = space.GetStaticBody();
            cpShape shape;

            // beveling all of the line segments slightly helps prevent things from getting stuck on cracks
            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-256, 16), new cpVect(-256, 300), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-256, 16), new cpVect(-192, 0), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-192, 0), new cpVect(-192, -64), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-128, -64), new cpVect(-128, 144), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-192, 80), new cpVect(-192, 176), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-192, 176), new cpVect(-128, 240), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-128, 144), new cpVect(192, 64), 2.0f));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            cpVect[] verts = new cpVect[] {
                new cpVect(-30, -80),
                new cpVect(-30, 80),
                new cpVect(30, 64),
                new cpVect(30, -80),
            };

            cpBody plunger = space.AddBody(new cpBody(1.0f, cp.Infinity));

            plunger.SetPosition(new cpVect(-160, -80));

            shape = space.AddShape(new cpPolyShape(plunger, 4, verts, cpTransform.Identity, 0));
            shape.SetElasticity(1.0f);
            shape.SetFriction(0.5f);
            shape.SetFilter(new cpShapeFilter(cp.NO_GROUP, 1, 1));
            balls = new cpBody[numBalls];
            // add balls to hopper
            for (int i = 0; i < numBalls; i++)
            {
                balls[i] = add_ball(space, new cpVect(-224 + i, 80 + 64 * i));
            }

            // add small gear
            cpBody smallGear = space.AddBody(new cpBody(10.0f, cp.MomentForCircle(10.0f, 80, 0, cpVect.Zero)));

            smallGear.SetPosition(new cpVect(-160, -160));
            smallGear.SetAngle(-cp.M_PI_2);

            shape = space.AddShape(new cpCircleShape(smallGear, 80.0f, cpVect.Zero));
            shape.SetFilter(cpShape.FILTER_NONE);

            space.AddConstraint(new cpPivotJoint(staticBody, smallGear, new cpVect(-160, -160), cpVect.Zero));

            // add big gear
            cpBody bigGear = space.AddBody(new cpBody(40.0f, cp.MomentForCircle(40.0f, 160, 0, cpVect.Zero)));

            bigGear.SetPosition(new cpVect(80, -160));
            bigGear.SetAngle(cp.M_PI_2);

            shape = space.AddShape(new cpCircleShape(bigGear, 160.0f, cpVect.Zero));
            shape.SetFilter(cpShape.FILTER_NONE);

            space.AddConstraint(new cpPivotJoint(staticBody, bigGear, new cpVect(80, -160), cpVect.Zero));

            // connect the plunger to the small gear.
            space.AddConstraint(new cpPinJoint(smallGear, plunger, new cpVect(80, 0), new cpVect(0, 0)));
            // connect the gears.
            space.AddConstraint(new cpGearJoint(smallGear, bigGear, -cp.M_PI_2, -2.0f));


            // feeder mechanism
            float  bottom = -300.0f;
            float  top    = 32.0f;
            cpBody feeder = space.AddBody(new cpBody(1.0f, cp.MomentForSegment(1.0f, new cpVect(-224.0f, bottom), new cpVect(-224.0f, top), 0.0f)));

            feeder.SetPosition(new cpVect(-224, (bottom + top) / 2.0f));

            float len = top - bottom;

            shape = space.AddShape(new cpSegmentShape(feeder, new cpVect(0.0f, len / 2.0f), new cpVect(0.0f, -len / 2.0f), 20.0f));
            shape.SetFilter(GRAB_FILTER);

            space.AddConstraint(new cpPivotJoint(staticBody, feeder, new cpVect(-224.0f, bottom), new cpVect(0.0f, -len / 2.0f)));
            cpVect anchr = feeder.WorldToLocal(new cpVect(-224.0f, -160.0f));

            space.AddConstraint(new cpPinJoint(feeder, smallGear, anchr, new cpVect(0.0f, 80.0f)));

            // motorize the second gear
            motor = space.AddConstraint(new cpSimpleMotor(staticBody, bigGear, 3.0f));


            Schedule();
        }