[Share] Breaking Glass: ShatteredSprite Demo

“MichaelB” shared this demo source code showing how to implement a breaking glass effect on cocos2d the forum, you can check the post here:
http://www.cocos2d-iphone.org/forum/topic/19523

Screen shot:

————————————————————————————————————————————————————
To Download Page…

☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆

If you feel all the stuffs in this site helped you a lot and you would buy me a beer 🙂


Or get a game I made 🙂


☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. i want same effect in android.
    but i am in confusion to write the same effect in android with java language.

    please help me.

    here is my code.

    package com.tutorials.cocos2dsimplegame;

    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.nio.FloatBuffer;

    import javax.microedition.khronos.opengles.GL10;

    import org.cocos2d.nodes.CCDirector;
    import org.cocos2d.nodes.CCSprite;
    import org.cocos2d.opengl.CCRenderTexture;
    import org.cocos2d.types.CGPoint;
    import org.cocos2d.types.CGRect;
    import org.cocos2d.types.ccColor4B;

    public class shatter extends CCSprite
    {

    public static final int SHATTER_VERTEX_MAX=100;

    TriangleVertices[] vertices;
    TriangleVertices[] texCoords;
    TriangleColors[] colorArray;
    float[] adelta;
    CGPoint[] vdelta;
    CGPoint[] centerPt;
    GL10 gl;

    int numVertices;

    FloatBuffer triVertices,triTexCoords;

    static FloatBuffer tmpFloatBuf;
    ByteBuffer colorBuffer;

    int value;

    public static class TriangleVertices{
    CGPoint pt1;
    CGPoint pt2;
    CGPoint pt3;

    TriangleVertices(CGPoint p1,CGPoint p2,CGPoint p3){
    pt1 =p1;
    pt2 =p2;
    pt3 =p3;
    }

    public static TriangleVertices tri(CGPoint pt1, CGPoint pt2, CGPoint pt3) {
    TriangleVertices t = new TriangleVertices(pt1, pt2, pt3 );
    return t;
    }
    }

    public class TriangleColors{
    ccColor4B c1;
    ccColor4B c2;
    ccColor4B c3;

    TriangleColors(ccColor4B cl1,ccColor4B cl2,ccColor4B cl3){
    c1 = cl1;
    c2 = cl2;
    c3 = cl3;
    }
    }

    shatter(){
    super();
    vertices = new TriangleVertices[SHATTER_VERTEX_MAX];
    texCoords = new TriangleVertices[SHATTER_VERTEX_MAX];
    colorArray = new TriangleColors[SHATTER_VERTEX_MAX];
    adelta = new float[SHATTER_VERTEX_MAX];
    vdelta = new CGPoint[SHATTER_VERTEX_MAX];
    centerPt = new CGPoint[SHATTER_VERTEX_MAX];
    gl=CCDirector.gl;

    }

    private static FloatBuffer getVertices(int size) {
    if(tmpFloatBuf == null || tmpFloatBuf.capacity() < size) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(4 * size);
    vbb.order(ByteOrder.nativeOrder());
    tmpFloatBuf = vbb.asFloatBuffer();
    }
    tmpFloatBuf.rewind();
    return tmpFloatBuf;
    }

    public float randf(float base, float range) {
    if (range==0) return base;
    long lRange = (long) (Math.random()%(int)((range*2)*10000));
    float fRange = (float) (((float)lRange/10000.0) – range);
    return base + fRange;
    }
    public shatter shatterWithSprite(CCSprite sprite ,int piecesX ,int piecesY ,float speedVar,float rotVar){
    return initWithSprite(sprite ,piecesX,piecesY,speedVar ,rotVar);

    }

    private shatter initWithSprite(CCSprite sprite, int piecesX, int piecesY, float speedVar, float rotVar) {
    // TODO Auto-generated method stub
    this.shatterSprite(sprite ,piecesX,piecesY,speedVar ,rotVar);
    this.scheduleUpdate();
    return this;
    }
    public void update(float dt){
    System.out.println("update");
    //Note, does NOT adjust vdelta and adelta for slow frames;
    //To do that, need some d=(delta*60.0) that's multiplied by the vdelta and adelta
    for (int i = 0; i<numVertices; i++) {
    vertices[i].pt1 = CGPoint.ccpAdd(vertices[i].pt1, vdelta[i]);
    vertices[i].pt2 = CGPoint.ccpAdd(vertices[i].pt2, vdelta[i]);
    vertices[i].pt3 = CGPoint.ccpAdd(vertices[i].pt3, vdelta[i]);
    centerPt[i] = CGPoint.ccpAdd(centerPt[i], vdelta[i]);

    vertices[i].pt1 = CGPoint.ccpRotateByAngle(vertices[i].pt1, centerPt[i], adelta[i]);
    vertices[i].pt2 = CGPoint.ccpRotateByAngle(vertices[i].pt2, centerPt[i], adelta[i]);
    vertices[i].pt3 = CGPoint.ccpRotateByAngle(vertices[i].pt3, centerPt[i], adelta[i]);
    }
    }

    public void draw(GL10 gl){

    gl.glBindTexture(GL10.GL_TEXTURE_2D, this.getTexture().name());

    triVertices = getVertices(2 *3* SHATTER_VERTEX_MAX);
    triTexCoords = getVertices(2 *3* SHATTER_VERTEX_MAX);
    colorBuffer =ByteBuffer.allocateDirect(2 *3* SHATTER_VERTEX_MAX);

    for(int i=0;i<numVertices;i++){

    triVertices.put(vertices[i].pt1.x);
    triVertices.put(vertices[i].pt1.y);
    triVertices.put(vertices[i].pt2.x);
    triVertices.put(vertices[i].pt2.y);
    triVertices.put(vertices[i].pt2.x);
    triVertices.put(vertices[i].pt2.y);

    triTexCoords.put(texCoords[i].pt1.x);
    triTexCoords.put(texCoords[i].pt1.y);
    triTexCoords.put(texCoords[i].pt2.x);
    triTexCoords.put(texCoords[i].pt2.y);
    triTexCoords.put(texCoords[i].pt2.x);
    triTexCoords.put(texCoords[i].pt2.y);

    colorBuffer.put((byte)colorArray[i].c1.r);
    colorBuffer.put((byte)colorArray[i].c1.g);
    colorBuffer.put((byte)colorArray[i].c1.b);
    colorBuffer.put((byte)colorArray[i].c1.a);

    colorBuffer.put((byte)colorArray[i].c2.r);
    colorBuffer.put((byte)colorArray[i].c2.g);
    colorBuffer.put((byte)colorArray[i].c2.b);
    colorBuffer.put((byte)colorArray[i].c2.a);

    colorBuffer.put((byte)colorArray[i].c3.r);
    colorBuffer.put((byte)colorArray[i].c3.g);
    colorBuffer.put((byte)colorArray[i].c3.b);
    colorBuffer.put((byte)colorArray[i].c3.a);
    value++;
    }
    triVertices.position(0);
    triTexCoords.position(0);
    colorBuffer.position(0);

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, triVertices);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, triTexCoords);
    gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, colorBuffer);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, numVertices*3);
    //gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length/3);

    }

    public void updateColor(){
    System.out.println("updateColor");
    ccColor4B color4 = ccColor4B.ccc4( 177,223,243,255);
    TriangleColors triColor4 =new TriangleColors( color4, color4, color4 );
    for (int i=0; i<numVertices; i++) {
    colorArray[i] = triColor4;
    }
    }

    private void shatterSprite(CCSprite sprite, int piecesX, int piecesY, float speedVar, float rotVar) {
    // TODO Auto-generated method stub
    System.out.println("shatterSprite");

    CCRenderTexture rt= CCRenderTexture.renderTexture((int)sprite.getContentSize().width, (int)sprite.getContentSize().height);
    rt.begin();
    CCSprite s2=CCSprite.sprite(sprite.getTexture(), sprite.getTextureRect());
    s2.setPosition(CGPoint.ccp(sprite.getContentSize().width/2, sprite.getContentSize().height/2));
    if(gl==null){
    gl=CCDirector.gl;
    }
    s2.visit(gl);
    rt.end();

    this.setTexture(rt.getSprite().getTexture());
    this.setTextureRect(CGRect.make((float)0, (float)0, sprite.getContentSize().width, sprite.getContentSize().height));

    //Sizey thingys
    float wid = sprite.getContentSize().width;
    float hgt = sprite.getContentSize().height;
    float pieceXsize = wid/(float)piecesX;
    float pieceYsize = hgt/(float)piecesY;
    float texWid = wid/this.getTexture().pixelsWide();
    float texHgt = hgt/this.getTexture().pixelsHigh();

    ccColor4B color4 = ccColor4B.ccc4(177,223,243,255);

    TriangleColors triColor4 = new TriangleColors( color4, color4, color4 );

    CGPoint[][] ptArray= new CGPoint [piecesX+1][piecesY+1];

    for (int x=0; x<=piecesX; x++) {
    for (int y=0; y0 && x0 && y<piecesY) {
    pt = CGPoint.ccpAdd(pt, CGPoint.ccp(Math.round(randf(0.0f,pieceXsize*0.45f)), Math.round(randf(0.0f, pieceYsize*0.45f))));
    }
    ptArray[x][y] = pt;
    }
    }

    numVertices = 0;

    for (int x=0; x<piecesX; x++) {
    for (int y=0; y=SHATTER_VERTEX_MAX) {
    System.out.println(“NeedABiggerArray!”);
    return;
    }

    vdelta[numVertices] = CGPoint.ccp(randf(0.0f, speedVar), randf(0.0f, speedVar));
    adelta[numVertices] = randf(0.0f, rotVar);
    colorArray[numVertices] = triColor4;
    centerPt[numVertices] = CGPoint.ccp((x*pieceXsize)+(pieceXsize*0.3f), (y*pieceYsize)+(pieceYsize*0.3f));

    vertices[numVertices] = TriangleVertices.tri(ptArray[x][y], ptArray[x+1][y], ptArray[x][y+1]);
    texCoords[numVertices++] = TriangleVertices.tri(CGPoint.ccp((ptArray[x][y].x/wid)*texWid, (ptArray[x][y].y/hgt)*texHgt),
    CGPoint.ccp((ptArray[x+1][y].x/wid)*texWid, (ptArray[x+1][y].y/hgt)*texHgt),
    CGPoint.ccp((ptArray[x][y+1].x/wid)*texWid, (ptArray[x][y+1].y/hgt)*texHgt));

    //Triangle #2
    vdelta[numVertices] = CGPoint.ccp(randf(0.0f, speedVar), randf(0.0f, speedVar));
    adelta[numVertices] = randf(0.0f, rotVar);
    colorArray[numVertices] = triColor4;
    centerPt[numVertices] = CGPoint.ccp((x*pieceXsize)+(pieceXsize*0.7f), (y*pieceYsize)+(pieceYsize*0.7f));

    vertices[numVertices] = TriangleVertices.tri(ptArray[x+1][y],
    ptArray[x+1][y+1],
    ptArray[x][y+1]);
    texCoords[numVertices++] = TriangleVertices.tri(CGPoint.ccp((ptArray[x+1][y].x/wid)*texWid, (ptArray[x+1][y].y/hgt)*texHgt),
    CGPoint.ccp((ptArray[x+1][y+1].x/wid)*texWid, (ptArray[x+1][y+1].y/hgt)*texHgt),
    CGPoint.ccp((ptArray[x][y+1].x/wid)*texWid, (ptArray[x][y+1].y/hgt)*texHgt));
    }
    }

    }
    }