Example #1
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);
        }