Example #1
0
        public void ShatterCell(cpPolyShape shape, cpVect cell, int cell_i, int cell_j, ref WorleyContex context)
        {
            //	printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y);

            cpBody body = shape.body;                             // cpShapeGetBody(shape);

            cpVect[] ping = new cpVect[MAX_VERTEXES_PER_VORONOI]; // cpVect[ (cpVect*)alloca( * sizeof(cpVect));
            cpVect[] pong = new cpVect[MAX_VERTEXES_PER_VORONOI]; //(cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect));

            int count = shape.Count;                              // cpPolyShapeGetCount();

            count = (count > MAX_VERTEXES_PER_VORONOI ? MAX_VERTEXES_PER_VORONOI : count);

            for (int i = 0; i < count; i++)
            {
                ping[i] = body.LocalToWorld(shape.GetVert(i));
            }

            cpPointQueryInfo info = null;

            for (int i = 0; i < context.width; i++)
            {
                for (int j = 0; j < context.height; j++)
                {
                    if (
                        !(i == cell_i && j == cell_j) &&
                        shape.PointQuery(cell, ref info) < 0
                        )
                    {
                        count = ClipCell(shape, cell, i, j, context, ping, pong, count);

                        for (int u = 0; u < pong.Length; u++)
                        {
                            if (pong[u] != null)
                            {
                                ping[u] = new cpVect(pong[u]);
                            }
                        }
                    }
                }
            }

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

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

            new_body.SetPosition(centroid);

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

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

            // Copy whatever properties you have set on the original shape that are important
            new_shape.SetFriction(shape.GetFriction());
        }
Example #2
0
 public void UpdateBodyPosition()
 {
     if (mouseBody != null && Position != null)
     {
         mouseBody.SetPosition(Position);
     }
 }
Example #3
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);
        }
Example #4
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);
        }
        public CCPhysicsSprite addGrossiniAtPosition(CCPoint location)
        {
            int posx, posy;

            posx = CCRandom.Next() * 200;
            posy = CCRandom.Next() * 200;

            posx = (Math.Abs(posx) % 4) * 85;
            posy = (Math.Abs(posy) % 3) * 121;

            CCPhysicsSprite sp = new CCPhysicsSprite(spriteTexture.Texture, new CCRect(posx, posy, 85, 121));

            cpBB verts = new cpBB(-24, -54, 24, 54);

            var body = new cpBody(1, cp.MomentForBox2(1, new cpBB(-24, -54, 24, 54)));             //);

            body.SetPosition(new cpVect(posx, posy));
            space.AddBody(body);

            var shape = cpPolyShape.BoxShape2(body, verts, 0);

            shape.e = .5f;
            shape.u = .5f;
            space.AddShape(shape);

            sp.Body = body;

            AddChild(sp);
            sp.Position = location;


            return(sp);
        }
        public override void OnEnter()
        {
            base.OnEnter();
            space.SetIterations(30);
            space.SetGravity(new cpVect(0f, -300f));
            space.SetCollisionSlop(0.5f);
            space.SetSleepTimeThreshold(1.0f);

            cpBody body, staticBody = space.GetStaticBody();

            // Create segments around the edge of the screen.
            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320f, -240f), new cpVect(-320f, 240f), 0.0f));

            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

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

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

            //RAMP
            scaleStaticBody = space.AddBody(cpBody.NewStatic());
            shape           = space.AddShape(new cpSegmentShape(scaleStaticBody, new cpVect(-240, -180), new cpVect(-140, -180), 4.0f));

            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            if (!isTest)
            {
                // add some boxes to stack on the scale
                for (int i = 0; i < 5; i++)
                {
                    body = space.AddBody(new cpBody(1.0f, cp.MomentForBox(1.0f, 30.0f, 30.0f)));
                    body.SetPosition(new cpVect(0f, i * 32f - 220f));

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

            //Add a ball that we'll track which objects are beneath it.
            float radius = 15.0f;

            ballBody = space.AddBody(new cpBody(10.0f, cp.MomentForCircle(10.0f, 0.0f, radius, cpVect.Zero)));
            ballBody.SetPosition(new cpVect(120, -240 + radius + 5));

            shape = space.AddShape(new cpCircleShape(ballBody, radius, cpVect.Zero));
            shape.SetElasticity(0.0f);
            shape.SetFriction(0.9f);
            Schedule();
        }
Example #7
0
        public override void OnEnter()
        {
            base.OnEnter();


            SetSubTitle("Sticky collisions using the cpArbiter data pointer.");

            space.SetIterations(10);
            space.SetGravity(new cpVect(0, -1000));
            space.SetCollisionSlop(2.0f);

            cpBody  staticBody = space.GetStaticBody();
            cpShape shape;

            // Create segments around the edge of the screen.
            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-340, -260), new cpVect(-340, 260), 20.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(340, -260), new cpVect(340, 260), 20.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-340, -260), new cpVect(340, -260), 20.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-340, 260), new cpVect(340, 260), 20.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            for (int i = 0; i < 80; i++)
            {
                float mass   = 0.15f;
                float radius = 10.0f;

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero)));

                body.SetPosition(
                    new cpVect(cp.cpflerp(-150.0f, 150.0f, RandomHelper.frand()),
                               cp.cpflerp(-150.0f, 150.0f, RandomHelper.frand()))
                    );

                shape = space.AddShape(new cpCircleShape(body, radius + STICK_SENSOR_THICKNESS, cpVect.Zero));
                shape.SetFriction(0.9f);
                shape.SetCollisionType(COLLISION_TYPE_STICKY);
            }

            cpCollisionHandler handler = space.AddWildcardHandler(COLLISION_TYPE_STICKY);

            handler.preSolveFunc = (a, s, o) => StickyPreSolve(a, null);
            handler.separateFunc = (a, s, o) => StickySeparate(a, null);

            Schedule();
        }
