Пример #1
0
    public MyLayer() {
      WYSize s = Director.getInstance().getWindowSize();
      Chipmunk chipmunk = Chipmunk.make();
      chipmunk.setDebugDraw(true);
      chipmunk.setPosition(s.width / 2, s.height / 2);
      addChild(chipmunk);

      mRandom = new Random();

      Shape.resetShapeIdCounter();

      mSpace = chipmunk.getSpace();
      mSpace.setIterations(5);
      mSpace.setGravity(0, -100);
      mSpace.resizeActiveHash(40f, 999);
      mSpace.resizeStaticHash(30f, 2999);

      Body staticBody = Body.make(Float.MAX_VALUE, Float.MAX_VALUE);

      // Create vertexes for a pentagon shape.
      WYPoint[] verts = new WYPoint[5];
      for (int i = 0; i < NUM_VERTS; i++) {
        float angle = -2 * (float) Math.PI * i / (NUM_VERTS);
        verts[i] = WYPoint.make(10 * (float) cos(angle), 10 * (float) sin(angle));
      }

      // Vertexes for a triangle shape.
      WYPoint tris[] = {WYPoint.make(-15, -15), WYPoint.make(0, 10), WYPoint.make(15, -15)};

      // Create the static triangles.
      for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 6; j++) {
          float stagger = (j % 2) * 40;
          WYPoint offset = WYPoint.make(i * 80 - s.width / 2 + stagger, j * 70 - s.width / 2);
          Poly shape = Poly.make(staticBody, tris, offset);
          shape.setRestitution(1f);
          shape.setFriction(1f);
          shape.setLayerMask(Chipmunk.NOT_GRABABLE_MASK);
          mSpace.addStaticShape(shape);
        }
      }

      // Add lots of pentagons
      float moment = Chipmunk.calculateMomentForPoly(1f, verts, WYPoint.makeZero());
      for (int i = 0; i < 50; i++) {
        Body body = Body.make(1f, moment);
        float x = frand() * s.width - s.width / 2;
        body.setPosition(x, s.height / 2 * 5 / 7);
        mSpace.addBody(body);

        Poly shape = Poly.make(body, verts, WYPoint.makeZero());
        shape.setFriction(0.4f);
        mSpace.addShape(shape);
      }

      schedule(new TargetSelector(this, "update(float)", new Object[] {0f}));
    }
Пример #2
0
 public void update(float delta) {
   mSpace.step(delta);
   mSpace.visitBody(this, null);
 }