I’d like to add a Pentagonal prism as a Spatial. There is Box for cube, but how can I add this (or similar) things?
Option a) Model it in blender and then import it
Option b) Do the math, and create a mesh from code
See here: http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:custom_meshes
As zzuegg said, simply creating your mesh in a 3D editor is probably the best solution though. Unless you want to make a 3D editor or need your primitive to be very flexible (or you want to do something like a block world type of game), then you should create a custom mesh. The existing primitives are really just to create placeholders, making a scene by combining mesh primitives is not a good idea for a final application.
I noticed there’s a primitive cube in the assetpack browser. Why not create a nice assetpack of primitive gemoetries and share them on the assetpack browser. That way all people can use and have access to them all the time and be able to use them in the scene composer too
I use the cube a lot!
Thats a good idea, I wanted to do that for a long time now. For now our web people broke the asset pack uploading/downloading though.
Here is a snippet I decided to share. It makes n-sided prisms.
eg to create pentagonal prism: new Prism(radius, halfHeight, 5);
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
public class Prism extends Mesh {
public Prism(float radius, float halfHeight, int sides) {
this(Vector3f.ZERO, radius, halfHeight, sides);
}
public Prism(Vector3f center, float radius, float halfHeight, int sides) {
FloatBuffer fb = BufferUtils.createFloatBuffer(sides*18);
ShortBuffer idx = BufferUtils.createShortBuffer((sides+sides-2)*6);
FloatBuffer normal = BufferUtils.createFloatBuffer(sides*18);
normal.position(6);
float angle = FastMath.TWO_PI/sides,x,z;
float lastX = 0,//FastMath.sin(0),
lastZ = 1;//FastMath.cos(0);
short c = 2;
x = FastMath.sin(angle);
z = FastMath.cos(angle)+1;
float normVal = 1f/FastMath.sqrt(x*x+z*z);
for (int i = 0; i < sides-1; i++, c+= 6) {
x = center.x+radius * lastX;
z = center.z+radius * lastZ;
for(int j = 0; j < 3; j++) {
fb.put(x).put(center.y+halfHeight).put(z);
fb.put(x).put(center.y-halfHeight).put(z);
}
x = FastMath.sin((i+1)*angle);
z = FastMath.cos((i+1)*angle);
lastX = (lastX+x)*normVal;
lastZ = (lastZ+z)*normVal;
normal.put(lastX).put(0).put(lastZ);
normal.put(lastX).put(0).put(lastZ);
normal.put(0).put(1).put(0);
normal.put(0).put(-1).put(0);
normal.put(lastX).put(0).put(lastZ);
normal.put(lastX).put(0).put(lastZ);
lastX = x;
lastZ = z;
//index for sides
idx.put(c).put((short) (c+1)).put((short) (c+4));
idx.put((short) (c+5)).put((short) (c+4)).put((short) (c+1));
}
x = center.x+radius * lastX;
z = center.z+radius * lastZ;
for(int j = 0; j < 3; j++) {
fb.put(x).put(center.y+halfHeight).put(z);
fb.put(x).put(center.y-halfHeight).put(z);
}
x = 0;//FastMath.sin(0);
z = 1;//FastMath.cos(0);
lastX = (lastX+x)*normVal;
lastZ = (lastZ+z)*normVal;
normal.put(lastX).put(0).put(lastZ);
normal.put(lastX).put(0).put(lastZ);
normal.put(0).put(1).put(0);
normal.put(0).put(-1).put(0);
normal.flip();
normal.put(lastX).put(0).put(lastZ);
normal.put(lastX).put(0).put(lastZ);
idx.put(c).put((short) (c+1)).put((short)0);
idx.put((short) 1).put((short) 0).put((short) (c+1));
c = 10; //index for caps
for (int i = 0; i < sides-2; i++, c += 6) {
idx.put((short)4).put((short) (c)).put((short) (c+6));
idx.put((short)5).put((short) (c+7)).put((short) (c+1));
}
normal.rewind();
fb.flip();
idx.flip();
setBuffer(VertexBuffer.Type.Position, 3, fb);
setBuffer(VertexBuffer.Type.Index, 3, idx);
setBuffer(VertexBuffer.Type.Normal, 3, normal);
updateBound();
}
}