COOKIES

This site may be using cookies to melk you with your own data. I, ben0bi, am not the owner of this web service and I also do not maintain their servers. But the EU and the owner of this service think, that the user (me) has the responsibility to inform the consumer (you), that this website uses cookies. Again: I, ben0bi, NEVER use cookies. I am not responsible for the setup of this web service. I just present some information here and do not intend to spy on you for whatever reason ever. But (also again), I do not host this website nor do I maintain any servers related to this website nor do I benefit from using the cookies maintained from this service. I hereby give the responsibility for using cookies on blogspot back to the owners of blogspot.

Sonntag, 17. Mai 2015

jMonkeyEngine: Circle Class

With this class you can draw simple circles in 3D-Space.

This post is somewhat a direct copy of this post here:
http://hub.jmonkeyengine.org/t/drawing-a-simple-circle-in-jme3/15461/13
because there it is a little bit messed up.

Here's the class:

/*
 * Original code by Martin Simons.
 * Created by Oki Wan Ben0bi @ 2015
 */

import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.util.BufferUtils;
import java.nio.FloatBuffer;

public class Circle extends Mesh
{
    private Vector3f center;
    private float radius;
    private int sampleCount;
   
    public Circle(float radius)
    {
        this(Vector3f.ZERO,radius, 16);
    }
   
    public Circle(float radius, int samples)
    {
        this(Vector3f.ZERO, radius, samples);
    }
   
    public Circle(Vector3f center, float radius, int samples)
    {
        super();
           
        this.center=center;
        this.radius=radius;
        this.sampleCount=samples;
       
        setMode(Mode.Lines);
        updateGeometry();
    }
   
    protected void updateGeometry()
    {
        FloatBuffer positions=BufferUtils.createFloatBuffer(sampleCount*3);
        FloatBuffer normals=BufferUtils.createFloatBuffer(sampleCount*3);
        short[] indices = new short[sampleCount*2];
       
        float rate=FastMath.TWO_PI / (float)sampleCount;
        float angle=0;
       
        int idc=0;
        for(int i=0;i<sampleCount;i++)
        {
            float x=FastMath.cos(angle)*radius+center.x;
            float z=FastMath.sin(angle)*radius+center.z;
           
            positions.put(x).put(center.y).put(z);
            normals.put(new float[] {0,1,0});
           
            indices[idc++]=(short)i;
            if(i< sampleCount-1)
                indices[idc++]=(short)(i+1);
            else
                indices[idc++]=0;
           
            angle+=rate;
        }
       
        setBuffer(Type.Position,3, positions);
        setBuffer(Type.Normal,3,normals);
        setBuffer(Type.Index, 2, indices);
       
        setBuffer(Type.TexCoord,2,new float[] {0,0,1,1});
        updateBound();
    }
}

And here is an example for implementing it:

Circle circle=new Circle(10);
Geometry g=new Geometry("Circle", circle);
Material mat= new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
g.setMaterial(mat);
rootNode.attachChild(g);

I hope you can use it.

Keine Kommentare:

Kommentar veröffentlichen