use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.
the class TestCylinder method simpleInitApp.
@Override
public void simpleInitApp() {
Cylinder t = new Cylinder(20, 50, 1, 2, true);
Geometry geom = new Geometry("Cylinder", t);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key = new TextureKey("Interface/Logo/Monkey.jpg", true);
key.setGenerateMips(true);
Texture tex = assetManager.loadTexture(key);
tex.setMinFilter(Texture.MinFilter.Trilinear);
mat.setTexture("ColorMap", tex);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.
the class Cylinder method updateGeometry.
/**
* Rebuilds the cylinder based on a new set of parameters.
*
* @param axisSamples the number of samples along the axis.
* @param radialSamples the number of samples around the radial.
* @param radius the radius of the bottom of the cylinder.
* @param radius2 the radius of the top of the cylinder.
* @param height the cylinder's height.
* @param closed should the cylinder have top and bottom surfaces.
* @param inverted is the cylinder is meant to be viewed from the inside.
*/
public void updateGeometry(int axisSamples, int radialSamples, float radius, float radius2, float height, boolean closed, boolean inverted) {
this.axisSamples = axisSamples;
this.radialSamples = radialSamples;
this.radius = radius;
this.radius2 = radius2;
this.height = height;
this.closed = closed;
this.inverted = inverted;
// VertexBuffer pvb = getBuffer(Type.Position);
// VertexBuffer nvb = getBuffer(Type.Normal);
// VertexBuffer tvb = getBuffer(Type.TexCoord);
axisSamples += (closed ? 2 : 0);
// Vertices
int vertCount = axisSamples * (radialSamples + 1) + (closed ? 2 : 0);
setBuffer(Type.Position, 3, createVector3Buffer(getFloatBuffer(Type.Position), vertCount));
// Normals
setBuffer(Type.Normal, 3, createVector3Buffer(getFloatBuffer(Type.Normal), vertCount));
// Texture co-ordinates
setBuffer(Type.TexCoord, 2, createVector2Buffer(vertCount));
int triCount = ((closed ? 2 : 0) + 2 * (axisSamples - 1)) * radialSamples;
setBuffer(Type.Index, 3, createShortBuffer(getShortBuffer(Type.Index), 3 * triCount));
// generate geometry
float inverseRadial = 1.0f / radialSamples;
float inverseAxisLess = 1.0f / (closed ? axisSamples - 3 : axisSamples - 1);
float inverseAxisLessTexture = 1.0f / (axisSamples - 1);
float halfHeight = 0.5f * height;
// Generate points on the unit circle to be used in computing the mesh
// points on a cylinder slice.
float[] sin = new float[radialSamples + 1];
float[] cos = new float[radialSamples + 1];
for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
float angle = FastMath.TWO_PI * inverseRadial * radialCount;
cos[radialCount] = FastMath.cos(angle);
sin[radialCount] = FastMath.sin(angle);
}
sin[radialSamples] = sin[0];
cos[radialSamples] = cos[0];
// calculate normals
Vector3f[] vNormals = null;
Vector3f vNormal = Vector3f.UNIT_Z;
if ((height != 0.0f) && (radius != radius2)) {
vNormals = new Vector3f[radialSamples];
Vector3f vHeight = Vector3f.UNIT_Z.mult(height);
Vector3f vRadial = new Vector3f();
for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
vRadial.set(cos[radialCount], sin[radialCount], 0.0f);
Vector3f vRadius = vRadial.mult(radius);
Vector3f vRadius2 = vRadial.mult(radius2);
Vector3f vMantle = vHeight.subtract(vRadius2.subtract(vRadius));
Vector3f vTangent = vRadial.cross(Vector3f.UNIT_Z);
vNormals[radialCount] = vMantle.cross(vTangent).normalize();
}
}
FloatBuffer nb = getFloatBuffer(Type.Normal);
FloatBuffer pb = getFloatBuffer(Type.Position);
FloatBuffer tb = getFloatBuffer(Type.TexCoord);
// generate the cylinder itself
Vector3f tempNormal = new Vector3f();
for (int axisCount = 0, i = 0; axisCount < axisSamples; axisCount++, i++) {
float axisFraction;
float axisFractionTexture;
int topBottom = 0;
if (!closed) {
// in [0,1]
axisFraction = axisCount * inverseAxisLess;
axisFractionTexture = axisFraction;
} else {
if (axisCount == 0) {
// bottom
topBottom = -1;
axisFraction = 0;
axisFractionTexture = inverseAxisLessTexture;
} else if (axisCount == axisSamples - 1) {
// top
topBottom = 1;
axisFraction = 1;
axisFractionTexture = 1 - inverseAxisLessTexture;
} else {
axisFraction = (axisCount - 1) * inverseAxisLess;
axisFractionTexture = axisCount * inverseAxisLessTexture;
}
}
// compute center of slice
float z = -halfHeight + height * axisFraction;
Vector3f sliceCenter = new Vector3f(0, 0, z);
// compute slice vertices with duplication at end point
int save = i;
for (int radialCount = 0; radialCount < radialSamples; radialCount++, i++) {
// in [0,1)
float radialFraction = radialCount * inverseRadial;
tempNormal.set(cos[radialCount], sin[radialCount], 0.0f);
if (vNormals != null) {
vNormal = vNormals[radialCount];
} else if (radius == radius2) {
vNormal = tempNormal;
}
if (topBottom == 0) {
if (!inverted)
nb.put(vNormal.x).put(vNormal.y).put(vNormal.z);
else
nb.put(-vNormal.x).put(-vNormal.y).put(-vNormal.z);
} else {
nb.put(0).put(0).put(topBottom * (inverted ? -1 : 1));
}
tempNormal.multLocal((radius - radius2) * axisFraction + radius2).addLocal(sliceCenter);
pb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
tb.put((inverted ? 1 - radialFraction : radialFraction)).put(axisFractionTexture);
}
BufferUtils.copyInternalVector3(pb, save, i);
BufferUtils.copyInternalVector3(nb, save, i);
tb.put((inverted ? 0.0f : 1.0f)).put(axisFractionTexture);
}
if (closed) {
// bottom center
pb.put(0).put(0).put(-halfHeight);
nb.put(0).put(0).put(-1 * (inverted ? -1 : 1));
tb.put(0.5f).put(0);
// top center
pb.put(0).put(0).put(halfHeight);
nb.put(0).put(0).put(1 * (inverted ? -1 : 1));
tb.put(0.5f).put(1);
}
IndexBuffer ib = getIndexBuffer();
int index = 0;
// Connectivity
for (int axisCount = 0, axisStart = 0; axisCount < axisSamples - 1; axisCount++) {
int i0 = axisStart;
int i1 = i0 + 1;
axisStart += radialSamples + 1;
int i2 = axisStart;
int i3 = i2 + 1;
for (int i = 0; i < radialSamples; i++) {
if (closed && axisCount == 0) {
if (!inverted) {
ib.put(index++, i0++);
ib.put(index++, vertCount - 2);
ib.put(index++, i1++);
} else {
ib.put(index++, i0++);
ib.put(index++, i1++);
ib.put(index++, vertCount - 2);
}
} else if (closed && axisCount == axisSamples - 2) {
ib.put(index++, i2++);
ib.put(index++, inverted ? vertCount - 1 : i3++);
ib.put(index++, inverted ? i3++ : vertCount - 1);
} else {
ib.put(index++, i0++);
ib.put(index++, inverted ? i2 : i1);
ib.put(index++, inverted ? i1 : i2);
ib.put(index++, i1++);
ib.put(index++, inverted ? i2++ : i3++);
ib.put(index++, inverted ? i3++ : i2++);
}
}
}
updateBound();
setStatic();
}
use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.
the class Torus method setGeometryData.
private void setGeometryData() {
// allocate vertices
int vertCount = (circleSamples + 1) * (radialSamples + 1);
FloatBuffer fpb = BufferUtils.createVector3Buffer(vertCount);
setBuffer(Type.Position, 3, fpb);
// allocate normals if requested
FloatBuffer fnb = BufferUtils.createVector3Buffer(vertCount);
setBuffer(Type.Normal, 3, fnb);
// allocate texture coordinates
FloatBuffer ftb = BufferUtils.createVector2Buffer(vertCount);
setBuffer(Type.TexCoord, 2, ftb);
// generate geometry
float inverseCircleSamples = 1.0f / circleSamples;
float inverseRadialSamples = 1.0f / radialSamples;
int i = 0;
// generate the cylinder itself
Vector3f radialAxis = new Vector3f(), torusMiddle = new Vector3f(), tempNormal = new Vector3f();
for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
// compute center point on torus circle at specified angle
float circleFraction = circleCount * inverseCircleSamples;
float theta = FastMath.TWO_PI * circleFraction;
float cosTheta = FastMath.cos(theta);
float sinTheta = FastMath.sin(theta);
radialAxis.set(cosTheta, sinTheta, 0);
radialAxis.mult(outerRadius, torusMiddle);
// compute slice vertices with duplication at end point
int iSave = i;
for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
float radialFraction = radialCount * inverseRadialSamples;
// in [0,1)
float phi = FastMath.TWO_PI * radialFraction;
float cosPhi = FastMath.cos(phi);
float sinPhi = FastMath.sin(phi);
tempNormal.set(radialAxis).multLocal(cosPhi);
tempNormal.z += sinPhi;
fnb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
tempNormal.multLocal(innerRadius).addLocal(torusMiddle);
fpb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
ftb.put(radialFraction).put(circleFraction);
i++;
}
BufferUtils.copyInternalVector3(fpb, iSave, i);
BufferUtils.copyInternalVector3(fnb, iSave, i);
ftb.put(1.0f).put(circleFraction);
i++;
}
// duplicate the cylinder ends to form a torus
for (int iR = 0; iR <= radialSamples; iR++, i++) {
BufferUtils.copyInternalVector3(fpb, iR, i);
BufferUtils.copyInternalVector3(fnb, iR, i);
BufferUtils.copyInternalVector2(ftb, iR, i);
ftb.put(i * 2 + 1, 1.0f);
}
}
use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.
the class TestConeVSFrustum method simpleInitApp.
@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
frustumCam = cam.clone();
frustumCam.setFrustumFar(25);
Vector3f[] points = new Vector3f[8];
for (int i = 0; i < 8; i++) {
points[i] = new Vector3f();
}
ShadowUtil.updateFrustumPoints2(frustumCam, points);
WireFrustum frustumShape = new WireFrustum(points);
Geometry frustum = new Geometry("frustum", frustumShape);
frustum.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
rootNode.attachChild(frustum);
rootNode.addLight(new DirectionalLight());
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.2f));
rootNode.addLight(al);
Grid grid = new Grid(50, 50, 5);
Geometry gridGeom = new Geometry("grid", grid);
gridGeom.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
gridGeom.getMaterial().setColor("Color", ColorRGBA.Gray);
rootNode.attachChild(gridGeom);
gridGeom.setLocalTranslation(-125, -25, -125);
// flyCam.setMoveSpeed(30);
// flyCam.setDragToRotate(true);
// cam.setLocation(new Vector3f(56.182674f, 19.037334f, 7.093905f));
// cam.setRotation(new Quaternion(0.0816657f, -0.82228005f, 0.12213967f, 0.5497892f));
spotLight = new SpotLight();
spotLight.setSpotRange(25);
spotLight.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD);
float radius = FastMath.tan(spotLight.getSpotOuterAngle()) * spotLight.getSpotRange();
Cylinder cylinder = new Cylinder(5, 16, 0, radius, spotLight.getSpotRange(), true, false);
geom = new Geometry("light", cylinder);
geom.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
geom.getMaterial().setColor("Diffuse", ColorRGBA.White);
geom.getMaterial().setColor("Ambient", ColorRGBA.DarkGray);
geom.getMaterial().setBoolean("UseMaterialColors", true);
final LightNode ln = new LightNode("lb", spotLight);
ln.attachChild(geom);
geom.setLocalTranslation(0, -spotLight.getSpotRange() / 2f, 0);
geom.rotate(-FastMath.HALF_PI, 0, 0);
rootNode.attachChild(ln);
// ln.rotate(FastMath.QUARTER_PI, 0, 0);
// ln.setLocalTranslation(0, 0, -16);
inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addMapping("shift", new KeyTrigger(KeyInput.KEY_LSHIFT), new KeyTrigger(KeyInput.KEY_RSHIFT));
inputManager.addMapping("middleClick", new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
inputManager.addMapping("up", new MouseAxisTrigger(MouseInput.AXIS_Y, false));
inputManager.addMapping("down", new MouseAxisTrigger(MouseInput.AXIS_Y, true));
inputManager.addMapping("left", new MouseAxisTrigger(MouseInput.AXIS_X, true));
inputManager.addMapping("right", new MouseAxisTrigger(MouseInput.AXIS_X, false));
final Node camTarget = new Node("CamTarget");
rootNode.attachChild(camTarget);
ChaseCameraAppState chaser = new ChaseCameraAppState();
chaser.setTarget(camTarget);
chaser.setMaxDistance(150);
chaser.setDefaultDistance(70);
chaser.setDefaultHorizontalRotation(FastMath.HALF_PI);
chaser.setMinVerticalRotation(-FastMath.PI);
chaser.setMaxVerticalRotation(FastMath.PI * 2);
chaser.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
stateManager.attach(chaser);
flyCam.setEnabled(false);
inputManager.addListener(new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
Spatial s = null;
float mult = 1;
if (moving) {
s = ln;
}
if (panning) {
s = camTarget;
mult = -1;
}
if ((moving || panning) && s != null) {
if (shift) {
if (name.equals("left")) {
tmp.set(cam.getDirection());
s.rotate(tmpQuat.fromAngleAxis(value, tmp));
}
if (name.equals("right")) {
tmp.set(cam.getDirection());
s.rotate(tmpQuat.fromAngleAxis(-value, tmp));
}
} else {
value *= MOVE_SPEED * mult;
if (name.equals("up")) {
tmp.set(cam.getUp()).multLocal(value);
s.move(tmp);
}
if (name.equals("down")) {
tmp.set(cam.getUp()).multLocal(-value);
s.move(tmp);
}
if (name.equals("left")) {
tmp.set(cam.getLeft()).multLocal(value);
s.move(tmp);
}
if (name.equals("right")) {
tmp.set(cam.getLeft()).multLocal(-value);
s.move(tmp);
}
}
}
}
}, "up", "down", "left", "right");
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("click")) {
if (isPressed) {
moving = true;
} else {
moving = false;
}
}
if (name.equals("middleClick")) {
if (isPressed) {
panning = true;
} else {
panning = false;
}
}
if (name.equals("shift")) {
if (isPressed) {
shift = true;
} else {
shift = false;
}
}
}
}, "click", "middleClick", "shift");
/**
* An unshaded textured cube. // * Uses texture from jme3-test-data
* library!
*/
Box boxMesh = new Box(1f, 1f, 1f);
boxGeo = new Geometry("A Textured Box", boxMesh);
Material boxMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture monkeyTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
boxMat.setTexture("ColorMap", monkeyTex);
boxGeo.setMaterial(boxMat);
// rootNode.attachChild(boxGeo);
//
//boxGeo2 = boxGeo.clone();
//rootNode.attachChild(boxGeo2);
System.err.println("light " + spotLight.getPosition());
}
use of com.jme3.scene.shape.Cylinder in project jmonkeyengine by jMonkeyEngine.
the class TestPhysicsCar method buildPlayer.
private void buildPlayer() {
Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
mat.getAdditionalRenderState().setWireframe(true);
mat.setColor("Color", ColorRGBA.Red);
//create a compound shape and attach the BoxCollisionShape for the car body at 0,1,0
//this shifts the effective center of mass of the BoxCollisionShape to 0,-1,0
CompoundCollisionShape compoundShape = new CompoundCollisionShape();
BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.2f, 0.5f, 2.4f));
compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
//create vehicle node
Node vehicleNode = new Node("vehicleNode");
vehicle = new VehicleControl(compoundShape, 400);
vehicleNode.addControl(vehicle);
//setting suspension values for wheels, this can be a bit tricky
//see also https://docs.google.com/Doc?docid=0AXVUZ5xw6XpKZGNuZG56a3FfMzU0Z2NyZnF4Zmo&hl=en
//200=f1 car
float stiffness = 60.0f;
//(should be lower than damp)
float compValue = .3f;
float dampValue = .4f;
vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
vehicle.setSuspensionStiffness(stiffness);
vehicle.setMaxSuspensionForce(10000.0f);
//Create four wheels and add them at their locations
// was 0, -1, 0
Vector3f wheelDirection = new Vector3f(0, -1, 0);
// was -1, 0, 0
Vector3f wheelAxle = new Vector3f(-1, 0, 0);
float radius = 0.5f;
float restLength = 0.3f;
float yOff = 0.5f;
float xOff = 1f;
float zOff = 2f;
Cylinder wheelMesh = new Cylinder(16, 16, radius, radius * 0.6f, true);
Node node1 = new Node("wheel 1 node");
Geometry wheels1 = new Geometry("wheel 1", wheelMesh);
node1.attachChild(wheels1);
wheels1.rotate(0, FastMath.HALF_PI, 0);
wheels1.setMaterial(mat);
vehicle.addWheel(node1, new Vector3f(-xOff, yOff, zOff), wheelDirection, wheelAxle, restLength, radius, true);
Node node2 = new Node("wheel 2 node");
Geometry wheels2 = new Geometry("wheel 2", wheelMesh);
node2.attachChild(wheels2);
wheels2.rotate(0, FastMath.HALF_PI, 0);
wheels2.setMaterial(mat);
vehicle.addWheel(node2, new Vector3f(xOff, yOff, zOff), wheelDirection, wheelAxle, restLength, radius, true);
Node node3 = new Node("wheel 3 node");
Geometry wheels3 = new Geometry("wheel 3", wheelMesh);
node3.attachChild(wheels3);
wheels3.rotate(0, FastMath.HALF_PI, 0);
wheels3.setMaterial(mat);
vehicle.addWheel(node3, new Vector3f(-xOff, yOff, -zOff), wheelDirection, wheelAxle, restLength, radius, false);
Node node4 = new Node("wheel 4 node");
Geometry wheels4 = new Geometry("wheel 4", wheelMesh);
node4.attachChild(wheels4);
wheels4.rotate(0, FastMath.HALF_PI, 0);
wheels4.setMaterial(mat);
vehicle.addWheel(node4, new Vector3f(xOff, yOff, -zOff), wheelDirection, wheelAxle, restLength, radius, false);
vehicleNode.attachChild(node1);
vehicleNode.attachChild(node2);
vehicleNode.attachChild(node3);
vehicleNode.attachChild(node4);
rootNode.attachChild(vehicleNode);
getPhysicsSpace().add(vehicle);
}
Aggregations