示例#1
0
        public override void Update(float dt)
        {
            base.Update(dt);

            if (CCMouse.Instance.rightclick)
            {
                cpPointQueryInfo info    = null;
                cpShape          nearest = space.PointQueryNearest(CCMouse.Instance.Position, 0.0f, GRAB_FILTER, ref info);
                if (nearest != null)
                {
                    cpBody body = nearest.GetBody();                    // cpShapeGetBody();
                    if (body.bodyType == cpBodyType.STATIC)
                    {
                        body.SetBodyType(cpBodyType.DYNAMIC);
                        body.SetMass(pentagon_mass);
                        body.SetMoment(pentagon_moment);
                    }
                    else if (body.bodyType == cpBodyType.DYNAMIC)
                    {
                        body.SetBodyType(cpBodyType.STATIC);
                    }
                }
            }



            space.EachBody(eachBody, null);

            space.Step(dt);
        }
示例#2
0
        /// <summary>
        /// Creates a physics body and adds it to the world.
        /// It also adds shape to the body, unless it is null
        /// 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 to the body too</param>
        /// <param name="bodyType">The type of body to create</param>
        /// <param name="mass">The mass of the object</param>
        /// <param name="moment">The moment of inertia</param>
        /// <returns></returns>
        public static cpBody CreateBody(World world, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1f, float moment = 1f)
        {
            //Create the body and set the type
            cpBody body = new cpBody(mass, moment);

            body.SetBodyType(bodyType);

            //Add it to the world
            Worlds[world].AddBody(body);

            //Return it
            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));
    }
示例#4
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;
            }
        }