示例#1
0
        static public cpSpace BouncyTerrainCircles_500(cpSpace space)
        {
            //cpSpace space = BENCH_SPACE_NEW();
            SetSubTitle("BouncyTerrainCircles 500");
            space.SetIterations(10);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++)
            {
                cpVect  a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1];
                cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
                shape.SetElasticity(1.0f);
            }

            for (int i = 0; i < 500; i++)
            {
                float  radius = 5.0f;
                float  mass   = radius * radius;
                cpBody body   = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero));
                body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f));

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

            return(space);
        }
示例#2
0
        static public cpSpace ComplexTerrainCircles_1000(cpSpace space)
        {
            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));
            space.SetCollisionSlop(0.5f);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (complex_terrain_verts.Length - 1); i++)
            {
                cpVect a = complex_terrain_verts[i], b = complex_terrain_verts[i + 1];
                space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
            }

            for (int i = 0; i < 1000; i++)
            {
                float  radius = 5.0f;
                float  mass   = radius * radius;
                cpBody body   = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f), new cpVect(0.0f, 300.0f)));
                cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));
                shape.SetElasticity(0.0f); shape.SetFriction(0.0f);
            }

            return(space);
        }
示例#3
0
 public void AddShape(CCPhysicsShapeInfo shape)
 {
     foreach (var item in shape.GetShapes())
     {
         _space.AddShape(item);
     }
 }
示例#4
0
        public void addFloor()
        {
            var floor = space.AddShape(
                new cpSegmentShape(space.GetStaticBody(), new cpVect(-Width * .5f, -Height * .5f), new cpVect(Width * .5f, -Height * .5f),
                                   0f));

            floor.SetElasticity(1);
            floor.SetFriction(1);
            floor.SetFilter(NOT_GRABBABLE_FILTER);
        }
示例#5
0
    static void Main(string[] args)
    {
        cpSpace space = new cpSpace();

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

        cpShape shape = cpShape.NewSegment(space.StaticBody, new cpVect(-20, 5), new cpVect(20, -5), 0);

        shape.SetFriction(1);

        space.AddShape(shape);

        double radius = 5;
        double mass   = 1;
        double moment = cpUtil.MomentForCircle(mass, 0, radius, cpVect.Zero);

        cpBody ballBody = new cpBody(mass, moment);

        space.AddBody(ballBody);
        ballBody.Position = new cpVect(0, 15);

        cpShape ballShape = cpShape.NewCircle(ballBody, radius, cpVect.Zero);

        space.AddShape(ballShape);
        ballShape.SetFriction(0.7);

        double timeStep = 1 / 60d;

        for (double time = 0; time < 2; time += timeStep)
        {
            cpVect pos = ballBody.Position;
            cpVect vel = ballBody.Velocity;
            Console.WriteLine("Time is {0:F2}. ballBody is at ({1:F2}, {2:F2}). It's velocity is ({3:F2}, {4:F2})",
                              time, pos.X, pos.Y, vel.X, vel.Y
                              );
            space.Step(timeStep);
        }

        Console.ReadKey();
    }
示例#6
0
        static public void add_circle(cpSpace space, int index, float radius)
        {
            float  mass = radius * radius / 25.0f;
            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));

            //	cpBody body = cpSpaceAddBody(space, cpBodyInit(&bodies[i], mass, cpMomentForCircle(mass, 0.0f, radius, cpVect.Zero)));
            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));

            cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero));

            //	cpShape shape = cpSpaceAddShape(space, cpCircleShapeInit(&circles[i], body, radius, cpVect.Zero));
            shape.SetElasticity(0.0f); shape.SetFriction(0.9f);
        }
示例#7
0
        public void ClipPoly(cpSpace space, cpShape shp, cpVect n, float dist)
        {
            cpPolyShape shape = (cpPolyShape)shp;

            cpBody body = shape.GetBody();


            int count        = shape.Count;
            int clippedCount = 0;

            cpVect[] clipped = new cpVect[count + 1];

            for (int i = 0, j = count - 1; i < count; j = i, i++)
            {
                cpVect a      = body.LocalToWorld(shape.GetVert(j));
                float  a_dist = cpVect.cpvdot(a, n) - dist;

                if (a_dist < 0)
                {
                    clipped[clippedCount] = a;
                    clippedCount++;
                }

                cpVect b      = body.LocalToWorld(shape.GetVert(i));
                float  b_dist = cpVect.cpvdot(b, n) - dist;

                if (a_dist * b_dist < 0)
                {
                    float t = cp.cpfabs(a_dist) / (cp.cpfabs(a_dist) + cp.cpfabs(b_dist));

                    clipped[clippedCount] = cpVect.cpvlerp(a, b, t);
                    clippedCount++;
                }
            }

            cpVect centroid = cp.CentroidForPoly(clippedCount, clipped);
            float  mass     = cp.AreaForPoly(clippedCount, clipped, 0) * DENSITY;
            float  moment   = cp.MomentForPoly(mass, clippedCount, clipped, cpVect.cpvneg(centroid), 0);

            cpBody new_body = space.AddBody(new cpBody(mass, moment));

            new_body.SetPosition(centroid);
            new_body.SetVelocity(body.GetVelocityAtWorldPoint(centroid));
            new_body.SetAngularVelocity(body.GetAngularVelocity());

            cpTransform transform = cpTransform.Translate(cpVect.cpvneg(centroid));
            cpShape     new_shape = space.AddShape(new cpPolyShape(new_body, clippedCount, clipped, transform, 0));

            // Copy whatever properties you have set on the original shape that are important
            new_shape.SetFriction(shape.GetFriction());
        }
