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 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); }
public void ShatterCell(cpPolyShape shape, cpVect cell, int cell_i, int cell_j, ref WorleyContex context) { // printf("cell %dx%d: (% 5.2f, % 5.2f)\n", cell_i, cell_j, cell.x, cell.y); cpBody body = shape.body; // cpShapeGetBody(shape); cpVect[] ping = new cpVect[MAX_VERTEXES_PER_VORONOI]; // cpVect[ (cpVect*)alloca( * sizeof(cpVect)); cpVect[] pong = new cpVect[MAX_VERTEXES_PER_VORONOI]; //(cpVect*)alloca(MAX_VERTEXES_PER_VORONOI * sizeof(cpVect)); int count = shape.Count; // cpPolyShapeGetCount(); count = (count > MAX_VERTEXES_PER_VORONOI ? MAX_VERTEXES_PER_VORONOI : count); for (int i = 0; i < count; i++) { ping[i] = body.LocalToWorld(shape.GetVert(i)); } cpPointQueryInfo info = null; for (int i = 0; i < context.width; i++) { for (int j = 0; j < context.height; j++) { if ( !(i == cell_i && j == cell_j) && shape.PointQuery(cell, ref info) < 0 ) { count = ClipCell(shape, cell, i, j, context, ping, pong, count); for (int u = 0; u < pong.Length; u++) { if (pong[u] != null) { ping[u] = new cpVect(pong[u]); } } } } } cpVect centroid = cp.CentroidForPoly(count, ping); float mass = cp.AreaForPoly(count, ping, 0) * DENSITY; float moment = cp.MomentForPoly(mass, count, ping, cpVect.cpvneg(centroid), 0); cpBody new_body = space.AddBody(new cpBody(mass, moment)); new_body.SetPosition(centroid); new_body.SetPosition(centroid); new_body.SetVelocity(body.GetVelocityAtLocalPoint(centroid)); new_body.SetAngularVelocity(body.GetAngularVelocity()); cpTransform transform = cpTransform.Translate(cpVect.cpvneg(centroid)); cpShape new_shape = space.AddShape(new cpPolyShape(new_body, count, ping, transform, 0)); // Copy whatever properties you have set on the original shape that are important new_shape.SetFriction(shape.GetFriction()); }
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); }
public void ClipPoly(cpSpace space, cpShape shp, cpVect n, float dist) { cpPolyShape shape = (cpPolyShape)shp; cpBody body = shape.GetBody(); int count = shape.Count; int clippedCount = 0; cpVect[] clipped = new cpVect[count + 1]; for (int i = 0, j = count - 1; i < count; j = i, i++) { cpVect a = body.LocalToWorld(shape.GetVert(j)); float a_dist = cpVect.cpvdot(a, n) - dist; if (a_dist < 0) { clipped[clippedCount] = a; clippedCount++; } cpVect b = body.LocalToWorld(shape.GetVert(i)); float b_dist = cpVect.cpvdot(b, n) - dist; if (a_dist * b_dist < 0) { float t = cp.cpfabs(a_dist) / (cp.cpfabs(a_dist) + cp.cpfabs(b_dist)); clipped[clippedCount] = cpVect.cpvlerp(a, b, t); clippedCount++; } } cpVect centroid = cp.CentroidForPoly(clippedCount, clipped); float mass = cp.AreaForPoly(clippedCount, clipped, 0) * DENSITY; float moment = cp.MomentForPoly(mass, clippedCount, clipped, cpVect.cpvneg(centroid), 0); cpBody new_body = space.AddBody(new cpBody(mass, moment)); new_body.SetPosition(centroid); new_body.SetVelocity(body.GetVelocityAtWorldPoint(centroid)); new_body.SetAngularVelocity(body.GetAngularVelocity()); cpTransform transform = cpTransform.Translate(cpVect.cpvneg(centroid)); cpShape new_shape = space.AddShape(new cpPolyShape(new_body, clippedCount, clipped, transform, 0)); // Copy whatever properties you have set on the original shape that are important new_shape.SetFriction(shape.GetFriction()); }
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); }
static void Main(string[] args) { cpSpace space = new cpSpace(); space.SetGravity(new cpVect(0, -100)); cpShape shape = cpShape.NewSegment(space.StaticBody, new cpVect(-20, 5), new cpVect(20, -5), 0); shape.SetFriction(1); space.AddShape(shape); double radius = 5; double mass = 1; double moment = cpUtil.MomentForCircle(mass, 0, radius, cpVect.Zero); cpBody ballBody = new cpBody(mass, moment); space.AddBody(ballBody); ballBody.Position = new cpVect(0, 15); cpShape ballShape = cpShape.NewCircle(ballBody, radius, cpVect.Zero); space.AddShape(ballShape); ballShape.SetFriction(0.7); double timeStep = 1 / 60d; for (double time = 0; time < 2; time += timeStep) { cpVect pos = ballBody.Position; cpVect vel = ballBody.Velocity; Console.WriteLine("Time is {0:F2}. ballBody is at ({1:F2}, {2:F2}). It's velocity is ({3:F2}, {4:F2})", time, pos.X, pos.Y, vel.X, vel.Y ); space.Step(timeStep); } Console.ReadKey(); }
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 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(); }