Beispiel #1
0
handlerSetTrans(cpCollisionHandler *handler, object unused)
{
	cpCollisionHandler *copy = (cpCollisionHandler *)cpcalloc(1, sizeof(cpCollisionHandler));
	(*copy) = (*handler);
	
	return copy;
}
Beispiel #2
0
handlerSetEql(cpCollisionHandler *check, cpCollisionHandler *pair)
{
	return ((check.a == pair.a && check.b == pair.b) || (check.b == pair.a && check.a == pair.b));
}
Beispiel #3
0
        public override void OnEnter()
        {
            base.OnEnter();

            platformInstance = new OneWayPlatform();

            SetSubTitle("One way platforms are trivial in Chipmunk using a very simple collision callback.");

            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));

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

            // Add our one way segment
            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-160, -100), new cpVect(160, -100), 10.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);

            shape.SetCollisionType(COLLISION_TYPE_ONE_WAY);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            // We'll use the data pointer for the OneWayPlatform struct
            platformInstance.n = new cpVect(0, 1);             // let objects pass upwards
            shape.userData     = platformInstance;


            // Add a ball to test it out
            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, -200));
            body.SetVelocity(new cpVect(0, 170));

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

            cpCollisionHandler handler = space.AddWildcardHandler(COLLISION_TYPE_ONE_WAY);

            handler.preSolveFunc = preSolve;


            Schedule();
        }
        public override void OnEnter()
        {
            base.OnEnter();
            space.SetIterations(5);
            space.SetDamping(0.1f);

            //space.defaultHandler.
            cpCollisionHandler handler = space.AddWildcardHandler(COLLISION_TYPE_ONE_WAY);

            handler.preSolveFunc = NeverCollide;
            //space.def SetDefaultCollisionHandler(space, NeverCollide, NULL, NULL, NULL, NULL);

            {
                float  mass = 1.0f;
                float  length = 100.0f;
                cpVect a = new cpVect(-length / 2.0f, 0.0f), b = new cpVect(length / 2.0f, 0.0f);

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForSegment(mass, a, b, 0.0f)));
                body.SetPosition(new cpVect(-160.0f, -80.0f));

                space.AddShape(new cpSegmentShape(body, a, b, 30.0f));
            }
            {
                float  mass = 1.0f;
                float  length = 100.0f;
                cpVect a = new cpVect(-length / 2.0f, 0.0f), b = new cpVect(length / 2.0f, 0.0f);
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForSegment(mass, a, b, 0.0f)));
                body.SetPosition(new cpVect(-160.0f, 80.0f));
                space.AddShape(new cpSegmentShape(body, a, b, 20.0f));
            }
            {
                float    mass      = 1.0f;
                int      NUM_VERTS = 5;
                cpVect[] verts     = new cpVect[NUM_VERTS];
                for (int i = 0; i < NUM_VERTS; i++)
                {
                    float angle = -2 * cp.M_PI * i / NUM_VERTS;
                    verts[i] = new cpVect(40 * cp.cpfcos(angle), 40 * cp.cpfsin(angle));
                }

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, NUM_VERTS, verts, cpVect.Zero, 0.0f)));
                body.SetPosition(new cpVect(-0.0f, -80.0f));
                space.AddShape(new cpPolyShape(body, NUM_VERTS, verts, 0.0f));
            }

            {
                float mass      = 1.0f;
                int   NUM_VERTS = 4;

                cpVect[] verts = new cpVect[NUM_VERTS];
                for (int i = 0; i < NUM_VERTS; i++)
                {
                    float angle = -2 * cp.M_PI * i / NUM_VERTS;
                    verts[i] = new cpVect(60 * cp.cpfcos(angle), 60 * cp.cpfsin(angle));
                }

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, NUM_VERTS, verts, cpVect.Zero, 0.0f)));
                body.SetPosition(new cpVect(-0.0f, 80.0f));
                space.AddShape(new cpPolyShape(body, NUM_VERTS, verts, 0));
            }

            {
                float mass = 1.0f;
                float r    = 60.0f;

                cpBody body = space.AddBody(new cpBody(mass, cp.Infinity));
                body.SetPosition(new cpVect(160, -80));
                space.AddShape(new cpCircleShape(body, r, cpVect.Zero));
            }

            {
                float mass = 1.0f;
                float r    = 40.0f;

                cpBody body = space.AddBody(new cpBody(mass, cp.Infinity));
                body.SetPosition(new cpVect(160, 80));
                space.AddShape(new cpCircleShape(body, r, cpVect.Zero));
            }

            Schedule();
        }
Beispiel #5
0
        public override void OnEnter()
        {
            base.OnEnter();
            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -500));
            space.SetSleepTimeThreshold(0.5f);
            space.SetCollisionSlop(0.5f);
            var staticBody = space.GetStaticBody();

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

            {
                // Add the edges of the bucket
                var bb     = new cpBB(-300, -200, 100, 0);
                var radius = 5.0f;

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.l, bb.b), new cpVect(bb.l, bb.t), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.r, bb.b), new cpVect(bb.r, bb.t), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(bb.l, bb.b), new cpVect(bb.r, bb.b), radius));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                // Add the sensor for the water.
                shape = space.AddShape(cpPolyShape.BoxShape2(staticBody, bb, 0.0f));
                shape.SetSensor(true);
                shape.SetCollisionType(1);
            }
            {
                float width  = 200.0f;
                float height = 50.0f;
                float mass   = 0.3f * FLUID_DENSITY * width * height;
                var   moment = cp.MomentForBox(mass, width, height);

                cpBody body = space.AddBody(new cpBody(mass, moment));
                body.SetPosition(new cpVect(-50, -100));
                body.SetVelocity(new cpVect(0, -100));
                body.SetAngularVelocity(1);

                shape = space.AddShape(cpPolyShape.BoxShape(body, width, height, 0.0f));
                shape.SetFriction(0.8f);
            }

            {
                float width  = 40.0f;
                float height = width * 2;
                float mass   = 0.3f * FLUID_DENSITY * width * height;
                float moment = cp.MomentForBox(mass, width, height);

                cpBody body = space.AddBody(new cpBody(mass, moment));
                body.SetPosition(new cpVect(-200, -50));
                body.SetVelocity(new cpVect(0, -100));
                body.SetAngularVelocity(1);

                shape = space.AddShape(cpPolyShape.BoxShape(body, width, height, 0.0f));
                shape.SetFriction(0.8f);
            }

            cpCollisionHandler handler = space.AddCollisionHandler(1, 0);

            handler.preSolveFunc = waterPreSolve;
            Schedule();
        }