Example #1
0
 public void DrawConstraint(cpConstraint constraint)
 {
     System.Type klass = constraint.GetType();
     if (klass == typeof(cpPinJoint))
     {
         Draw((cpPinJoint)constraint);
     }
     else if (klass == typeof(cpSlideJoint))
     {
         Draw((cpSlideJoint)constraint);
     }
     else if (klass == typeof(cpGrooveJoint))
     {
         Draw((cpGrooveJoint)constraint);
     }
     else if (klass == typeof(cpDampedSpring))
     {
         Draw((cpDampedSpring)constraint);
     }
     else if (klass == typeof(cpDampedRotarySpring))
     {
         Draw((cpDampedRotarySpring)constraint);
     }
     else if (klass == typeof(cpSimpleMotor))
     {
         Draw((cpSimpleMotor)constraint);
     }
 }
Example #2
0
        public void PostStepRemoveJoint(object key, object data)
        {
            //	printf("Removing joint for %p\n", data);
            cpConstraint joint = (cpConstraint)key;

            space.RemoveConstraint(joint);
        }
Example #3
0
        void motor_preSolve(cpConstraint motor, cpSpace space)
        {
            float dt = space.GetCurrentTimeStep();

            float target_x = CCMouse.Instance.Position.x;

            paint = new shape
            {
                point1 = new cpVect(target_x, -1000.0f),
                point2 = new cpVect(target_x, 1000.0f),
            };

            float max_v      = 500.0f;
            float target_v   = cp.cpfclamp(cp.bias_coef(0.5f, dt / 1.2f) * (target_x - balance_body.GetPosition().x) / dt, -max_v, max_v);
            float error_v    = (target_v - balance_body.GetVelocity().x);
            float target_sin = 3.0e-3f * cp.bias_coef(0.1f, dt) * error_v / dt;

            float max_sin = cp.cpfsin(0.6f);

            balance_sin = cp.cpfclamp(balance_sin - 6.0e-5f * cp.bias_coef(0.2f, dt) * error_v / dt, -max_sin, max_sin);
            float target_a     = (float)Math.Asin(cp.cpfclamp(-target_sin + balance_sin, -max_sin, max_sin));
            float angular_diff = (float)Math.Asin(cpVect.cpvcross(balance_body.GetRotation(), cpVect.cpvforangle(target_a)));
            float target_w     = cp.bias_coef(0.1f, dt / 0.4f) * (angular_diff) / dt;

            float max_rate = 50.0f;
            float rate     = cp.cpfclamp(wheel_body.GetAngularVelocity() + balance_body.GetAngularVelocity() - target_w, -max_rate, max_rate);

            motor.SetRate(cp.cpfclamp(rate, -max_rate, max_rate));
            motor.SetMaxForce(8.0e4f);
        }
Example #4
0
        public void PostStepAddJoint(object key, object data)
        {
            //	printf("Adding joint for %p\n", data);

            cpConstraint joint = (cpConstraint)key;

            space.AddConstraint(joint);
        }
 public void Add(cpConstraint joint)
 {
     if (joint == null)
     {
         return;
     }
     _joints.Add(joint);
     _map.Add(joint, this);
 }
Example #6
0
        void BreakableJointPostSolve(cpConstraint joint)
        {
            float dt = space.GetCurrentTimeStep();

            // Convert the impulse to a force by dividing it by the timestep.
            float force    = joint.GetImpulse() / dt;
            float maxForce = joint.GetMaxForce();// maxForce;

            // If the force is almost as big as the joint's max force, break it.
            if (force > 0.9 * maxForce)
            {
                space.AddPostStepCallback(
                    (s, o1, o2) => BreakablejointPostStepRemove(o1 as cpConstraint),
                    joint, null
                    );
            }
        }
Example #7
0
        public override void Update(float dt)
        {
            base.Update(dt);
            if (CCMouse.Instance.HasPosition)
            {
                // Set the first anchor point (the one attached to the static body) of the dolly servo to the mouse's x position.
                dollyServo.SetAnchorA(new cpVect(CCMouse.Instance.Position.x, 100));
                // Set the max length of the winch servo to match the mouse's height.
                winchServo.SetMax(cp.cpfmax(100 - CCMouse.Instance.Position.y, 50));

                if (hookJoint != null && CCMouse.Instance.rightclick)
                {
                    space.RemoveConstraint(hookJoint);
                    //cpConstraintFree(hookJoint);
                    hookJoint = null;
                }
            }
            space.Step(dt);
        }
        public void Remove(cpConstraint joint)
        {
            if (joint == null)
            {
                return;
            }

            var it = _joints.Find((c) => c == joint);

            if (it != null)
            {
                _joints.Remove(it);

                CCPhysicsJointInfo tmp;
                if (_map.TryGetValue(joint, out tmp))
                {
                    _map.Remove(joint);
                }
            }
        }
