Question

How do you create a torus in libgdx ? Modelbuilder doesn't support it. I have to create a torus in code, and cant load any objects.

Was it helpful?

Solution

With libGDX to create custom Models you'd use the MeshBuilder.

Via the MeshBuilder.vertex(...) method you can add a vertex with the necessary information, one by one. Basically you'll need two nested loops and look up the necessary formulas for the torus here.

You have to wrap it in MeshBuilder.begin(...) and MeshBuilder.end().

MeshBuilder.end() will return a Mesh, which you can then pass to ModelBuilder.fromMesh(mesh) to get the Model you need.

OTHER TIPS

Find resolution, how create torus in libgdx. May will be helpfull.

private void createTorus (int glMaterial, float X, float Y, float Z, float widthR, 
                          float height, int divisionsU, int divisionsV, float r, float g, float b, float a) {

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();
    MeshPartBuilder builder = modelBuilder.part("torus", glMaterial, Usage.Position |
            Usage.Normal, new Material(ColorAttribute.createDiffuse(r, g, b, a)));
    builder.setColor(Color.LIGHT_GRAY);

    VertexInfo curr1 = vertTmp3.set(null, null, null, null);
    curr1.hasUV = curr1.hasPosition = curr1.hasNormal = true;
    VertexInfo curr2 = vertTmp4.set(null, null, null, null);
    curr2.hasUV = curr2.hasPosition = curr2.hasNormal = true;
    short i1, i2, i3 = 0, i4 = 0;

    int i, j, k;
    double s, t, twopi;
    twopi = 2 * Math.PI;

    for (i = 0; i < divisionsV; i++) {
        for (j = 0; j <= divisionsU; j++) {
            for (k = 1; k >= 0; k--) {
                s = (i + k) % divisionsV + 0.5;
                t = j % divisionsU;

                curr1.position.set(
                        (float) ((widthR+height*Math.cos(s * twopi / divisionsV))*Math.cos(t * twopi / divisionsU)),
                        (float) ((widthR+height*Math.cos(s*twopi/divisionsV))*Math.sin(t * twopi / divisionsU)),
                        (float) (height * Math.sin(s * twopi / divisionsV)));
                curr1.normal.set(curr1.position).nor();
                k--;
                s = (i + k) % divisionsV + 0.5;
                curr2.position.set(
                        (float) ((widthR+height*Math.cos(s * twopi / divisionsV))*Math.cos(t * twopi / divisionsU)),
                        (float) ((widthR+height*Math.cos(s*twopi/divisionsV))*Math.sin(t * twopi / divisionsU)),
                        (float) (height * Math.sin(s * twopi / divisionsV)));
                curr2.normal.set(curr1.normal);
                //curr2.uv.set((float) s, 0);
                i1 = builder.vertex(curr1);
                i2 = builder.vertex(curr2);
                builder.rect(i4, i2, i1, i3);
                i4 = i2;
                i3 = i1;
            }
        }
    }

    torus_Model = modelBuilder.end();
    torus_Instances = new  ModelInstance(torus_Model);
}    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top