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(); }
static public cpSpace BouncyTerrainCircles_500(cpSpace space) { //cpSpace space = BENCH_SPACE_NEW(); SetSubTitle("BouncyTerrainCircles 500"); space.SetIterations(10); cpVect offset = new cpVect(-320, -240); for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++) { cpVect a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1]; cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f)); shape.SetElasticity(1.0f); } for (int i = 0; i < 500; i++) { float radius = 5.0f; float mass = radius * radius; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero))); body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero)); body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f)); cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)); shape.SetElasticity(1.0f); } return(space); }
static public cpSpace ComplexTerrainCircles_1000(cpSpace space) { space.SetIterations(10); space.SetGravity(new cpVect(0, -100)); space.SetCollisionSlop(0.5f); cpVect offset = new cpVect(-320, -240); for (int i = 0; i < (complex_terrain_verts.Length - 1); i++) { cpVect a = complex_terrain_verts[i], b = complex_terrain_verts[i + 1]; space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f)); } for (int i = 0; i < 1000; i++) { float radius = 5.0f; float mass = radius * radius; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero))); body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f), new cpVect(0.0f, 300.0f))); cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)); shape.SetElasticity(0.0f); shape.SetFriction(0.0f); } return(space); }
static public void add_circle(cpSpace space, int index, float radius) { float mass = radius * radius / 25.0f; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero))); // cpBody body = cpSpaceAddBody(space, cpBodyInit(&bodies[i], mass, cpMomentForCircle(mass, 0.0f, radius, cpVect.Zero))); body.SetPosition(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f)); cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)); // cpShape shape = cpSpaceAddShape(space, cpCircleShapeInit(&circles[i], body, radius, cpVect.Zero)); shape.SetElasticity(0.0f); shape.SetFriction(0.9f); }
static cpSpace NoCollide(cpSpace space) { space.SetIterations(10); var handler = space.AddCollisionHandler(2, 2); //, (a, s, o) => NoCollide_begin(a, s, o), null, null, null); handler.beginFunc = NoCollide_begin; float radius = 4.5f; var staticBody = space.GetStaticBody(); space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f); space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(330 + radius, -250 - radius), 0.0f)).SetElasticity(1.0f); space.AddShape(new cpSegmentShape(staticBody, new cpVect(330 + radius, 250 + radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f); space.AddShape(new cpSegmentShape(staticBody, new cpVect(-330 - radius, -250 - radius), new cpVect(-330 - radius, 250 + radius), 0.0f)).SetElasticity(1.0f); for (int x = -320; x <= 320; x += 20) { for (int y = -240; y <= 240; y += 20) { space.AddShape(new cpCircleShape(staticBody, radius, new cpVect(x, y))); } } for (int y = 10 - 240; y <= 240; y += 40) { float mass = 7.0f; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero))); body.SetPosition(new cpVect(-320.0f, y)); body.SetVelocity(new cpVect(100.0f, 0.0f)); cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)); shape.SetElasticity(1.0f); shape.SetCollisionType(2); } for (int x = 30 - 320; x <= 320; x += 40) { float mass = 7.0f; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero))); body.SetPosition(new cpVect(x, -240.0f)); body.SetVelocity(new cpVect(0.0f, 100.0f)); cpShape shape = space.AddShape(new cpCircleShape(body, radius, cpVect.Zero)); shape.SetElasticity(1.0f); shape.SetCollisionType(2); } return(space); }
public cpBody add_ball(cpSpace space, cpVect pos) { cpBody body = space.AddBody(new cpBody(1.0f, cp.MomentForCircle(1.0f, 30, 0, cpVect.Zero))); body.SetPosition(pos); cpShape shape = space.AddShape(new cpCircleShape(body, 30, cpVect.Zero)); shape.SetElasticity(0.0f); shape.SetFriction(0.5f); return(body); }
cpBody add_box(float size, float mass) { float radius = cpVect.cpvlength(new cpVect(size, size)); cpBody body = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, size, size))); body.SetPosition(new cpVect((float)CCRandom.Float_0_1() * (640 - 2 * radius) - (320 - radius), (float)CCRandom.Float_0_1() * (480 - 2 * radius) - (240 - radius))); cpShape shape = space.AddShape(cpPolyShape.BoxShape(body, size, size, 0)); shape.SetElasticity(0); shape.SetFriction(0.7f); return(body); }
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(); }
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); }
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(); }
static public cpSpace BouncyTerrainHexagons_500(cpSpace space) { SetSubTitle("BouncyTerrainHexagons 500"); //cpSpace space = BENCH_SPACE_NEW(); space.SetIterations(10); cpVect offset = new cpVect(-320, -240); for (int i = 0; i < (bouncy_terrain_verts.Length - 1); i++) { cpVect a = bouncy_terrain_verts[i], b = bouncy_terrain_verts[i + 1]; cpShape shape = space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f)); shape.SetElasticity(1.0f); } float radius = 5.0f; cpVect[] hexagon = new cpVect[6]; for (int i = 0; i < 6; i++) { float angle = -(float)Math.PI * 2.0f * i / 6.0f; hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel); } for (int i = 0; i < 500; i++) { float mass = radius * radius; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f))); body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 130.0f), cpVect.Zero)); body.SetVelocity(cpVect.cpvmult(cp.frand_unit_circle(), 50.0f)); cpShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel)); shape.SetElasticity(1.0f); } return(space); }
static public cpSpace ComplexTerrainHexagons_1000(cpSpace space) { SetSubTitle("ComplexTerrainHexagons_1000"); space.SetIterations(10); space.SetGravity(new cpVect(0, -100)); space.SetCollisionSlop(0.5f); cpVect offset = new cpVect(-320, -240); for (int i = 0; i < (complex_terrain_verts.Length - 1); i++) { cpVect a = complex_terrain_verts[i], b = complex_terrain_verts[i + 1]; space.AddShape(new cpSegmentShape(space.GetStaticBody(), cpVect.cpvadd(a, offset), cpVect.cpvadd(b, offset), 0.0f)); } float radius = 5.0f; cpVect[] hexagon = new cpVect[6]; for (int i = 0; i < 6; i++) { float angle = -(float)Math.PI * 2.0f * i / 6.0f; hexagon[i] = cpVect.cpvmult(cpVect.cpv(cp.cpfcos(angle), cp.cpfsin(angle)), radius - bevel); } for (int i = 0; i < 1000; i++) { float mass = radius * radius; cpBody body = space.AddBody(new cpBody(mass, cp.MomentForPoly(mass, 6, hexagon, cpVect.Zero, 0.0f))); body.SetPosition(cpVect.cpvadd(cpVect.cpvmult(cp.frand_unit_circle(), 180.0f), new cpVect(0.0f, 300.0f))); cpShape shape = space.AddShape(new cpPolyShape(body, 6, hexagon, cpTransform.Identity, bevel)); shape.SetElasticity(0.0f); shape.SetFriction(0.0f); } return(space); }
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(); }