Example #9
0
        public void StickySeparate(cpArbiter arb, object data)
        {
            cpConstraint joint = (cpConstraint)arb.GetUserData();

            if (joint != null)
            {
                // The joint won't be removed until the step is done.
                // Need to disable it so that it won't apply itself.
                // Setting the force to 0 will do just that

                joint.SetMaxForce(0.0f);

                space.AddPostStepCallback((s1, o1, o2) =>
                                          PostStepRemoveJoint(o1, o2), joint, null

                                          );
                // Perform the removal in a post-step() callback.

                // NULL out the reference to the joint.
                // Not required, but it's a good practice.
                arb.SetUserData(null);
            }
        }
Example #10
0
        public override void OnEnter()
        {
            base.OnEnter();
            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -100));
            space.SetSleepTimeThreshold(0.5f);

            cpBody  body, 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.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

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

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

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

            float mass    = 1;
            float width   = 20;
            float height  = 30;
            float spacing = width * 0.3f;

            // 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 = new cpVect(40 * (i - (CHAIN_COUNT - 1) / 2.0f), 240 - (j + 0.5f) * height - (j + 1) * spacing);
                    body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height)));
                    body.SetPosition(pos);

                    shape = space.AddShape(new cpSegmentShape(body, new cpVect(0, (height - width) / 2.0f), new cpVect(0, (width - height) / 2.0f), width / 2.0f));
                    shape.SetFriction(0.8f);

                    float breakingForce = 80000;

                    cpConstraint constraint = null;
                    if (prev == null)
                    {
                        constraint = space.AddConstraint(new cpSlideJoint(body, staticBody, new cpVect(0, height / 2), new cpVect(pos.x, 240), 0, spacing));
                    }
                    else
                    {
                        constraint = space.AddConstraint(new cpSlideJoint(body, prev, new cpVect(0, height / 2), new cpVect(0, -height / 2), 0, spacing));
                    }

                    constraint.SetMaxForce(breakingForce);
                    constraint.SetPostSolveFunc(s => BreakableJointPostSolve(constraint));
                    constraint.SetCollideBodies(false);

                    //cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve);
                    prev = body;
                }
            }

            float radius = 15.0f;

            body = space.AddBody(new cpBody(10.0f, cp.MomentForCircle(10.0f, 0.0f, radius, cpVect.Zero)));
            body.SetPosition(new cpVect(0, -240 + radius + 5));
            body.SetVelocity(new cpVect(0, 300));

            shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);;

            Schedule();
        }
Example #11
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();
        }
Example #12
0
        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();
        }
Example #13
0
        public override void OnEnter()
        {
            base.OnEnter();


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

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

            cpBody  staticBody = space.GetStaticBody();
            cpShape shape;
            cpVect  a, b;

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

            // Create segments around the edge of the screen.
            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);

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

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

            float offset = 30.0f;

            // make chassis
            float chassis_mass = 2.0f;

            a = new cpVect(-offset, 0.0f);
            b = new cpVect(offset, 0.0f);

            cpBody chassis = space.AddBody(new cpBody(chassis_mass, cp.MomentForSegment(chassis_mass, a, b, 0.0f)));

            shape = space.AddShape(new cpSegmentShape(chassis, a, b, seg_radius));
            shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            // make crank
            float  crank_mass   = 1.0f;
            float  crank_radius = 13.0f;
            cpBody crank        = space.AddBody(new cpBody(crank_mass, cp.MomentForCircle(crank_mass, crank_radius, 0.0f, cpVect.Zero)));

            shape = space.AddShape(new cpCircleShape(crank, crank_radius, cpVect.Zero));
            shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

            space.AddConstraint(new cpPivotJoint(chassis, crank, cpVect.Zero, cpVect.Zero));

            float side = 30.0f;

            int num_legs = 2;

            for (int i = 0; i < num_legs; i++)
            {
                make_leg(side, offset, chassis, crank, cpVect.cpvmult(cpVect.cpvforangle((float)(2 * i + 0) / (float)num_legs * ((float)Math.PI)), crank_radius));
                make_leg(side, -offset, chassis, crank, cpVect.cpvmult(cpVect.cpvforangle((float)(2 * i + 1) / (float)num_legs * ((float)Math.PI)), crank_radius));
            }

            motor = space.AddConstraint(new cpSimpleMotor(chassis, crank, 6.0f));

            Schedule();
        }
Example #14
0
cpBodyRemoveConstraint(cpBody body, cpConstraint constraint)
{
	body.constraintList = filterConstraints(body.constraintList, body, constraint);
}
Example #15
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 #16
0
 void BreakablejointPostStepRemove(cpConstraint joint)
 {
     space.RemoveConstraint(joint);
 }
Example #17
0
        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();
        }
Example #18
0
 void AttachHook(cpBody hook, cpBody crate)
 {
     hookJoint = space.AddConstraint(new cpPivotJoint(hook, crate, hook.GetPosition()));
 }
Example #19
0
getImpulse(cpConstraint joint)
{
	return System.Math.Abs(((cpSlideJoint *)joint).jnAcc);
}
Example #20
0
filterConstraints(cpConstraint node, cpBody body, cpConstraint filter)
{
	if(node == filter){
		return cpConstraintNext(node, body);
	} else if(node.a == body){
		node.next_a = filterConstraints(node.next_a, body, filter);
	} else {
		node.next_b = filterConstraints(node.next_b, body, filter);
	}
	
	return node;
}