Example #8
0
        void eachBody(cpBody body)
        {
            cpVect pos = body.GetPosition();

            if (pos.y < -400 || cp.cpfabs(pos.x) > 340)
            {
                body.SetPosition(new cpVect(
                                     RandomHelper.next(-300, 300),
                                     RandomHelper.next(350, 900)));
            }
        }
Example #9
0
        public void make_leg(float side, float offset, cpBody chassis, cpBody crank, cpVect anchor)
        {
            cpVect  a, b;
            cpShape shape;

            float leg_mass = 1.0f;

            // make leg
            a = cpVect.Zero;
            b = new cpVect(0.0f, side);

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

            upper_leg.SetPosition(new cpVect(offset, 0.0f));

            shape = space.AddShape(new cpSegmentShape(upper_leg, a, b, seg_radius));
            shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

            space.AddConstraint(new cpPivotJoint(chassis, upper_leg, new cpVect(offset, 0.0f), cpVect.Zero));


            //shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

            // lower leg
            a = cpVect.Zero;
            b = new cpVect(0.0f, -1.0f * side);

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

            lower_leg.SetPosition(new cpVect(offset, -side));

            shape = space.AddShape(new cpSegmentShape(lower_leg, a, b, seg_radius));
            shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            shape = space.AddShape(new cpCircleShape(lower_leg, seg_radius * 2.0f, b));
            shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

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

            space.AddConstraint(new cpPinJoint(chassis, lower_leg, new cpVect(offset, 0.0f), cpVect.Zero));

            space.AddConstraint(new cpGearJoint(upper_leg, lower_leg, 0.0f, 1.0f));

            cpPinJoint constraint;
            float      diag = cp.cpfsqrt(side * side + offset * offset);

            constraint = space.AddConstraint(new cpPinJoint(crank, upper_leg, anchor, new cpVect(0.0f, side))) as cpPinJoint;

            constraint.SetDist(diag);

            constraint = space.AddConstraint(new cpPinJoint(crank, lower_leg, anchor, cpVect.Zero)) as cpPinJoint;
            constraint.SetDist(diag);
        }
Example #10
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);
        }
Example #11
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());
        }
Example #12
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);
        }
Example #13
0
        public cpBall make_ball(float x, float y)
        {
            var body = new cpBody(1, cp.Infinity);

            body.SetPosition(new cpVect(x, y));

            var shape = new cpBall(body, 0.95f, cpVect.Zero);

            //var shape = new cpBall(body, 35, cpVect.Zero);
            shape.SetElasticity(0);
            shape.SetFriction(0);

            return(shape);
        }
Example #14
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);
        }
