Пример #1
0
        public override void OnEnter()
        {
            base.OnEnter();


            space.SetIterations(5);
            space.SetDamping(0.1f);

            float mass = 1.0f;

            {
                float size = 100.0f;

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, size, size)));
                body.SetPosition(new cpVect(100.0f, 50.0f));

                shape1 = space.AddShape(cpPolyShape.BoxShape(body, size, size, 0.0f));
                //shape1.SetGroup(1);
            }
            {
                float size = 100.0f;

                cpBody body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, size, size)));
                body.SetPosition(new cpVect(120.0f, -40.0f));
                body.SetAngle(1e-2f);

                shape2 = space.AddShape(cpPolyShape.BoxShape(body, size, size, 0.0f));
                //shape2.SetGroup(1);
            }


            Schedule();
        }
    //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));
    }
Пример #3
0
        /// <summary>
        /// Create a physics body and a box shape and adds it to the world
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add it to</param>
        /// <param name="position">The position of the center of the box</param>
        /// <param name="rotation">The rotation of the box</param>
        /// <param name="size">The size of the box</param>
        /// <param name="bodyType">The body type of the physics body</param>
        /// <param name="mass">The mass of the physics body</param>
        /// <param name="moment">The moment of inertia for the physics body</param>
        /// <returns></returns>
        public static PhysicsObject CreateBox(World world, Vector2 position, float rotation, Vector2 size, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create body
            cpBody body = CreateBody(world, bodyType, mass, moment);

            body.SetPosition(new cpVect(position.X / PHYSICS_TRANSFORM_SCALE, position.Y / PHYSICS_TRANSFORM_SCALE));
            body.SetAngle(rotation);

            cpPolyShape box = cpPolyShape.BoxShape(body, size.X / PHYSICS_TRANSFORM_SCALE, size.Y / PHYSICS_TRANSFORM_SCALE, 0.1f);

            Worlds[world].AddShape(box);

            //Return the Physics body
            return(new PhysicsObject()
            {
                Body = body, Shape = box
            });
        }
Пример #4
0
        /// <summary>
        /// Create a physics body and a circle shape and adds it to the world
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add it to</param>
        /// <param name="position">The starting position for the circle</param>
        /// <param name="rotation">The starting rotation for the circle</param>
        /// <param name="radius">The radius of the circle</param>
        /// <param name="bodyType">The body type of the physics body</param>
        /// <param name="mass">The mass of the physics body</param>
        /// <param name="moment">The moment of inertia for the physics body</param>
        /// <returns></returns>
        public static PhysicsObject CreateCircle(World world, Vector2 position, float rotation, float radius, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create a body and set its transform
            cpBody body = CreateBody(world, bodyType, mass, moment);

            body.SetPosition(new cpVect(position.X / PHYSICS_TRANSFORM_SCALE, position.Y / PHYSICS_TRANSFORM_SCALE));
            body.SetAngle(rotation);

            //Create the circle shape and add it to the world
            cpCircleShape circleShape = new cpCircleShape(body, radius / PHYSICS_TRANSFORM_SCALE, cpVect.Zero);

            Worlds[world].AddShape(circleShape);

            //Return the phyics object
            return(new PhysicsObject()
            {
                Body = body, Shape = circleShape
            });
        }
Пример #5
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);
        }
Пример #6
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();
        }