Beispiel #1
0
        static cpSpace NoCollide(cpSpace space)
        {
            space.SetIterations(10);

            var handler = space.AddCollisionHandler(2, 2);

            //, (a, s, o) => NoCollide_begin(a, s, o), null, null, null);
            handler.beginFunc = NoCollide_begin;
            float radius     = 4.5f;
            var   staticBody = space.GetStaticBody();

            space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f);
            space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f);

            for (int x = -320; x <= 320; x += 20)
            {
                for (int y = -240; y <= 240; y += 20)
                {
                    space.AddShape(new cpCircleShape(staticBody, radius, new cpVect(x, y)));
                }
            }

            for (int y = 10 - 240; y <= 240; y += 40)
            {
                float  mass = 7.0f;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(new cpVect(-320.0f, y));
                body.SetVelocity(new cpVect(100.0f, 0.0f));

                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(1.0f);
                shape.SetCollisionType(2);
            }

            for (int x = 30 - 320; x <= 320; x += 40)
            {
                float  mass = 7.0f;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));

                body.SetPosition(new cpVect(x, -240.0f));
                body.SetVelocity(new cpVect(0.0f, 100.0f));

                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(1.0f);
                shape.SetCollisionType(2);
            }
            return(space);
        }
Beispiel #2
0
        public void Start(int worldSize)
        {
            lock (UpdateLock)
            {
                _worldSize = worldSize;

                _space = new cpSpace {
                    CollisionEnabled = true
                };
                _space.SetGravity(cpVect.Zero);

                var boxBody = cpBody.NewStatic();

                var leftShape   = new cpSegmentShape(boxBody, new cpVect(-worldSize, -worldSize), new cpVect(-worldSize, worldSize), 5);
                var topShape    = new cpSegmentShape(boxBody, new cpVect(-worldSize, worldSize), new cpVect(worldSize, worldSize), 5);
                var rightShape  = new cpSegmentShape(boxBody, new cpVect(worldSize, worldSize), new cpVect(worldSize, -worldSize), 5);
                var bottomShape = new cpSegmentShape(boxBody, new cpVect(worldSize, -worldSize), new cpVect(-worldSize, -worldSize), 5);

                leftShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                topShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                rightShape.SetCollisionType(GenerateCollisionType <cpSpace>());
                bottomShape.SetCollisionType(GenerateCollisionType <cpSpace>());

                _space.AddShape(leftShape);
                _space.AddShape(topShape);
                _space.AddShape(rightShape);
                _space.AddShape(bottomShape);

                var defaultCollisionHandler = _space.AddDefaultCollisionHandler();
                defaultCollisionHandler.preSolveFunc = (arbiter, space, userData) => false;

                var playerCollisionHandler = _space.AddCollisionHandler(GenerateCollisionType <PlayerBlob>(), GenerateCollisionType <PlayerBlob>());
                playerCollisionHandler.preSolveFunc = OnPreSolve;

                _reindexStatic = true;
            }
        }