I'm working on a game based on Box2D and Starling which includes breakable bodies - bodies/objects that when hit with sufficient force break apart (into smaller pieces). I've managed the physics part but I'm stuck on the graphics.

Using the old non-Stage3D Flash APIs I would do something like this to draw the resulting pieces of the breakable body (after the impact):

       var m:Matrix = new Matrix();
        m.tx=- texture.width*0.5;
        m.ty=- texture.height*0.5;

        this.graphics.beginBitmapFill(texture, m, true, true);
        this.graphics.moveTo(verticesVec[0].x, verticesVec[0].y);
        for (var i:int=1; i<verticesVec.length; i++) {
            this.graphics.lineTo(verticesVec[i].x, verticesVec[i].y);
        this.graphics.lineTo(verticesVec[0].x, verticesVec[0].y);

The verticesVec contains the vertices of the piece I want to draw. In Starling I've managed to draw the pieces using a fragment shader like so:

      fill = new Fill();
      fill.material.fragmentShader = new TextureVertexColorFragmentShader();
      //fill.material.textures[0] = Assets.getTexture("Box");
      fill.addVertex(verticesVec[0].x,  verticesVec[0].y,0x000000);

      for (var i:int=1; i<verticesVec.length; i++) {
               fill.addVertex(verticesVec[i].x,  verticesVec[i].y,0x000000 );

     fill.addVertex(verticesVec[0].x, verticesVec[0].y,0x000000 );

But I can't figure out how to manipulate the texture so it fits my pieces. When I try adding a texture it ends up stretched/rotated, etc.

I'm kind of new to shaders and I would really appreciate a push in the right direction.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.