use of net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume in project tdme by andreasdr.
the class EntityBoundingVolumeView method setBoundingVolumes.
/**
* Set bounding volumes
* @param entity
*/
public void setBoundingVolumes(LevelEditorEntity entity) {
// set up default bounding volumes
for (int i = 0; i < EntityBoundingVolumeSubScreenController.MODEL_BOUNDINGVOLUME_COUNT; i++) {
resetBoundingVolume(entity, i);
}
// set up existing bounding volumes
for (int i = 0; i < entity.getBoundingVolumeCount(); i++) {
LevelEditorEntityBoundingVolume bv = entity.getBoundingVolumeAt(i);
if (bv == null) {
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.NONE);
continue;
} else if (bv.getBoundingVolume() instanceof Sphere) {
Sphere sphere = (Sphere) bv.getBoundingVolume();
modelViewerScreenController.setupSphere(i, sphere.getCenter(), sphere.getRadius());
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.SPHERE);
} else if (bv.getBoundingVolume() instanceof Capsule) {
Capsule capsule = (Capsule) bv.getBoundingVolume();
modelViewerScreenController.setupCapsule(i, capsule.getA(), capsule.getB(), capsule.getRadius());
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.CAPSULE);
} else if (bv.getBoundingVolume() instanceof BoundingBox) {
BoundingBox aabb = (BoundingBox) bv.getBoundingVolume();
modelViewerScreenController.setupBoundingBox(i, aabb.getMin(), aabb.getMax());
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.BOUNDINGBOX);
} else if (bv.getBoundingVolume() instanceof OrientedBoundingBox) {
OrientedBoundingBox obb = (OrientedBoundingBox) bv.getBoundingVolume();
modelViewerScreenController.setupOrientedBoundingBox(i, obb.getCenter(), obb.getAxes()[0], obb.getAxes()[1], obb.getAxes()[2], obb.getHalfExtension());
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.ORIENTEDBOUNDINGBOX);
} else if (bv.getBoundingVolume() instanceof ConvexMesh) {
modelViewerScreenController.setupConvexMesh(i, bv.getModelMeshFile());
modelViewerScreenController.selectBoundingVolume(i, EntityBoundingVolumeSubScreenController.BoundingVolumeType.CONVEXMESH);
}
// enable bounding volume and set type in GUI
modelViewerScreenController.enableBoundingVolume(i);
modelViewerScreenController.setupModelBoundingVolumeType(entity, i);
}
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume in project tdme by andreasdr.
the class Level method addLevel.
/**
* Add level to physics world
* @param world
* @param level
* @param rigid bodies (will be filled by logic)
* @param translation
*/
public static void addLevel(World world, LevelEditorLevel level, ArrayList<RigidBody> rigidBodies, Vector3 translation) {
// load level objects
for (int i = 0; i < level.getObjectCount(); i++) {
LevelEditorObject object = level.getObjectAt(i);
// skip on empties or trigger
if (object.getEntity().getType() == EntityType.EMPTY)
continue;
if (object.getEntity().getType() == EntityType.TRIGGER)
continue;
if (object.getEntity().getType() == EntityType.PARTICLESYSTEM)
continue;
//
for (int j = 0; j < object.getEntity().getBoundingVolumeCount(); j++) {
LevelEditorEntityBoundingVolume entityBv = object.getEntity().getBoundingVolumeAt(j);
// keep track of world ids
String worldId = object.getId() + ".bv." + j;
// TODO: apply transformations
Transformations transformations = new Transformations();
// apply transformations
transformations.fromTransformations(object.getTransformations());
// apply translation
if (translation != null) {
transformations.getTranslation().add(translation);
transformations.update();
}
// add to physics world
RigidBody rigidBody = world.addStaticRigidBody(worldId, true, RIGIDBODY_TYPEID_STATIC, transformations, entityBv.getBoundingVolume(), 1.0f);
rigidBody.setCollisionTypeIds(RIGIDBODY_TYPEID_STATIC | RIGIDBODY_TYPEID_PLAYER);
// add to rigid bodies
rigidBodies.add(rigidBody);
}
}
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume in project tdme by andreasdr.
the class ModelMetaDataFileImport method parseBoundingVolume.
/**
* Parse bounding volume
* @param idx
* @param level editor entity
* @param JSON bounding volume node
* @return level editor entity bounding volume
* @throws JSONException
*/
private static LevelEditorEntityBoundingVolume parseBoundingVolume(int idx, LevelEditorEntity levelEditorEntity, JSONObject jBv) throws JSONException {
LevelEditorEntityBoundingVolume entityBoundingVolume = new LevelEditorEntityBoundingVolume(idx, levelEditorEntity);
BoundingVolume bv;
String bvTypeString = jBv.getString("type");
if (bvTypeString.equalsIgnoreCase("none") == true) {
entityBoundingVolume.setupNone();
} else if (bvTypeString.equalsIgnoreCase("sphere") == true) {
entityBoundingVolume.setupSphere(new Vector3((float) jBv.getDouble("cx"), (float) jBv.getDouble("cy"), (float) jBv.getDouble("cz")), (float) jBv.getDouble("r"));
} else if (bvTypeString.equalsIgnoreCase("capsule") == true) {
entityBoundingVolume.setupCapsule(new Vector3((float) jBv.getDouble("ax"), (float) jBv.getDouble("ay"), (float) jBv.getDouble("az")), new Vector3((float) jBv.getDouble("bx"), (float) jBv.getDouble("by"), (float) jBv.getDouble("bz")), (float) jBv.getDouble("r"));
} else if (bvTypeString.equalsIgnoreCase("aabb") == true) {
entityBoundingVolume.setupAabb(new Vector3((float) jBv.getDouble("mix"), (float) jBv.getDouble("miy"), (float) jBv.getDouble("miz")), new Vector3((float) jBv.getDouble("max"), (float) jBv.getDouble("may"), (float) jBv.getDouble("maz")));
} else if (bvTypeString.equalsIgnoreCase("obb") == true) {
entityBoundingVolume.setupObb(new Vector3((float) jBv.getDouble("cx"), (float) jBv.getDouble("cy"), (float) jBv.getDouble("cz")), new Vector3((float) jBv.getDouble("a0x"), (float) jBv.getDouble("a0y"), (float) jBv.getDouble("a0z")), new Vector3((float) jBv.getDouble("a1x"), (float) jBv.getDouble("a1y"), (float) jBv.getDouble("a1z")), new Vector3((float) jBv.getDouble("a2x"), (float) jBv.getDouble("a2y"), (float) jBv.getDouble("a2z")), new Vector3((float) jBv.getDouble("hex"), (float) jBv.getDouble("hey"), (float) jBv.getDouble("hez")));
} else if (bvTypeString.equalsIgnoreCase("convexmesh") == true) {
try {
entityBoundingVolume.setupConvexMesh(jBv.getString("file"));
} catch (Exception e) {
e.printStackTrace();
}
}
// done
return entityBoundingVolume;
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume in project tdme by andreasdr.
the class Tools method setupEntity.
/**
* Set up entity in given engine with look from rotations and scale
* @param entity
* @param engine
* @param look from rotations
* @param scale
*/
public static void setupEntity(LevelEditorEntity entity, Engine engine, Transformations lookFromRotations, float scale) {
if (entity == null)
return;
// entity bounding box
BoundingBox entityBoundingBox = null;
// add model to engine
if (entity.getType() == EntityType.PARTICLESYSTEM) {
entityBoundingBox = new BoundingBox(new Vector3(-0.5f, 0f, -0.5f), new Vector3(0.5f, 3f, 0.5f));
Entity particleSystemObject = Level.createParticleSystem(entity.getParticleSystem(), "model", true);
if (particleSystemObject != null) {
engine.addEntity(particleSystemObject);
}
} else {
entityBoundingBox = entity.getModel().getBoundingBox();
Object3D modelObject = new Object3D("model", entity.getModel());
modelObject.setDynamicShadowingEnabled(true);
engine.addEntity(modelObject);
}
// create ground object
Model ground = createGroundModel((entityBoundingBox.getMax().getX() - entityBoundingBox.getMin().getX()) * 1f, (entityBoundingBox.getMax().getZ() - entityBoundingBox.getMin().getZ()) * 1f, entityBoundingBox.getMin().getY() - MathTools.EPSILON);
// add ground to engine
Object3D groundObject = new Object3D("ground", ground);
groundObject.setEnabled(false);
engine.addEntity(groundObject);
// add bounding volume if we have any
for (int i = 0; i < entity.getBoundingVolumeCount(); i++) {
LevelEditorEntityBoundingVolume boundingVolume = entity.getBoundingVolumeAt(i);
if (boundingVolume.getModel() == null)
continue;
Object3D modelBoundingVolumeObject = new Object3D("model_bv." + i, boundingVolume.getModel());
modelBoundingVolumeObject.setEnabled(false);
engine.addEntity(modelBoundingVolumeObject);
}
// set up lights
for (Light light : engine.getLights()) light.setEnabled(false);
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(entityBoundingBox.getMin().getX() + ((entityBoundingBox.getMax().getX() - entityBoundingBox.getMin().getX()) / 2f), //modelBoundingBox.getMax().getY(),
entityBoundingBox.getMin().getY() + ((entityBoundingBox.getMax().getY() - entityBoundingBox.getMin().getY()) / 2f), -entityBoundingBox.getMin().getZ() * 4f, 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);
// model dimension
Vector3 dimension = entityBoundingBox.getMax().clone().sub(entityBoundingBox.getMin());
// determine max dimension on each axis
float maxAxisDimension = computeMaxAxisDimension(entityBoundingBox);
// set up cam
Camera cam = engine.getCamera();
cam.setZNear(maxAxisDimension / 5000f);
cam.setZFar(maxAxisDimension);
// look at
Vector3 lookAt = entityBoundingBox.getMin().clone().add(dimension.clone().scale(0.5f));
cam.getLookAt().set(lookAt);
// look at -> look to vector
Vector3 lookAtToFromVector = new Vector3(0f, 0f, +(maxAxisDimension * 1.2f));
// apply look from rotations
Vector3 lookAtToFromVectorTransformed = new Vector3();
Vector3 lookAtToFromVectorScaled = new Vector3();
Vector3 upVector = new Vector3();
lookFromRotations.getTransformationsMatrix().multiply(lookAtToFromVector, lookAtToFromVectorTransformed);
lookAtToFromVectorScaled.set(lookAtToFromVectorTransformed).scale(scale);
lookFromRotations.getRotations().get(2).getQuaternion().multiply(new Vector3(0f, 1f, 0f), upVector);
// look from with rotations
Vector3 lookFrom = lookAt.clone().add(lookAtToFromVectorScaled);
cam.getLookFrom().set(lookFrom);
// up vector
cam.getUpVector().set(upVector);
}
use of net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume in project tdme by andreasdr.
the class EntityBoundingVolumeView method applyBoundingVolumeConvexMesh.
/**
* On bounding volume convex mesh apply
* @param entity
* @param bounding volume index
* @param file
*/
public void applyBoundingVolumeConvexMesh(LevelEditorEntity entity, int idx, String file) {
// exit if no entity
if (entity == null)
return;
LevelEditorEntityBoundingVolume entityBoundingVolume = entity.getBoundingVolumeAt(idx);
entityBoundingVolume.setupConvexMesh(file);
updateModelBoundingVolume(entity, idx);
}
Aggregations