Example #15
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);
        }
    //public cpBodyType BodyType { get { return mRigidbody2D.bodyType; } set { mBodyType = value; mRigidbody2D.SetBodyType(mBodyType); } }

    //public float Mass { get { return mRigidbody2D.GetMass(); } set { mMass = value; mRigidbody2D.SetMass(mMass); } }
    public void Init()
    {
        mRigidbody2D = new cpBody(mMass, mMonent);
        mRigidbody2D.SetBodyType(mBodyType);
        mRigidbody2D.SetPositionUpdateFunc(UpdatePosition);//Set Update Poition callback

        cpVect pos = new cpVect(this.transform.position.x * XSpaceManager.PixelsPerUnit, this.transform.position.y * XSpaceManager.PixelsPerUnit);

        mRigidbody2D.SetPosition(pos);
        mRigidbody2D.SetAngle(cp.cpfradian(this.transform.eulerAngles.z));
        // mRigidbody2D.SetTransform(pos, cp.cpfradian(this.transform.eulerAngles.z)); //设置这个不对,position不更新

        //X.Tools.LogUtils.Debug(X.Tools.LogTag.Test, this.gameObject.name + ":" + mRigidbody2D.GetPosition().ToString());
        //this.transform.SetPositionAndRotation(Vector3.zero, Quaternion.Euler(0, 0, 0));
        //X.Tools.LogUtils.Debug(string.Format("rotation:{0}, eulerAngles:{1}, {2} ,{3}", this.transform.rotation, this.transform.eulerAngles, this.transform.localRotation, this.transform.localEulerAngles));
    }
Example #17
0
        cpBody add_box(float size, float mass)
        {
            float radius = cpVect.cpvlength(new cpVect(size, size));

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

            body.SetPosition(new cpVect((float)CCRandom.Float_0_1() * (640 - 2 * radius) - (320 - radius), (float)CCRandom.Float_0_1() * (480 - 2 * radius) - (240 - radius)));


            cpShape shape = space.AddShape(cpPolyShape.BoxShape(body, size, size, 0));

            shape.SetElasticity(0);
            shape.SetFriction(0.7f);

            return(body);
        }
Example #18
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);
        }
Example #19
0
        void add_box()
        {
            const float size = 10;
            const float mass = 1;

            cpVect[] verts = new cpVect[] {
                new cpVect(-size, -size),
                new cpVect(-size, size),
                new cpVect(size, size),
                new cpVect(size, -size)
            };


            float  radius = cpVect.cpvlength(new cpVect(size, size));
            cpVect pos    = rand_pos(radius);

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

            body.SetVelocityUpdateFunc(
                (s, f1, f2) => planetGravityVelocityFunc(body, s, f1, f2)
                );

            body.SetPosition(pos);

            // Set the box's velocity to put it into a circular orbit from its
            // starting position.
            float r = cpVect.cpvlength(pos);
            float v = cp.cpfsqrt(gravityStrength / r) / r;

            body.SetVelocity(cpVect.cpvmult(cpVect.cpvperp(pos), v));

            // Set the box's angular velocity to match its orbital period and
            // align its initial angle with its position.
            body.SetAngularVelocity(v);
            body.SetAngle(cp.cpfatan2(pos.y, pos.x));

            cpShape shape = space.AddShape(new cpPolyShape(body, 4, verts, cpTransform.Identity, 0.0f));             //cpTransformIdentity

            shape.SetElasticity(0);
            shape.SetFriction(0.7f);
        }
Example #20
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;
            }
        }
Example #21
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);
        }
Example #22
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);
        }
Example #23
0
        public override void Update(float dt)
        {
            base.Update(dt);

            float coef = (2.0f + ChipmunkDemoKeyboard.y) / 3.0f;
            float rate = ChipmunkDemoKeyboard.x * 30.0f * coef;

            motor.SetRate(rate);
            motor.SetMaxForce(rate > 0 ? 1000000.0f : 0.0f);

            space.Step(dt);

            for (int i = 0; i < numBalls; i++)
            {
                cpBody ball = balls[i];
                cpVect pos  = ball.GetPosition();

                if (pos.x > 320.0f)
                {
                    ball.SetVelocity(cpVect.Zero);
                    ball.SetPosition(new cpVect(-224.0f, 200.0f));
                }
            }
        }
Example #24
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();
        }
        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();
        }