Search in sources :

Example 6 with LevelEditorEntityBoundingVolume

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);
    }
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) Sphere(net.drewke.tdme.engine.primitives.Sphere) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) Capsule(net.drewke.tdme.engine.primitives.Capsule) ConvexMesh(net.drewke.tdme.engine.primitives.ConvexMesh)

Example 7 with LevelEditorEntityBoundingVolume

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);
        }
    }
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) Transformations(net.drewke.tdme.engine.Transformations) MutableString(net.drewke.tdme.utils.MutableString) RigidBody(net.drewke.tdme.engine.physics.RigidBody) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject)

Example 8 with LevelEditorEntityBoundingVolume

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;
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) Vector3(net.drewke.tdme.math.Vector3) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 9 with LevelEditorEntityBoundingVolume

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);
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume) Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Light(net.drewke.tdme.engine.Light) BoundingBox(net.drewke.tdme.engine.primitives.BoundingBox) Model(net.drewke.tdme.engine.model.Model) Vector3(net.drewke.tdme.math.Vector3) Camera(net.drewke.tdme.engine.Camera) Object3D(net.drewke.tdme.engine.Object3D)

Example 10 with LevelEditorEntityBoundingVolume

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);
}
Also used : LevelEditorEntityBoundingVolume(net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume)

Aggregations

LevelEditorEntityBoundingVolume (net.drewke.tdme.tools.shared.model.LevelEditorEntityBoundingVolume)12 BoundingBox (net.drewke.tdme.engine.primitives.BoundingBox)3 Vector3 (net.drewke.tdme.math.Vector3)3 IOException (java.io.IOException)2 Entity (net.drewke.tdme.engine.Entity)2 Object3D (net.drewke.tdme.engine.Object3D)2 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)2 Capsule (net.drewke.tdme.engine.primitives.Capsule)2 ConvexMesh (net.drewke.tdme.engine.primitives.ConvexMesh)2 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)2 Sphere (net.drewke.tdme.engine.primitives.Sphere)2 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)2 File (java.io.File)1 Camera (net.drewke.tdme.engine.Camera)1 Light (net.drewke.tdme.engine.Light)1 Transformations (net.drewke.tdme.engine.Transformations)1 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)1 Model (net.drewke.tdme.engine.model.Model)1 RigidBody (net.drewke.tdme.engine.physics.RigidBody)1 Triangle (net.drewke.tdme.engine.primitives.Triangle)1