use of net.drewke.tdme.engine.primitives.BoundingVolume in project tdme by andreasdr.
the class ModelMetaDataFileExport method exportToJSON.
/**
* Export model meta data file to JSON node
* @param entity
*/
public static JSONObject exportToJSON(LevelEditorEntity entity) throws Exception {
JSONObject jEntityRoot = new JSONObject();
// re-convert to tm if having a model file
if (entity.getType() == EntityType.MODEL && entity.getFileName() != null) {
String modelPathName = Tools.getPath(entity.getFileName());
String modelFileName = Tools.getFileName(entity.getFileName()) + (entity.getFileName().endsWith(".tm") == false ? ".tm" : "");
TMWriter.write(entity.getModel(), modelPathName, modelFileName);
jEntityRoot.put("file", modelPathName + "/" + modelFileName);
// try to copy thumbnail
try {
String thumbnail = modelFileName + ".png";
jEntityRoot.put("thumbnail", thumbnail);
copyFile(new File("./tmp", entity.getThumbnail()), new File(Tools.getPath(entity.getFileName()), thumbnail));
} catch (IOException ioe) {
System.out.println("ModelMetaDataFileExport::export(): Could not copy thumbnail for '" + entity.getFileName() + "'");
}
}
// general data
jEntityRoot.put("version", "0.99");
jEntityRoot.put("type", entity.getType());
jEntityRoot.put("name", entity.getName());
jEntityRoot.put("descr", entity.getDescription());
jEntityRoot.put("px", entity.getPivot().getX());
jEntityRoot.put("py", entity.getPivot().getY());
jEntityRoot.put("pz", entity.getPivot().getZ());
// export particle system
if (entity.getType() == EntityType.PARTICLESYSTEM) {
LevelEditorEntityParticleSystem particleSystem = entity.getParticleSystem();
JSONObject jParticleSystem = new JSONObject();
// type
jParticleSystem.put("t", particleSystem.getType().toString());
switch(particleSystem.getType()) {
case NONE:
{
break;
}
case OBJECT_PARTICLE_SYSTEM:
{
JSONObject jObjectParticleSystem = new JSONObject();
// do we have a model file name?
if (particleSystem.getObjectParticleSystem().getModelFile() != null && particleSystem.getObjectParticleSystem().getModelFile().length() > 0) {
// yep, convert to .tm
String modelPathName = Tools.getPath(particleSystem.getObjectParticleSystem().getModelFile());
String modelFileName = Tools.getFileName(particleSystem.getObjectParticleSystem().getModelFile() + (particleSystem.getObjectParticleSystem().getModelFile().endsWith(".tm") == false ? ".tm" : ""));
TMWriter.write(particleSystem.getObjectParticleSystem().getModel(), modelPathName, modelFileName);
// and store to file
particleSystem.getObjectParticleSystem().setModelFile(modelPathName + "/" + modelFileName);
}
jObjectParticleSystem.put("mc", particleSystem.getObjectParticleSystem().getMaxCount());
jObjectParticleSystem.put("sx", particleSystem.getObjectParticleSystem().getScale().getX());
jObjectParticleSystem.put("sy", particleSystem.getObjectParticleSystem().getScale().getY());
jObjectParticleSystem.put("sz", particleSystem.getObjectParticleSystem().getScale().getZ());
jObjectParticleSystem.put("mf", particleSystem.getObjectParticleSystem().getModelFile());
jObjectParticleSystem.put("ae", particleSystem.getObjectParticleSystem().isAutoEmit());
jParticleSystem.put("ops", jObjectParticleSystem);
break;
}
case POINT_PARTICLE_SYSTEM:
{
JSONObject jPointParticleSystem = new JSONObject();
jPointParticleSystem.put("mp", particleSystem.getPointParticleSystem().getMaxPoints());
jPointParticleSystem.put("ae", particleSystem.getPointParticleSystem().isAutoEmit());
jParticleSystem.put("pps", jPointParticleSystem);
break;
}
default:
{
System.out.println("ModelMetaDataFileExport::export(): unknown particle system type '" + particleSystem.getType() + "'");
break;
}
}
// emitter
jParticleSystem.put("e", particleSystem.getEmitter().toString());
switch(particleSystem.getEmitter()) {
case NONE:
{
break;
}
case POINT_PARTICLE_EMITTER:
{
JSONObject jPointParticleEmitter = new JSONObject();
PointParticleEmitter emitter = particleSystem.getPointParticleEmitter();
jPointParticleEmitter.put("c", emitter.getCount());
jPointParticleEmitter.put("lt", emitter.getLifeTime());
jPointParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
jPointParticleEmitter.put("m", emitter.getMass());
jPointParticleEmitter.put("mrnd", emitter.getMassRnd());
jPointParticleEmitter.put("px", emitter.getPosition().getX());
jPointParticleEmitter.put("py", emitter.getPosition().getY());
jPointParticleEmitter.put("pz", emitter.getPosition().getZ());
jPointParticleEmitter.put("vx", emitter.getVelocity().getX());
jPointParticleEmitter.put("vy", emitter.getVelocity().getY());
jPointParticleEmitter.put("vz", emitter.getVelocity().getZ());
jPointParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
jPointParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
jPointParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
jPointParticleEmitter.put("csr", emitter.getColorStart().getRed());
jPointParticleEmitter.put("csg", emitter.getColorStart().getGreen());
jPointParticleEmitter.put("csb", emitter.getColorStart().getBlue());
jPointParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
jPointParticleEmitter.put("cer", emitter.getColorEnd().getRed());
jPointParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
jPointParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
jPointParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
jParticleSystem.put("ppe", jPointParticleEmitter);
break;
}
case BOUNDINGBOX_PARTICLE_EMITTER:
{
JSONObject jBoundingBoxParticleEmitter = new JSONObject();
BoundingBoxParticleEmitter emitter = particleSystem.getBoundingBoxParticleEmitters();
jBoundingBoxParticleEmitter.put("c", emitter.getCount());
jBoundingBoxParticleEmitter.put("lt", emitter.getLifeTime());
jBoundingBoxParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
jBoundingBoxParticleEmitter.put("m", emitter.getMass());
jBoundingBoxParticleEmitter.put("mrnd", emitter.getMassRnd());
jBoundingBoxParticleEmitter.put("vx", emitter.getVelocity().getX());
jBoundingBoxParticleEmitter.put("vy", emitter.getVelocity().getY());
jBoundingBoxParticleEmitter.put("vz", emitter.getVelocity().getZ());
jBoundingBoxParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
jBoundingBoxParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
jBoundingBoxParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
jBoundingBoxParticleEmitter.put("csr", emitter.getColorStart().getRed());
jBoundingBoxParticleEmitter.put("csg", emitter.getColorStart().getGreen());
jBoundingBoxParticleEmitter.put("csb", emitter.getColorStart().getBlue());
jBoundingBoxParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
jBoundingBoxParticleEmitter.put("cer", emitter.getColorEnd().getRed());
jBoundingBoxParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
jBoundingBoxParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
jBoundingBoxParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
jBoundingBoxParticleEmitter.put("ocx", emitter.getObbCenter().getX());
jBoundingBoxParticleEmitter.put("ocy", emitter.getObbCenter().getY());
jBoundingBoxParticleEmitter.put("ocz", emitter.getObbCenter().getZ());
jBoundingBoxParticleEmitter.put("ohex", emitter.getObbHalfextension().getX());
jBoundingBoxParticleEmitter.put("ohey", emitter.getObbHalfextension().getY());
jBoundingBoxParticleEmitter.put("ohez", emitter.getObbHalfextension().getZ());
jBoundingBoxParticleEmitter.put("oa0x", emitter.getObbAxis0().getX());
jBoundingBoxParticleEmitter.put("oa0y", emitter.getObbAxis0().getY());
jBoundingBoxParticleEmitter.put("oa0z", emitter.getObbAxis0().getZ());
jBoundingBoxParticleEmitter.put("oa1x", emitter.getObbAxis1().getX());
jBoundingBoxParticleEmitter.put("oa1y", emitter.getObbAxis1().getY());
jBoundingBoxParticleEmitter.put("oa1z", emitter.getObbAxis1().getZ());
jBoundingBoxParticleEmitter.put("oa2x", emitter.getObbAxis2().getX());
jBoundingBoxParticleEmitter.put("oa2y", emitter.getObbAxis2().getY());
jBoundingBoxParticleEmitter.put("oa2z", emitter.getObbAxis2().getZ());
jParticleSystem.put("bbpe", jBoundingBoxParticleEmitter);
break;
}
case CIRCLE_PARTICLE_EMITTER:
{
JSONObject jCircleParticleEmitter = new JSONObject();
CircleParticleEmitter emitter = particleSystem.getCircleParticleEmitter();
jCircleParticleEmitter.put("c", emitter.getCount());
jCircleParticleEmitter.put("lt", emitter.getLifeTime());
jCircleParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
jCircleParticleEmitter.put("m", emitter.getMass());
jCircleParticleEmitter.put("mrnd", emitter.getMassRnd());
jCircleParticleEmitter.put("vx", emitter.getVelocity().getX());
jCircleParticleEmitter.put("vy", emitter.getVelocity().getY());
jCircleParticleEmitter.put("vz", emitter.getVelocity().getZ());
jCircleParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
jCircleParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
jCircleParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
jCircleParticleEmitter.put("csr", emitter.getColorStart().getRed());
jCircleParticleEmitter.put("csg", emitter.getColorStart().getGreen());
jCircleParticleEmitter.put("csb", emitter.getColorStart().getBlue());
jCircleParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
jCircleParticleEmitter.put("cer", emitter.getColorEnd().getRed());
jCircleParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
jCircleParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
jCircleParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
jCircleParticleEmitter.put("cx", emitter.getCenter().getX());
jCircleParticleEmitter.put("cy", emitter.getCenter().getY());
jCircleParticleEmitter.put("cz", emitter.getCenter().getZ());
jCircleParticleEmitter.put("r", emitter.getRadius());
jCircleParticleEmitter.put("a0x", emitter.getAxis0().getX());
jCircleParticleEmitter.put("a0y", emitter.getAxis0().getY());
jCircleParticleEmitter.put("a0z", emitter.getAxis0().getZ());
jCircleParticleEmitter.put("a1x", emitter.getAxis1().getX());
jCircleParticleEmitter.put("a1y", emitter.getAxis1().getY());
jCircleParticleEmitter.put("a1z", emitter.getAxis1().getZ());
jParticleSystem.put("cpe", jCircleParticleEmitter);
break;
}
case CIRCLE_PARTICLE_EMITTER_PLANE_VELOCITY:
{
JSONObject jCircleParticleEmitterPlaneVelocity = new JSONObject();
CircleParticleEmitterPlaneVelocity emitter = particleSystem.getCircleParticleEmitterPlaneVelocity();
jCircleParticleEmitterPlaneVelocity.put("c", emitter.getCount());
jCircleParticleEmitterPlaneVelocity.put("lt", emitter.getLifeTime());
jCircleParticleEmitterPlaneVelocity.put("ltrnd", emitter.getLifeTimeRnd());
jCircleParticleEmitterPlaneVelocity.put("m", emitter.getMass());
jCircleParticleEmitterPlaneVelocity.put("mrnd", emitter.getMassRnd());
jCircleParticleEmitterPlaneVelocity.put("v", emitter.getVelocity());
jCircleParticleEmitterPlaneVelocity.put("vrnd", emitter.getVelocityRnd());
jCircleParticleEmitterPlaneVelocity.put("csr", emitter.getColorStart().getRed());
jCircleParticleEmitterPlaneVelocity.put("csg", emitter.getColorStart().getGreen());
jCircleParticleEmitterPlaneVelocity.put("csb", emitter.getColorStart().getBlue());
jCircleParticleEmitterPlaneVelocity.put("csa", emitter.getColorStart().getAlpha());
jCircleParticleEmitterPlaneVelocity.put("cer", emitter.getColorEnd().getRed());
jCircleParticleEmitterPlaneVelocity.put("ceg", emitter.getColorEnd().getGreen());
jCircleParticleEmitterPlaneVelocity.put("ceb", emitter.getColorEnd().getBlue());
jCircleParticleEmitterPlaneVelocity.put("cea", emitter.getColorEnd().getAlpha());
jCircleParticleEmitterPlaneVelocity.put("cx", emitter.getCenter().getX());
jCircleParticleEmitterPlaneVelocity.put("cy", emitter.getCenter().getY());
jCircleParticleEmitterPlaneVelocity.put("cz", emitter.getCenter().getZ());
jCircleParticleEmitterPlaneVelocity.put("r", emitter.getRadius());
jCircleParticleEmitterPlaneVelocity.put("a0x", emitter.getAxis0().getX());
jCircleParticleEmitterPlaneVelocity.put("a0y", emitter.getAxis0().getY());
jCircleParticleEmitterPlaneVelocity.put("a0z", emitter.getAxis0().getZ());
jCircleParticleEmitterPlaneVelocity.put("a1x", emitter.getAxis1().getX());
jCircleParticleEmitterPlaneVelocity.put("a1y", emitter.getAxis1().getY());
jCircleParticleEmitterPlaneVelocity.put("a1z", emitter.getAxis1().getZ());
jParticleSystem.put("cpeev", jCircleParticleEmitterPlaneVelocity);
break;
}
case SPHERE_PARTICLE_EMITTER:
{
JSONObject jSphereParticleEmitter = new JSONObject();
SphereParticleEmitter emitter = particleSystem.getSphereParticleEmitter();
jSphereParticleEmitter.put("c", emitter.getCount());
jSphereParticleEmitter.put("lt", emitter.getLifeTime());
jSphereParticleEmitter.put("ltrnd", emitter.getLifeTimeRnd());
jSphereParticleEmitter.put("m", emitter.getMass());
jSphereParticleEmitter.put("mrnd", emitter.getMassRnd());
jSphereParticleEmitter.put("vx", emitter.getVelocity().getX());
jSphereParticleEmitter.put("vy", emitter.getVelocity().getY());
jSphereParticleEmitter.put("vz", emitter.getVelocity().getZ());
jSphereParticleEmitter.put("vrndx", emitter.getVelocityRnd().getX());
jSphereParticleEmitter.put("vrndy", emitter.getVelocityRnd().getY());
jSphereParticleEmitter.put("vrndz", emitter.getVelocityRnd().getZ());
jSphereParticleEmitter.put("csr", emitter.getColorStart().getRed());
jSphereParticleEmitter.put("csg", emitter.getColorStart().getGreen());
jSphereParticleEmitter.put("csb", emitter.getColorStart().getBlue());
jSphereParticleEmitter.put("csa", emitter.getColorStart().getAlpha());
jSphereParticleEmitter.put("cer", emitter.getColorEnd().getRed());
jSphereParticleEmitter.put("ceg", emitter.getColorEnd().getGreen());
jSphereParticleEmitter.put("ceb", emitter.getColorEnd().getBlue());
jSphereParticleEmitter.put("cea", emitter.getColorEnd().getAlpha());
jSphereParticleEmitter.put("cx", emitter.getCenter().getX());
jSphereParticleEmitter.put("cy", emitter.getCenter().getY());
jSphereParticleEmitter.put("cz", emitter.getCenter().getZ());
jSphereParticleEmitter.put("r", emitter.getRadius());
jParticleSystem.put("spe", jSphereParticleEmitter);
break;
}
default:
System.out.println("ModelMetaDataFileExport::export(): unknown particle system emitter '" + particleSystem.getEmitter() + "'");
}
// add to file
jEntityRoot.put("ps", jParticleSystem);
}
// bounding volume
JSONArray jBoundingVolumes = new JSONArray();
for (int i = 0; i < entity.getBoundingVolumeCount(); i++) {
LevelEditorEntityBoundingVolume entityBoundingVolume = entity.getBoundingVolumeAt(i);
BoundingVolume bv = entityBoundingVolume.getBoundingVolume();
if (bv == null)
continue;
JSONObject jBoundingVolume = new JSONObject();
if (bv == null) {
jBoundingVolume.put("type", "none");
jBoundingVolumes.put(jBoundingVolume);
} else if (bv instanceof Sphere) {
Sphere sphere = (Sphere) bv;
jBoundingVolume.put("type", "sphere");
jBoundingVolume.put("cx", sphere.getCenter().getX());
jBoundingVolume.put("cy", sphere.getCenter().getY());
jBoundingVolume.put("cz", sphere.getCenter().getZ());
jBoundingVolume.put("r", sphere.getRadius());
jBoundingVolumes.put(jBoundingVolume);
} else if (bv instanceof Capsule) {
Capsule capsule = (Capsule) bv;
jBoundingVolume.put("type", "capsule");
jBoundingVolume.put("ax", capsule.getA().getX());
jBoundingVolume.put("ay", capsule.getA().getY());
jBoundingVolume.put("az", capsule.getA().getZ());
jBoundingVolume.put("bx", capsule.getB().getX());
jBoundingVolume.put("by", capsule.getB().getY());
jBoundingVolume.put("bz", capsule.getB().getZ());
jBoundingVolume.put("r", capsule.getRadius());
jBoundingVolumes.put(jBoundingVolume);
} else if (bv instanceof BoundingBox) {
BoundingBox aabb = (BoundingBox) bv;
jBoundingVolume.put("type", "aabb");
jBoundingVolume.put("mix", aabb.getMin().getX());
jBoundingVolume.put("miy", aabb.getMin().getY());
jBoundingVolume.put("miz", aabb.getMin().getZ());
jBoundingVolume.put("max", aabb.getMax().getX());
jBoundingVolume.put("may", aabb.getMax().getY());
jBoundingVolume.put("maz", aabb.getMax().getZ());
jBoundingVolumes.put(jBoundingVolume);
} else if (bv instanceof OrientedBoundingBox) {
OrientedBoundingBox obb = (OrientedBoundingBox) bv;
jBoundingVolume.put("type", "obb");
jBoundingVolume.put("cx", obb.getCenter().getX());
jBoundingVolume.put("cy", obb.getCenter().getY());
jBoundingVolume.put("cz", obb.getCenter().getZ());
jBoundingVolume.put("a0x", obb.getAxes()[0].getX());
jBoundingVolume.put("a0y", obb.getAxes()[0].getY());
jBoundingVolume.put("a0z", obb.getAxes()[0].getZ());
jBoundingVolume.put("a1x", obb.getAxes()[1].getX());
jBoundingVolume.put("a1y", obb.getAxes()[1].getY());
jBoundingVolume.put("a1z", obb.getAxes()[1].getZ());
jBoundingVolume.put("a2x", obb.getAxes()[2].getX());
jBoundingVolume.put("a2y", obb.getAxes()[2].getY());
jBoundingVolume.put("a2z", obb.getAxes()[2].getZ());
jBoundingVolume.put("hex", obb.getHalfExtension().getX());
jBoundingVolume.put("hey", obb.getHalfExtension().getY());
jBoundingVolume.put("hez", obb.getHalfExtension().getZ());
jBoundingVolumes.put(jBoundingVolume);
} else if (bv instanceof ConvexMesh) {
ConvexMesh mesh = (ConvexMesh) bv;
jBoundingVolume.put("type", "convexmesh");
jBoundingVolume.put("file", entityBoundingVolume.getModelMeshFile());
JSONArray jMeshTriangles = new JSONArray();
int triangleIdx = 0;
for (Triangle triangle : mesh.getTriangles()) {
JSONArray jMeshTriangleVertices = new JSONArray();
int vertexIdx = 0;
for (Vector3 vertex : triangle.getVertices()) {
JSONArray jMeshTriangleVertex = new JSONArray();
for (int vcIdx = 0; vcIdx < 3; vcIdx++) {
jMeshTriangleVertex.put(vcIdx, vertex.getArray()[vcIdx]);
}
jMeshTriangleVertices.put(vertexIdx++, jMeshTriangleVertex);
}
jMeshTriangles.put(triangleIdx++, jMeshTriangleVertices);
}
jBoundingVolume.put("t", jMeshTriangles);
jBoundingVolumes.put(jBoundingVolume);
}
}
jEntityRoot.put("bvs", jBoundingVolumes);
// model properties
JSONArray jModelProperties = new JSONArray();
for (PropertyModelClass modelProperty : entity.getProperties()) {
JSONObject jObjectProperty = new JSONObject();
jObjectProperty.put("name", modelProperty.getName());
jObjectProperty.put("value", modelProperty.getValue());
jModelProperties.put(jObjectProperty);
}
jEntityRoot.put("properties", jModelProperties);
//
return jEntityRoot;
}
use of net.drewke.tdme.engine.primitives.BoundingVolume in project tdme by andreasdr.
the class LevelEditorLevel method computeBoundingBox.
/**
* Computes level bounding box
*/
public void computeBoundingBox() {
boolean haveDimension = false;
float left = 0.0f;
float right = 0.0f;
float near = 0.0f;
float far = 0.0f;
float top = 0.0f;
float bottom = 0.0f;
Vector3 bbDimension = new Vector3();
Vector3 bbMin = new Vector3();
Vector3 bbMax = new Vector3();
for (LevelEditorObject levelEditorObject : objects) {
if (levelEditorObject.getEntity().getType() != EntityType.MODEL)
continue;
BoundingBox bv = levelEditorObject.getEntity().getModel().getBoundingBox();
BoundingVolume cbv = bv.clone();
cbv.fromBoundingVolumeWithTransformations(bv, levelEditorObject.getTransformations());
bbDimension.set(cbv.computeDimensionOnAxis(new Vector3(1f, 0f, 0f)), cbv.computeDimensionOnAxis(new Vector3(0f, 1f, 0f)), cbv.computeDimensionOnAxis(new Vector3(0f, 0f, 1f)));
bbDimension.scale(0.5f);
bbMin.set(cbv.getCenter());
bbMin.sub(bbDimension);
bbMax.set(cbv.getCenter());
bbMax.add(bbDimension);
float objectLeft = bbMin.getX();
float objectRight = bbMax.getX();
float objectNear = bbMin.getZ();
float objectFar = bbMax.getZ();
float objectBottom = bbMin.getY();
float objectTop = bbMax.getY();
if (haveDimension == false) {
left = objectLeft;
right = objectRight;
near = objectNear;
far = objectFar;
top = objectTop;
bottom = objectBottom;
haveDimension = true;
} else {
if (objectLeft < left)
left = objectLeft;
if (objectRight > right)
right = objectRight;
if (objectNear < near)
near = objectNear;
if (objectFar > far)
far = objectFar;
if (objectTop > top)
top = objectTop;
if (objectBottom < bottom)
bottom = objectBottom;
}
}
boundingBox.getMin().set(left, bottom, near);
boundingBox.getMax().set(right, top, far);
boundingBox.update();
dimension.setX(right - left);
dimension.setZ(far - near);
dimension.setY(top - bottom);
}
use of net.drewke.tdme.engine.primitives.BoundingVolume in project tdme by andreasdr.
the class PartitionQuadTree method addEntity.
/**
* Adds a object
* @param entity
*/
protected void addEntity(Entity entity) {
// update if already exists
ArrayList<PartitionTreeNode> objectPartitionsVector = objectPartitionNodes.get(entity.getId());
if (objectPartitionsVector != null) {
while (objectPartitionsVector.size() > 0) {
int lastIdx = objectPartitionsVector.size() - 1;
objectPartitionsVector.get(lastIdx).partitionObjects.remove(entity);
objectPartitionsVector.remove(lastIdx);
}
}
// determine max first level partition dimension
// convert to aabb for fast collision tests
BoundingVolume cbv = entity.getBoundingBoxTransformed();
Vector3 center = cbv.getCenter();
halfExtension.set(cbv.computeDimensionOnAxis(sideVector) + 0.2f, cbv.computeDimensionOnAxis(upVector) + 0.2f, cbv.computeDimensionOnAxis(forwardVector) + 0.2f).scale(0.5f);
boundingBox.getMin().set(center);
boundingBox.getMin().sub(halfExtension);
boundingBox.getMax().set(center);
boundingBox.getMax().add(halfExtension);
boundingBox.update();
// find, create root nodes if not exists
int minXPartition = (int) Math.floor(boundingBox.getMin().getX() / PARTITION_SIZE_MAX);
int minYPartition = (int) Math.floor(boundingBox.getMin().getY() / PARTITION_SIZE_MAX);
int minZPartition = (int) Math.floor(boundingBox.getMin().getZ() / PARTITION_SIZE_MAX);
int maxXPartition = (int) Math.floor(boundingBox.getMax().getX() / PARTITION_SIZE_MAX);
int maxYPartition = (int) Math.floor(boundingBox.getMax().getY() / PARTITION_SIZE_MAX);
int maxZPartition = (int) Math.floor(boundingBox.getMax().getZ() / PARTITION_SIZE_MAX);
for (int yPartition = minYPartition; yPartition <= maxYPartition; yPartition++) for (int xPartition = minXPartition; xPartition <= maxXPartition; xPartition++) for (int zPartition = minZPartition; zPartition <= maxZPartition; zPartition++) {
// try to find node by key
key.reset();
key.append(xPartition);
key.append(",");
key.append(yPartition);
key.append(",");
key.append(zPartition);
PartitionTreeNode node = treeRoot.subNodesByCoordinate.get(key);
if (node == null) {
node = createPartition(treeRoot, xPartition, yPartition, zPartition, PARTITION_SIZE_MAX);
}
}
// add entity to tree
addToPartitionTree(entity, boundingBox);
}
use of net.drewke.tdme.engine.primitives.BoundingVolume in project tdme by andreasdr.
the class EngineTest method init.
/*
* (non-Javadoc)
* @see com.jogamp.opengl.GLEventListener#init(com.jogamp.opengl.GLAutoDrawable)
*/
public void init(GLAutoDrawable drawable) {
engine.init(drawable);
if (osEngine == null) {
osEngine = Engine.createOffScreenInstance(drawable, 512, 512);
//
Light osLight0 = osEngine.getLightAt(0);
osLight0.getAmbient().set(1.0f, 1.0f, 1.0f, 1.0f);
osLight0.getDiffuse().set(1.0f, 1.0f, 1.0f, 1.0f);
osLight0.getPosition().set(0.0f, -4f, -4f, 1.0f);
osLight0.getSpotDirection().set(new Vector3(osLight0.getPosition().getArray())).sub(new Vector3(0f, 0f, 0f));
osLight0.setEnabled(true);
// cam
Camera osCam = osEngine.getCamera();
osCam.setZNear(0.10f);
osCam.setZFar(50.00f);
osCam.getLookFrom().set(0f, 4f, -4f);
osCam.getLookAt().set(0f, 0.50f, 0f);
osCam.computeUpVector(osCam.getLookFrom(), osCam.getLookAt(), osCam.getUpVector());
// scene color
osEngine.getSceneColor().set(0.5f, 0.0f, 0.0f, 1.0f);
}
// cam
Camera cam = engine.getCamera();
cam.setZNear(0.10f);
cam.setZFar(50.00f);
cam.getLookFrom().set(0f, 3f, -8f);
cam.getLookAt().set(0f, 0.50f, 0f);
cam.computeUpVector(cam.getLookFrom(), cam.getLookAt(), cam.getUpVector());
// lights
Light light0 = engine.getLightAt(0);
light0.getAmbient().set(1.0f, 1.0f, 1.0f, 1.0f);
light0.getDiffuse().set(0.5f, 0.5f, 0.5f, 1f);
light0.getSpecular().set(1f, 1f, 1f, 1f);
light0.getPosition().set(0f, 20000f, 0f, 1f);
light0.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light0.getPosition().getArray()));
light0.setConstantAttenuation(0.5f);
light0.setLinearAttenuation(0f);
light0.setQuadraticAttenuation(0f);
light0.setSpotExponent(0f);
light0.setSpotCutOff(180f);
light0.setEnabled(true);
Light light1 = engine.getLightAt(1);
light1.getDiffuse().set(1.0f, 0.0f, 0.0f, 1.0f);
light1.getPosition().set(-4.0f, 5.0f, -5.0f, 1.0f);
light1.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light1.getPosition().getArray()));
light1.setEnabled(true);
Light light2 = engine.getLightAt(2);
light2.getDiffuse().set(0.0f, 1.0f, 0.0f, 1.0f);
light2.getPosition().set(+4.0f, 5.0f, 0.0f, 1.0f);
light2.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light2.getPosition().getArray()));
light2.setEnabled(true);
// scene
players = new ArrayList<Object3D>();
playersBoundingVolumeModel = new ArrayList<Object3D>();
playerBoundingVolumesTransformed = new ArrayList<BoundingVolume>();
try {
Model _barrel = DAEReader.read("resources/tests/models/barrel", "barrel.dae");
Object3D barrel = new Object3D("barrel", _barrel);
barrelBoundingVolume = new ConvexMesh(new Object3DModel(_barrel));
barrel.getTranslation().set(1.5f, 0.35f, -2f);
barrel.setDynamicShadowingEnabled(true);
barrel.setEnabled(true);
barrel.update();
barrelBoundingVolumeTransformed = barrelBoundingVolume.clone();
barrelBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(barrelBoundingVolume, barrel);
engine.addEntity(barrel);
// wall
Model _farPlane = createWallModel();
Object3D farPlane = new Object3D("wall", _farPlane);
farPlane.bindDiffuseTexture("wall", "wall", osEngine.getFrameBuffer());
engine.addEntity(farPlane);
//
Model _grass = DAEReader.read("resources/tests/models/grass", "grass.dae");
Object3D grass = new Object3D("ground", _grass);
grass.setEnabled(true);
grass.getScale().set(8f, 1f, 8f);
grass.update();
engine.addEntity(grass);
// players
Model _player = DAEReader.read("resources/tests/models/dummy", "testDummy_textured.DAE");
_player.addAnimationSetup("still", 3, 3, true);
_player.addAnimationSetup("walk", 0, 18, true);
// player bounding volume
// playerBoundingVolume = Sphere.createBoundingVolume(new Vector3(0,90f/130f,0), 90f/130f);
playerBoundingVolume = Capsule.createBoundingVolume(new Vector3(0, 30f / 130f, 0), new Vector3(0, 230f / 130f, 0), 25 / 130f);
// playerBoundingVolume = BoundingBox.createBoundingVolume(new Vector3(-25f/130f, 0, -25f/130f), new Vector3(+25f/130f, 180f/130f, +25f/130f));
/*
playerBoundingVolume = OrientedBoundingBox.createBoundingVolume(
new Vector3(0f, 90f/130f, 0f),
new Vector3(1f, 0f, 0f),
new Vector3(0f, 1f, 0f),
new Vector3(0f, 0f, 1f),
new Vector3(25f/130f, 90f/130f, 25f/130f)
);
*/
playerBoundingVolumeModel = PrimitiveModel.createModel(playerBoundingVolume, "player_bv");
// add player 1
// player
Object3D player1 = new Object3D("player1", _player);
player1.getTranslation().add(new Vector3(-1.50f, 0f, 0f));
player1.setAnimation("still");
player1.getRotations().add(new Rotation(0f, new Vector3(0f, 1f, 0f)));
player1.update();
player1.setEnabled(true);
player1.setPickable(true);
player1.setDynamicShadowingEnabled(true);
engine.addEntity(player1);
// bounding volume transformed
BoundingVolume player1BoundingVolumeTransformed = playerBoundingVolume.clone();
player1BoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player1);
playerBoundingVolumesTransformed.add(player1BoundingVolumeTransformed);
// add to engine
players.add(player1);
// bounding volume
Object3D player1BoundingVolume = new Object3D("player1_bv", playerBoundingVolumeModel);
player1BoundingVolume.fromTransformations(player1);
player1BoundingVolume.setEnabled(true);
playersBoundingVolumeModel.add(player1BoundingVolume);
// engine.addEntity(player1BoundingVolume);
// add player 2
// player
Object3D player2 = new Object3D("player2", _player);
player2.getTranslation().add(new Vector3(1.50f, 0f, 0f));
player2.setAnimation("still");
player2.getRotations().add(new Rotation(0f, new Vector3(0f, 1f, 0f)));
player2.update();
player2.setEnabled(true);
player2.setPickable(true);
player2.setDynamicShadowingEnabled(true);
players.add(player2);
// bounding volume transformed
BoundingVolume player2BoundingVolumeTransformed = playerBoundingVolume.clone();
player2BoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player2);
playerBoundingVolumesTransformed.add(player2BoundingVolumeTransformed);
// add to engine
engine.addEntity(player2);
// bounding volume
Object3D player2BoundingVolume = new Object3D("player2_bv", playerBoundingVolumeModel);
player2BoundingVolume.fromTransformations(player2);
player2BoundingVolume.setEnabled(true);
playersBoundingVolumeModel.add(player2BoundingVolume);
// engine.addEntity(player2BoundingVolume);
// add cube
Model _cube = DAEReader.read("resources/tests/models/test", "cube.dae");
cube = new Object3D("cube", _cube);
cube.getTranslation().add(new Vector3(0f, 0f, 0f));
cube.getScale().set(2f, 2f, 2f);
cube.update();
cube.setPickable(true);
cube.setDynamicShadowingEnabled(true);
cube.setEnabled(true);
cubeBoundingVolume = cube.getBoundingBox();
cubeBoundingVolumeTransformed = cubeBoundingVolume.clone();
cubeBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(cubeBoundingVolume, cube);
engine.addEntity(cube);
//
cubeBoundingVolumeModel = PrimitiveModel.createModel(cubeBoundingVolume, "cube_bv");
// bounding volume
Object3D cubeBoundingVolumeObject3D = new Object3D("cube_bv", cubeBoundingVolumeModel);
cubeBoundingVolumeObject3D.fromTransformations(cube);
cubeBoundingVolumeObject3D.setEnabled(true);
engine.addEntity(cubeBoundingVolumeObject3D);
// wall
Model _wall = DAEReader.read("resources/tests/models/wall", "wall.dae");
Object3D wall0 = new Object3D("wall0", _wall);
wall0.getTranslation().add(new Vector3(-1.00f, 0f, 3.00f));
wall0.update();
wall0.setPickable(true);
wall0.setEnabled(true);
engine.addEntity(wall0);
Object3D wall1 = new Object3D("wall1", _wall);
wall1.getTranslation().add(new Vector3(0f, 0f, 3.00f));
wall1.update();
wall1.setPickable(true);
wall1.setEnabled(true);
engine.addEntity(wall1);
// os engine test
Object3D osCube = new Object3D("cube", _cube);
osCube.getTranslation().add(new Vector3(0f, 0f, 0f));
osCube.getScale().set(2f, 2f, 2f);
osCube.update();
osEngine.addEntity(osCube);
//
circleTransformations = new Transformations();
engine.addEntity(new PointsParticleSystemEntity("circle", false, new CircleParticleEmitter(3000, 50, 50, new Vector3(1f, 0f, 0f), new Vector3(0f, 0f, 1f), new Vector3(0f, 0f, 0f), 0.4f, 0f, 0f, new Vector3(0f, 0.2f, 0f), new Vector3(0f, 0.2f, 0f), new Color4(1f, 1f, 1f, 0.3f), new Color4(1f, 1f, 1f, 0.3f)), 1000, true));
engine.getEntity("circle").setEnabled(true);
engine.addEntity(new PointsParticleSystemEntity("water", true, new SphereParticleEmitter(4000, 1000, 0, 0.1f, 0.0f, new Sphere(new Vector3(-1f, 1f, 0f), 0.05f), new Vector3(-4f, 0f, 1f), new Vector3(-1f, 0f, 0f), new Color4(0.8f, 0.8f, 1f, 0.25f), new Color4(0.8f, 0.8f, 1f, 0.25f)), 4000, true));
engine.getEntity("water").setEnabled(true);
engine.addEntity(new PointsParticleSystemEntity("snow", false, new BoundingBoxParticleEmitter(15, 15000, 1000, 0, 0, new OrientedBoundingBox(new Vector3(0f, 4f, 0f), new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, 0f, 1f), new Vector3(4f, 0f, 4f)), new Vector3(0f, -0.5f, 0f), new Vector3(0f, -0.1f, 0f), new Color4(0.8f, 0.8f, 1f, 0.5f), new Color4(0.8f, 0.8f, 1f, 0.5f)), 1024, true));
engine.getEntity("snow").setEnabled(true);
engine.addEntity(new PointsParticleSystemEntity("firebase", false, new SphereParticleEmitter(2048, 1024, 2048, 0, 0, new Sphere(new Vector3(2.5f, 0.2f, 0f), 0.2f), new Vector3(0f, 0.1f, 0f), new Vector3(0f, 0.1f, 0f), new Color4(0.0f, 0f, 0f, 0.5f), new Color4(0.4f, 0f, 0f, 0.5f)), 2048, true));
engine.getEntity("firebase").setEnabled(true);
//
engine.addEntity(new PointsParticleSystemEntity("firetop", false, new SphereParticleEmitter(2048, 1024, 2048, 0, 0, new Sphere(new Vector3(2.5f, 0.7f, 0f), 0.1f), new Vector3(0f, 0.06f, 0f), new Vector3(0f, 0.12f, 0f), new Color4(0.75f, 0.0f, 0f, 0.5f), new Color4(1f, 1f, 0f, 0.5f)), 2048, true));
engine.getEntity("firetop").setEnabled(true);
//
engine.addEntity(new PointsParticleSystemEntity("firesmoke", false, new SphereParticleEmitter(2048, 1024, 2048, 0, 0, new Sphere(new Vector3(2.5f, 0.7f, 0f), 0.1f), new Vector3(0f, 0.2f, 0f), new Vector3(0f, 0.4f, 0f), new Color4(0.8f, 0.8f, 0.8f, 0.1f), new Color4(0.8f, 0.8f, 0.8f, 0.1f)), 2048, true));
engine.getEntity("firesmoke").setEnabled(true);
((ParticleSystemEntity) engine.getEntity("circle")).setPickable(false);
((ParticleSystemEntity) engine.getEntity("snow")).setPickable(false);
((ParticleSystemEntity) engine.getEntity("firebase")).setPickable(true);
((ParticleSystemEntity) engine.getEntity("firetop")).setPickable(true);
((ParticleSystemEntity) engine.getEntity("firesmoke")).setPickable(true);
} catch (Exception exception) {
exception.printStackTrace();
System.out.println("Could not load object: " + exception.getMessage());
}
}
use of net.drewke.tdme.engine.primitives.BoundingVolume in project tdme by andreasdr.
the class EngineTest method doPlayerControl.
/**
* Do player control
* @param idx
* @param key left
* @param key right
* @param key up
*/
private void doPlayerControl(int idx, boolean keyLeft, boolean keyRight, boolean keyUp) {
float fps = engine.getTiming().getCurrentFPS();
Object3D player = players.get(idx);
BoundingVolume playerBoundingVolumeTransformed = playerBoundingVolumesTransformed.get(idx);
Rotations rotations = player.getRotations();
Rotation r = rotations.get(0);
player.update();
Vector3 movement = new Vector3();
// left, right
if (keyRight)
r.setAngle(r.getAngle() - (135f / fps));
if (keyLeft)
r.setAngle(r.getAngle() + (135f / fps));
if (keyRight || keyLeft) {
player.update();
playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
}
// forward
if (keyUp) {
// apply movement
r.getQuaternion().multiply(new Vector3(0f, 0f, 1f), movement);
movement.scale(1.50f / fps);
player.getTranslation().add(movement);
player.update();
playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
// walking animation
if (player.getAnimation().equals("walk") == false) {
player.setAnimation("walk");
}
} else {
// still animation
if (player.getAnimation().equals("walk") == true) {
player.setAnimation("still");
}
}
// check if collides with cube
if (playerBoundingVolumeTransformed.doesCollideWith(cubeBoundingVolumeTransformed, movement, collision) == true && collision.hasPenetration() == true) {
System.out.println("cube: " + collision);
// yep, move object out of collision
player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
player.update();
playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
}
// check if collides with barrel
if (CollisionDetection.getInstance().doCollide((Capsule) playerBoundingVolumeTransformed, (ConvexMesh) barrelBoundingVolumeTransformed, movement, collision) == true && collision.hasPenetration() == true) {
System.out.println("barrel: " + collision);
// yep, move object out of collision
player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
player.update();
playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
}
// check if collides with other players
for (int i = 0; i < players.size(); i++) {
// do not check with same player
if (idx == i)
continue;
// do collide?
if (playerBoundingVolumeTransformed.doesCollideWith(playerBoundingVolumesTransformed.get(i), movement, collision) == true && collision.hasPenetration()) {
System.out.println("player: " + collision);
// yep, move object out of collision
player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
player.update();
playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
}
}
}
Aggregations