Exemplo n.º 1
0
 public Game1(cpSpace space)
 {
     spc = space;
     spc.SetGravity(new cpVect(0, 1f));
     spc.AddDefaultCollisionHandler().preSolveFunc = new Func <cpArbiter, cpSpace, object, bool> ((arg1, arg2, arg3) => { Console.WriteLine("ij"); return(true); });
     spc.AddDefaultCollisionHandler().beginFunc    = new Func <cpArbiter, cpSpace, object, bool> ((arg1, arg2, arg3) => { Console.WriteLine("ij"); return(true); });
 }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a copy of the registered world to run physics simulations in
        /// </summary>
        /// <param name="world">The world to create a copy of</param>
        public static void RegisterWorld(World world)
        {
            if (Worlds.ContainsKey(world))
            {
                throw new System.InvalidOperationException("Tried to register physics world when one is already registered!");
            }
            else
            {
                //Create the space
                cpSpace space = new cpSpace();
                space.SetGravity(new cpVect(0, 10));

                Worlds.Add(world, space);
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 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();
    }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
 public void SetGravity(cpVect gravity)
 {
     _space.SetGravity(gravity);
 }