protected override void OnAwake() { base.OnAwake(); Vector2 pos = this.transform.position;//static body(0,0) should add postion , Dynamic use Rigidbody position as parent XRigidbody2D xRigidbody2D = this.GetComponent <XRigidbody2D>(); if (xRigidbody2D != null) { if (xRigidbody2D.mBodyType != cpBodyType.STATIC) { xRigidbody2D.Init(); mRigidbody2D = xRigidbody2D.Rigidbody2D; pos = Vector2.zero; } else { mRigidbody2D = XSpaceManager.Instance.Space.GetStaticBody(); } } else { //静态的,不能移动 mRigidbody2D = XSpaceManager.Instance.Space.GetStaticBody(); } //local Vector2 left, bottom, right, top; Quaternion rotation; switch (mShapeType) { case ColliderType.Segment: float angle = Vector2.Angle(this.mEnd - this.mStart, Vector2.right); rotation = Quaternion.AngleAxis(angle + this.transform.eulerAngles.z, Vector3.forward); left = rotation * new Vector2(this.mStart.x, this.mStart.y - this.mRadius); bottom = rotation * new Vector2(this.mEnd.x, this.mEnd.y - this.mRadius); right = rotation * new Vector2(this.mEnd.x, this.mEnd.y + this.mRadius); top = rotation * new Vector2(this.mStart.x, this.mStart.y + this.mRadius); Vector2 start = (Vector2)pos + (left + top) / 2; Vector2 end = (Vector2)pos + (bottom + right) / 2; mCollider2D = new cpSegmentShape(mRigidbody2D, new cpVect(start.x, start.y) * XSpaceManager.PixelsPerUnit, new cpVect(end.x, end.y) * XSpaceManager.PixelsPerUnit, mRadius * XSpaceManager.PixelsPerUnit); break; case ColliderType.Circle: mCollider2D = new cpCircleShape(mRigidbody2D, mRadius * XSpaceManager.PixelsPerUnit, new cpVect(pos.x + mCenter.x, pos.y + mCenter.y) * XSpaceManager.PixelsPerUnit); break; case ColliderType.Box: mCollider2D = cpPolyShape.BoxShape2(mRigidbody2D, new cpBB((pos.x + mCenter.x - mSize.x * this.transform.localScale.x / 2) * XSpaceManager.PixelsPerUnit, (pos.y + mCenter.y - mSize.y * this.transform.localScale.y / 2) * XSpaceManager.PixelsPerUnit, (pos.x + mCenter.x + mSize.x * this.transform.localScale.x / 2) * XSpaceManager.PixelsPerUnit, (pos.y + mCenter.y + mSize.y * this.transform.localScale.y / 2) * XSpaceManager.PixelsPerUnit), mRadius * XSpaceManager.PixelsPerUnit); break; case ColliderType.Polygon: int count = mVects.Length; cpVect [] vts = new cpVect [mVects.Length]; for (int i = 0; i < count; i++) { vts[i] = cpVect.Zero; vts[i].x = (pos.x + mVects[i].x) * XSpaceManager.PixelsPerUnit; vts[i].y = (pos.y + mVects[i].y) * XSpaceManager.PixelsPerUnit; } mCollider2D = new cpPolyShape(mRigidbody2D, mVects.Length, vts, mRadius * XSpaceManager.PixelsPerUnit); break; //case cpShapeType.NumShapes: // break; } mCollider2D.SetSensor(mTrigger); mCollider2D.SetElasticity(mElasticity); mCollider2D.SetFriction(mFriction); cpShapeFilter filter = new cpShapeFilter(mGroup, (int)mCategory, (int)mMask); mCollider2D.SetFilter(filter); }