示例#8
0
        static public void add_box(cpSpace space, int index, float size)
        {
            float mass = size * size / 100.0f;

            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, size, size)));

            //	cpBody body = cpSpaceAddBody(space, cpBodyInit(&bodies[i], mass, cpMomentForBox(mass, size, size)));
            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));
            cpPolyShape shape = space.AddShape(cpPolyShape.BoxShape(body, size - bevel * 2f, size - bevel * 2f, 0f)) as cpPolyShape;

            shape.SetRadius(bevel);
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);
        }
示例#9
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;
            }
        }
示例#10
0
        static public cpSpace BouncyTerrainHexagons_500(cpSpace space)
        {
            SetSubTitle("BouncyTerrainHexagons 500");
            //cpSpace space = BENCH_SPACE_NEW();
            space.SetIterations(10);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++)
            {
                cpVect  a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1];
                cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
                shape.SetElasticity(1.0f);
            }

            float radius = 5.0f;

            cpVect[] hexagon = new cpVect[6];
            for (int i = 0; i < 6; i++)
            {
                float angle = -(float)Math.PI * 2.0f * i / 6.0f;
                hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel);
            }

            for (int i = 0; i < 500; i++)
            {
                float  mass = radius * radius;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero));
                body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f));

                cpShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel));
                shape.SetElasticity(1.0f);
            }

            return(space);
        }
示例#11
0
        static public cpSpace ComplexTerrainHexagons_1000(cpSpace space)
        {
            SetSubTitle("ComplexTerrainHexagons_1000");
            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));
            space.SetCollisionSlop(0.5f);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (complex_terrain_verts.Length - 1); i++)
            {
                cpVect a = complex_terrain_verts[i], b = complex_terrain_verts[i + 1];
                space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
            }

            float radius = 5.0f;

            cpVect[] hexagon = new cpVect[6];
            for (int i = 0; i < 6; i++)
            {
                float angle = -(float)Math.PI * 2.0f * i / 6.0f;
                hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel);
            }

            for (int i = 0; i < 1000; i++)
            {
                float  mass = radius * radius;
                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f)));
                body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f), new cpVect(0.0f, 300.0f)));

                cpShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel));
                shape.SetElasticity(0.0f); shape.SetFriction(0.0f);
            }

            return(space);
        }
示例#12
0
        public cpBody add_ball(cpSpace space, cpVect pos)
        {
            cpBody body = space.AddBody(new cpBody(1.0f, cp.MomentForCircle(1.0f, 30, 0, cpVect.Zero)));

            body.SetPosition(pos);

            cpShape shape = space.AddShape(new cpCircleShape(body, 30, cpVect.Zero));


            shape.SetElasticity(0.0f);
            shape.SetFriction(0.5f);


            return(body);
        }
示例#13
0
        private void AddShapes()
        {
            lock (ShapesListLock)
            {
                foreach (var shape in _shapesToAdd)
                {
                    _space.AddShape(shape);
                    if (!_space.ContainsBody(shape.body))
                    {
                        _space.AddBody(shape.body);
                    }
                }

                _shapesToAdd.Clear();
            }
        }
示例#14
0
        static public cpSpace SetupSpace_simpleTerrain(cpSpace space)
        {
            SetSubTitle("SetupSpace_simpleTerrain");
            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -100));
            space.SetCollisionSlop(0.5f);

            cpVect offset = new cpVect(-320, -240);

            for (int i = 0; i < (simple_terrain_verts.Length - 1); i++)
            {
                cpVect a = simple_terrain_verts[i], b = simple_terrain_verts[i + 1];
                space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f));
            }
            return(space);
        }
示例#15
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);
        }
示例#16
0
        static public void add_hexagon(cpSpace space, int index, float radius)
        {
            cpVect[] hexagon = new cpVect[6];
            for (int i = 0; i < 6; i++)
            {
                float angle = -(float)Math.PI * 2.0f * i / 6.0f;
                hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel);
            }

            float mass = radius * radius;

            cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f)));

            body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f));

            cpPolyShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel)) as cpPolyShape;

            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);
        }
示例#17
0
        public PhysicObject(UIImage firstText, int radius, cpSpace space, bool isKinematic = false) : base(firstText)
        {
            trsf = new cpTransform();
            collCount++;
            physic = new cpBody(rnd.Next(500, 1000), cp.PHYSICS_INFINITY);

            if (isKinematic)
            {
                physic.SetBodyType(cpBodyType.KINEMATIC);
            }
            shp = new cpCircleShape(physic, radius, cpVect.Zero);
            shp.Active();
            shp.SetSensor(true);
            shp.SetCollisionType(1);
            physic.SetPosition(new cpVect((float)Frame.Location.X, (float)Frame.Location.Y));
            if (space != null)
            {
                space.AddBody(physic);
                space.AddShape(shp);
                this.space = space;
            }
        }