1
\$\begingroup\$

I have a map of tiles that is drawn using two triangles. I draw my map using a height map to generate vertices and make a Cell(tile) out of 4 vertices like so:

cells[x][z] = new Cell(corner1, corner2, corner3, corner4, colour1, colour2, colour3, colour4);

Now I want it so that if 3 corners match in colour, let's say blue, and the other 4th is green then I should have one triangle will be that blue and the other that is green.

Now my logic for this is as follows:

// triangle 1
VertexInfo v1 = new VertexInfo();
VertexInfo v2 = new VertexInfo();
VertexInfo v3 = new VertexInfo();

// triangle 2
VertexInfo v4 = new VertexInfo();
VertexInfo v5 = new VertexInfo();
VertexInfo v6 = new VertexInfo();

for (Cell[] cellrow : chunks[c].cells) {
for (Cell cell : cellrow) {
    if (cell.getColor1().equals(cell.getColor4())
            && cell.getColor1().equals(cell.getColor2())
            && cell.getColor2().equals(cell.getColor4())
                    && !cell.getColor1().equals(cell.getColor3())) {
/* 4\
*  | \ 
*  1--\2
*/                      
        v1.setPos(cell.getCorner3()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());
        v2.setPos(cell.getCorner2()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());
        v3.setPos(cell.getCorner4()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());

        v4.setPos(cell.getCorner1()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v5.setPos(cell.getCorner4()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v6.setPos(cell.getCorner2()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6);

    } else if (cell.getColor2().equals(cell.getColor1())
            && cell.getColor2().equals(cell.getColor3())
            && cell.getColor3().equals(cell.getColor1())
                    && !cell.getColor2().equals(cell.getColor4())) {
/*    /3
*    / |
*  1/--2
*/                  
        v1.setPos(cell.getCorner1()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());
        v2.setPos(cell.getCorner3()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());
        v3.setPos(cell.getCorner2()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());

        v4.setPos(cell.getCorner3()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());
        v5.setPos(cell.getCorner1()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());
        v6.setPos(cell.getCorner4()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6);

    } else if (cell.getColor3().equals(cell.getColor4())
            && cell.getColor3().equals(cell.getColor2())
            && cell.getColor2().equals(cell.getColor4())
            && !cell.getColor3().equals(cell.getColor1())) {
/* 4\--3
*    \ |
*     \2
*/              
        v1.setPos(cell.getCorner3()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());
        v2.setPos(cell.getCorner2()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());
        v3.setPos(cell.getCorner4()).setNor(cell.getLeftNormal()).setCol(cell.getColor3()).setUV(cell.getTexturePos());

        v4.setPos(cell.getCorner1()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v5.setPos(cell.getCorner4()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v6.setPos(cell.getCorner2()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6);

    }else if (cell.getColor4().equals(cell.getColor1())
            && cell.getColor4().equals(cell.getColor3())
            && cell.getColor3().equals(cell.getColor1())
                    && !cell.getColor4().equals(cell.getColor2())) {
/* 4--/3
*  | / 
*  1/
*/  

        v1.setPos(cell.getCorner1()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());
        v2.setPos(cell.getCorner3()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());
        v3.setPos(cell.getCorner2()).setNor(cell.getLeftNormal()).setCol(cell.getColor2()).setUV(cell.getTexturePos());

        v4.setPos(cell.getCorner4()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());
        v5.setPos(cell.getCorner3()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());
        v6.setPos(cell.getCorner1()).setNor(cell.getRightNormal()).setCol(cell.getColor4()).setUV(cell.getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6); 
    } else {

/* 4---3
*  |   |
*  1---2
* (both same colour)
*/
        v1.setPos(cell.getCorner1()).setNor(cell.getLeftNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v2.setPos(cell.getCorner3()).setNor(cell.getLeftNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v3.setPos(cell.getCorner2()).setNor(cell.getLeftNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());

        v4.setPos(cell.getCorner4()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v5.setPos(cell.getCorner3()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());
        v6.setPos(cell.getCorner1()).setNor(cell.getRightNormal()).setCol(cell.getColor1()).setUV(cell.getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6);

    }
}

But with the above logic I get this:

I don't see why this isn't working. Does anyone have a solution? or has anyone already done this?

\$\endgroup\$
0
\$\begingroup\$

I decided to use the surrounding tiles rather than the corners of a single cell. It's not exactly what I was looking for but here is the resulting map and the code (Still have some fine tuning to do):

for (int cx = 0; cx < chunks[c].cells.length; cx++) {
for (int cz = 0; cz < chunks[c].cells[0].length; cz++) {

    colorC = chunks[c].cells[cx][cz].getColor1();
    if (cx != 0
            && cz != 0
            && cx != chunks[c].cells.length - 1
            && cz != chunks[c].cells[0].length - 1) {

        colorN = chunks[c].cells[cx][cz + 1].getColor1();
        colorE = chunks[c].cells[cx - 1][cz].getColor1();
        colorW = chunks[c].cells[cx + 1][cz].getColor1();
        colorS = chunks[c].cells[cx][cz - 1].getColor1();
        colorNW = chunks[c].cells[cx + 1][cz + 1].getColor1();
        colorNE = chunks[c].cells[cx - 1][cz + 1].getColor1();
        colorSW = chunks[c].cells[cx + 1][cz - 1].getColor1();
        colorSE = chunks[c].cells[cx - 1][cz - 1].getColor1();

        /* Check for South East */
        if (colorC.equals(colorS)
                && colorC.equals(colorE)
                && colorC.equals(colorSE)
                && !colorC.equals(colorN)
                && !colorC.equals(colorNW)
                && !colorC.equals(colorW)
                ) {

            chunks[c].cells[cx][cz].setColor1(colorN);

            v1.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v2.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v3.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());

            v4.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v5.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v6.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());

            meshBuilder.triangle(v1, v2, v3);
            meshBuilder.triangle(v4, v5, v6);

            /* Check for South West */
        } else if (colorC.equals(colorS)
                && colorC.equals(colorW)
                && colorC.equals(colorSW)
                && !colorC.equals(colorN)
                && !colorC.equals(colorNE)
                && !colorC.equals(colorE)
                ) {

            chunks[c].cells[cx][cz].setColor1(colorS);

            v1.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v2.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v3.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());

            v4.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v5.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v6.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());

            meshBuilder.triangle(v1, v2, v3);
            meshBuilder.triangle(v4, v5, v6);

            /* Check for North West */
        } else if (colorC.equals(colorN)
                && colorC.equals(colorW)
                && colorC.equals(colorNW)
                && !colorC.equals(colorS)
                && !colorC.equals(colorSE)
                && !colorC.equals(colorE)
                ) {

            chunks[c].cells[cx][cz].setColor1(colorN);

            v1.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v2.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v3.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());

            v4.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v5.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v6.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());

            meshBuilder.triangle(v1, v2, v3);
            meshBuilder.triangle(v4, v5, v6);

            /* Check for North East */
        } else if (colorC.equals(colorN)
                && colorC.equals(colorE)
                && colorC.equals(colorNE)
                && !colorC.equals(colorN)
                && !colorC.equals(colorSW)
                && !colorC.equals(colorW)
                ) {

            chunks[c].cells[cx][cz].setColor1(colorN);

            v1.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v2.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v3.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorS).setUV(chunks[c].cells[cx][cz].getTexturePos());

            v4.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v5.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v6.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorN).setUV(chunks[c].cells[cx][cz].getTexturePos());

            meshBuilder.triangle(v1, v2, v3);
            meshBuilder.triangle(v4, v5, v6);

            /* Full Square*/
        } else {
            v1.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v2.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v3.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());

            v4.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v5.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
            v6.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());

            meshBuilder.triangle(v1, v2, v3);
            meshBuilder.triangle(v4, v5, v6);
        }

        /* Edge of chunk*/
    } else {
        v1.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
        v2.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
        v3.setPos(chunks[c].cells[cx][cz].getCorner2()).setNor(chunks[c].cells[cx][cz].getLeftNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());

        v4.setPos(chunks[c].cells[cx][cz].getCorner4()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
        v5.setPos(chunks[c].cells[cx][cz].getCorner3()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());
        v6.setPos(chunks[c].cells[cx][cz].getCorner1()).setNor(chunks[c].cells[cx][cz].getRightNormal()).setCol(colorC).setUV(chunks[c].cells[cx][cz].getTexturePos());

        meshBuilder.triangle(v1, v2, v3);
        meshBuilder.triangle(v4, v5, v6);
    }
| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.