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(); }
public void Add(cpShape shape) { if (shape == null) { return; } shape.SetFilter(new cpShapeFilter(_group, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES)); _shapes.Add(shape); Map.Add(shape, this); }
public override void OnEnter() { base.OnEnter(); space.SetIterations(30); space.SetGravity(new cpVect(0, -300)); space.SetSleepTimeThreshold(0.5f); space.SetCollisionSlop(0.5f); cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), new cpVect(-600, -240), new cpVect(600, -240), 0.0f)); shape.SetElasticity(1.0f); shape.SetFriction(1.0f); shape.SetFilter(NOT_GRABBABLE_FILTER); // Add the dominoes. var n = 7; for (var i = 0; i < n; i++) { for (var j = 0; j < (n - i); j++) { var offset = new cpVect((j - (n - 1 - i) * 0.5f) * 1.5f * HEIGHT, (i + 0.5f) * (HEIGHT + 2 * WIDTH) - WIDTH - 240); add_domino(offset, false); add_domino(cpVect.cpvadd(offset, new cpVect(0, (HEIGHT + WIDTH) / 2)), true); if (j == 0) { add_domino(cpVect.cpvadd(offset, new cpVect(0.5f * (WIDTH - HEIGHT), HEIGHT + WIDTH)), false); } if (j != n - i - 1) { add_domino(cpVect.cpvadd(offset, new cpVect(HEIGHT * 0.75f, (HEIGHT + 3 * WIDTH) / 2)), true); } else { add_domino(cpVect.cpvadd(offset, new cpVect(0.5f * (HEIGHT - WIDTH), HEIGHT + WIDTH)), false); } } } Schedule(); }
public override void OnEnter() { base.OnEnter(); space.SetIterations(20); planetBody = space.AddBody(cpBody.NewKinematic()); planetBody.SetAngularVelocity(0.2f); for (int i = 0; i < 30; i++) { add_box(); } cpShape shape = space.AddShape(new cpCircleShape(planetBody, 70, cpVect.Zero)); shape.SetElasticity(1); shape.SetFriction(1); shape.SetFilter(NOT_GRABBABLE_FILTER); Schedule(); }
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); }
public override void OnEnter() { base.OnEnter(); SetSubTitle("This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it."); space.SetIterations(30); space.SetGravity(new cpVect(0, -500)); { cpShape shape = null; cpBody staticBody = space.GetStaticBody(); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-3200, -240), new cpVect(3200, -240), 0.0f)); shape.SetElasticity(1.0f); shape.SetFriction(1.0f); shape.SetFilter(NOT_GRABBABLE_FILTER); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(0, -200), new cpVect(240, -240), 0.0f)); shape.SetElasticity(1.0f); shape.SetFriction(1.0f); shape.SetFilter(NOT_GRABBABLE_FILTER); shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-240, -240), new cpVect(0, -200), 0.0f)); shape.SetElasticity(1.0f); shape.SetFriction(1.0f); shape.SetFilter(NOT_GRABBABLE_FILTER); } { float radius = 20.0f; float mass = 1.0f; float moment = cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero); wheel_body = space.AddBody(new cpBody(mass, moment)); wheel_body.SetPosition(new cpVect(0.0f, -160.0f + radius)); cpShape shape = space.AddShape(new cpCircleShape(wheel_body, radius, cpVect.Zero)); shape.SetFriction(0.7f); shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES)); } { float cog_offset = 30.0f; cpBB bb1 = new cpBB(-5.0f, 0.0f - cog_offset, 5.0f, cog_offset * 1.2f - cog_offset); cpBB bb2 = new cpBB(-25.0f, bb1.t, 25.0f, bb1.t + 10.0f); float mass = 3.0f; float moment = cp.MomentForBox2(mass, bb1) + cp.MomentForBox2(mass, bb2); balance_body = space.AddBody(new cpBody(mass, moment)); balance_body.SetPosition(new cpVect(0.0f, wheel_body.GetPosition().y + cog_offset)); cpShape shape = null; shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb1, 0.0f)); shape.SetFriction(1.0f); shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES)); shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb2, 0.0f)); shape.SetFriction(1.0f); shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES)); } cpVect anchorA = balance_body.WorldToLocal(wheel_body.GetPosition()); cpVect groove_a = cpVect.cpvadd(anchorA, new cpVect(0.0f, 30.0f)); cpVect groove_b = cpVect.cpvadd(anchorA, new cpVect(0.0f, -10.0f)); space.AddConstraint(new cpGrooveJoint(balance_body, wheel_body, groove_a, groove_b, cpVect.Zero)); space.AddConstraint(new cpDampedSpring(balance_body, wheel_body, anchorA, cpVect.Zero, 0.0f, 6.0e2f, 30.0f)); motor = space.AddConstraint(new cpSimpleMotor(wheel_body, balance_body, 0.0f)); motor.SetPreSolveFunc((s) => motor_preSolve(motor, s)); { float width = 100.0f; float height = 20.0f; float mass = 3.0f; cpBody boxBody = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height))); boxBody.SetPosition(new cpVect(200, -100)); cpShape shape = space.AddShape(cpPolyShape.BoxShape(boxBody, width, height, 0.0f)); shape.SetFriction(0.7f); } Schedule(); }