Search in sources :

Example 11 with Rotation

use of net.drewke.tdme.engine.Rotation in project tdme by andreasdr.

the class Tools method oseInit.

/**
	 * Init off screen engine for making thumbails
	 * @param drawable
	 */
public static void oseInit(GLAutoDrawable drawable) {
    osEngine = Engine.createOffScreenInstance(drawable, 128, 128);
    osEngine.setPartition(new PartitionNone());
    setDefaultLight(osEngine.getLightAt(0));
    oseScale = 0.75f;
    oseLookFromRotations = new Transformations();
    oseLookFromRotations.getRotations().add(new Rotation(-45f, new Vector3(0f, 1f, 0f)));
    oseLookFromRotations.getRotations().add(new Rotation(-45f, new Vector3(1f, 0f, 0f)));
    oseLookFromRotations.getRotations().add(new Rotation(0f, new Vector3(0f, 0f, 1f)));
    oseLookFromRotations.update();
}
Also used : PartitionNone(net.drewke.tdme.engine.PartitionNone) Transformations(net.drewke.tdme.engine.Transformations) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation)

Example 12 with Rotation

use of net.drewke.tdme.engine.Rotation in project tdme by andreasdr.

the class DAEReader method readLevel.

/**
	 * Reads Collada DAE file level
	 * @param path name
	 * @param file name
	 * @throws Exception
	 * @return Model instance
	 */
public static LevelEditorLevel readLevel(String pathName, String fileName) throws Exception {
    // (re)create tm files folder
    File tmFilesFolder = new File(pathName + "/" + fileName + "-models");
    if (tmFilesFolder.exists()) {
        tmFilesFolder.delete();
    }
    tmFilesFolder.mkdir();
    // create level
    LevelEditorLevel levelEditorLevel = new LevelEditorLevel();
    LevelPropertyPresets.getInstance().setDefaultLevelProperties(levelEditorLevel);
    // load dae xml document
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(FileSystem.getInstance().getInputStream(pathName, fileName));
    Element xmlRoot = document.getDocumentElement();
    // authoring tool
    AuthoringTool authoringTool = getAuthoringTool(xmlRoot);
    // up vector and rotation order
    UpVector upVector = getUpVector(xmlRoot);
    RotationOrder rotationOrder = null;
    switch(upVector) {
        case Y_UP:
            rotationOrder = RotationOrder.ZYX;
        case Z_UP:
            rotationOrder = RotationOrder.YZX;
    }
    levelEditorLevel.setRotationOrder(rotationOrder);
    // parse scene from xml
    String xmlSceneId = null;
    Element xmlScene = getChildrenByTagName(xmlRoot, "scene").get(0);
    for (Element xmlInstanceVisualscene : getChildrenByTagName(xmlScene, "instance_visual_scene")) {
        xmlSceneId = xmlInstanceVisualscene.getAttribute("url").substring(1);
    }
    // check for xml scene id
    if (xmlSceneId == null) {
        throw new ModelFileIOException("No scene id found");
    }
    // parse visual scenes
    Element xmlLibraryVisualScenes = getChildrenByTagName(xmlRoot, "library_visual_scenes").get(0);
    for (Element xmlLibraryVisualScene : getChildrenByTagName(xmlLibraryVisualScenes, "visual_scene")) {
        String xmlVisualSceneId = xmlLibraryVisualScene.getAttribute("id");
        if (xmlVisualSceneId.equals(xmlSceneId)) {
            // default FPS
            float fps = 30f;
            // parse frames per second
            List<Element> xmlExtraNodes = getChildrenByTagName(xmlLibraryVisualScene, "extra");
            if (xmlExtraNodes.isEmpty() == false) {
                Element xmlExtraNode = xmlExtraNodes.get(0);
                for (Element xmlTechnique : getChildrenByTagName(xmlExtraNode, "technique")) {
                    List<Element> xmlFrameRateNodes = getChildrenByTagName(xmlTechnique, "frame_rate");
                    if (xmlFrameRateNodes.isEmpty() == false) {
                        fps = Float.parseFloat(xmlFrameRateNodes.get(0).getTextContent());
                        break;
                    }
                }
            }
            // visual scene root nodes
            LevelEditorEntityLibrary entityLibrary = levelEditorLevel.getEntityLibrary();
            LevelEditorEntity emptyEntity = null;
            int nodeIdx = 0;
            for (Element xmlNode : getChildrenByTagName(xmlLibraryVisualScene, "node")) {
                // derive model name from node id
                String modelName = xmlNode.getAttribute("id");
                // replace blender _|-NUMBER, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[\\-\\_]{1}+[0-9]+$", "");
                // replace number at the end still, not sure if this is a good idea for all cases, we will see
                modelName = modelName.replaceAll("[0-9]+$", "");
                // check if name is available, if not extend with numbers :DDD
                boolean haveName = entityLibrary.getEntityCount() == 0;
                if (haveName == false) {
                    for (int i = 0; i < 10000; i++) {
                        haveName = true;
                        String modelNameTry = modelName + (i == 0 ? "" : String.valueOf(i));
                        for (int entityIdx = 0; entityIdx < entityLibrary.getEntityCount(); entityIdx++) {
                            LevelEditorEntity entity = entityLibrary.getEntityAt(entityIdx);
                            if (entity.getName().equals(modelNameTry) == true) {
                                haveName = false;
                                break;
                            }
                        }
                        if (haveName == true) {
                            modelName = modelNameTry;
                            break;
                        }
                    }
                }
                // do we have a name now?
                if (haveName == false) {
                    // nope, cant imagine this will happen 
                    System.out.println("DAEReader::readLevel(): Skipping model '" + modelName + "' as no name could be created for it.");
                    continue;
                }
                // 	create model
                Model model = new Model(pathName + File.separator + fileName + '-' + modelName, fileName + '-' + modelName, upVector, rotationOrder, null);
                // import matrix
                setupModelImportRotationMatrix(xmlRoot, model);
                Matrix4x4 modelImportRotationMatrix = new Matrix4x4(model.getImportTransformationsMatrix());
                setupModelImportScaleMatrix(xmlRoot, model);
                // translation, scaling, rotation
                Vector3 translation = new Vector3();
                Vector3 scale = new Vector3();
                Vector3 rotation = new Vector3();
                Vector3 xAxis = new Vector3();
                Vector3 yAxis = new Vector3();
                Vector3 zAxis = new Vector3();
                // set up local transformations matrix
                Matrix4x4 nodeTransformationsMatrix = null;
                List<Element> xmlMatrixElements = getChildrenByTagName(xmlNode, "matrix");
                if (xmlMatrixElements.size() == 1) {
                    String xmlMatrix = xmlMatrixElements.get(0).getTextContent();
                    StringTokenizer t = new StringTokenizer(xmlMatrix, " \n\r");
                    // 
                    nodeTransformationsMatrix = new Matrix4x4(Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken()), Float.parseFloat(t.nextToken())).transpose();
                }
                // check if we have node transformations matrix
                if (nodeTransformationsMatrix == null) {
                    throw new ModelFileIOException("missing node transformations matrix for node " + xmlNode.getAttribute("id"));
                }
                // extract coordinate system axes
                nodeTransformationsMatrix.getAxes(xAxis, yAxis, zAxis);
                nodeTransformationsMatrix.getTranslation(translation);
                nodeTransformationsMatrix.getScale(scale);
                // normalize coordinate axes
                xAxis.normalize();
                yAxis.normalize();
                zAxis.normalize();
                // write back normalized x axis
                nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                // TODO: move me into Matrix4x4 decomposing code?
                if ((upVector == UpVector.Y_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, yAxis), zAxis) < 0.0f) || (upVector == UpVector.Z_UP && Vector3.computeDotProduct(Vector3.computeCrossProduct(xAxis, zAxis), yAxis) < 0.0f)) {
                    // negate axes
                    xAxis.scale(-1f);
                    yAxis.scale(-1f);
                    zAxis.scale(-1f);
                    // and write back to node transformation matrices
                    nodeTransformationsMatrix.setAxes(xAxis, yAxis, zAxis);
                    // scale
                    scale.scale(-1f);
                }
                // determine rotation
                nodeTransformationsMatrix.computeEulerAngles(rotation);
                // apply model import matrix
                modelImportRotationMatrix.multiply(scale, scale);
                modelImportRotationMatrix.multiply(rotation, rotation);
                model.getImportTransformationsMatrix().multiply(translation, translation);
                // set up frames per seconds
                model.setFPS(fps);
                // read sub groups
                Group group = readVisualSceneNode(authoringTool, pathName, model, null, xmlRoot, xmlNode, fps);
                if (group != null) {
                    group.getTransformationsMatrix().identity();
                    model.getSubGroups().put(group.getId(), group);
                    model.getGroups().put(group.getId(), group);
                }
                // set up joints
                ModelHelper.setupJoints(model);
                // fix animation length
                ModelHelper.fixAnimationLength(model);
                // prepare for indexed rendering
                ModelHelper.prepareForIndexedRendering(model);
                // check if empty model
                EntityType entityType = EntityType.MODEL;
                ModelStatistics modelStatistics = ModelUtilities.computeModelStatistics(model);
                if (modelStatistics.getOpaqueFaceCount() == 0 && modelStatistics.getTransparentFaceCount() == 0) {
                    entityType = EntityType.EMPTY;
                }
                // level editor entity
                LevelEditorEntity levelEditorEntity = null;
                // model
                if (entityType == EntityType.MODEL) {
                    // check if we have that model already
                    for (int i = 0; i < levelEditorLevel.getEntityLibrary().getEntityCount(); i++) {
                        LevelEditorEntity levelEditorEntityCompare = levelEditorLevel.getEntityLibrary().getEntityAt(i);
                        if (levelEditorEntityCompare.getType() != EntityType.MODEL)
                            continue;
                        if (ModelUtilities.equals(model, levelEditorEntityCompare.getModel()) == true) {
                            levelEditorEntity = levelEditorEntityCompare;
                            break;
                        }
                    }
                    // create level editor model, if not yet exists
                    if (levelEditorEntity == null) {
                        // save model
                        TMWriter.write(model, pathName + "/" + fileName + "-models", modelName + ".tm");
                        // create level editor entity
                        levelEditorEntity = entityLibrary.addModel(nodeIdx++, modelName, modelName, pathName + "/" + fileName + "-models", modelName + ".tm", new Vector3());
                    }
                } else // empty
                if (entityType == EntityType.EMPTY) {
                    if (emptyEntity == null) {
                        emptyEntity = entityLibrary.addEmpty(nodeIdx++, "Default Empty", "");
                    }
                    levelEditorEntity = emptyEntity;
                } else {
                    System.out.println("DAEReader::readLevel(): unknown entity type. Skipping");
                    continue;
                }
                // level editor object transformations
                Transformations levelEditorObjectTransformations = new Transformations();
                levelEditorObjectTransformations.getTranslation().set(translation);
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis0VectorIndex()], rotationOrder.getAxis0()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis1VectorIndex()], rotationOrder.getAxis1()));
                levelEditorObjectTransformations.getRotations().add(new Rotation(rotation.getArray()[rotationOrder.getAxis2VectorIndex()], rotationOrder.getAxis2()));
                levelEditorObjectTransformations.getScale().set(scale);
                levelEditorObjectTransformations.update();
                // level editor object
                LevelEditorObject object = new LevelEditorObject(xmlNode.getAttribute("id"), xmlNode.getAttribute("id"), levelEditorObjectTransformations, levelEditorEntity);
                // add object to level
                levelEditorLevel.addObject(object);
            }
        }
    }
    // save level
    LevelFileExport.export(pathName + "/" + fileName + ".tl", levelEditorLevel);
    //
    return levelEditorLevel;
}
Also used : Group(net.drewke.tdme.engine.model.Group) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ModelStatistics(net.drewke.tdme.engine.subsystems.object.ModelUtilitiesInternal.ModelStatistics) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) UpVector(net.drewke.tdme.engine.model.Model.UpVector) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation) Joint(net.drewke.tdme.engine.model.Joint) Matrix4x4(net.drewke.tdme.math.Matrix4x4) EntityType(net.drewke.tdme.tools.shared.model.LevelEditorEntity.EntityType) LevelEditorEntityLibrary(net.drewke.tdme.tools.shared.model.LevelEditorEntityLibrary) StringTokenizer(java.util.StringTokenizer) LevelEditorLevel(net.drewke.tdme.tools.shared.model.LevelEditorLevel) DocumentBuilder(javax.xml.parsers.DocumentBuilder) RotationOrder(net.drewke.tdme.engine.model.RotationOrder) Model(net.drewke.tdme.engine.model.Model) Transformations(net.drewke.tdme.engine.Transformations) File(java.io.File)

Example 13 with Rotation

use of net.drewke.tdme.engine.Rotation in project tdme by andreasdr.

the class PhysicsTest3 method init.

/*
	 * (non-Javadoc)
	 * @see com.jogamp.opengl.GLEventListener#init(com.jogamp.opengl.GLAutoDrawable)
	 */
public void init(GLAutoDrawable drawable) {
    drawable.getGL().setSwapInterval(0);
    engine.init(drawable);
    Object3D entity;
    // cam
    Camera cam = engine.getCamera();
    cam.setZNear(0.10f);
    cam.setZFar(50.00f);
    cam.getLookFrom().set(0f, 4f * 2.5f, -6f * 2.5f);
    cam.getLookAt().set(0f, 0.0f, 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);
    // side
    OrientedBoundingBox side = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(1f, 16f, 8f));
    Model sideModel = PrimitiveModel.createModel(side, "side_model");
    sideModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    sideModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    OrientedBoundingBox nearFar = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(8f, 16f, 1f));
    Model nearFarModel = PrimitiveModel.createModel(nearFar, "far_model");
    nearFarModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    nearFarModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    entity = new Object3D("far", nearFarModel);
    entity.getTranslation().addZ(+9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("far", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // near
    entity = new Object3D("near", nearFarModel);
    entity.getTranslation().addZ(-9f);
    entity.getEffectColorMul().set(1f, 1f, 1f, 0f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("near", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // side left
    entity = new Object3D("sideright", sideModel);
    entity.getTranslation().addX(-9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideright", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // side right
    entity = new Object3D("sideleft", sideModel);
    entity.getTranslation().addX(9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideleft", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // box
    OrientedBoundingBox box = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.6f, 0.6f, 0.6f));
    Model boxModel = PrimitiveModel.createModel(box, "box_model");
    boxModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.5f, 0.5f, 1f);
    boxModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    // boxes
    for (int i = 0; i < BOX_COUNT; i++) {
        entity = new Object3D("box" + i, boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(10f + i * 3.0f);
        entity.getTranslation().addX(-2f + i * 0.1f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + i, true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // stack
    for (int i = 0; i < BOXSTACK_COUNT; i++) {
        entity = new Object3D("box" + (BOX_COUNT + i), boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(1.6f + (i * 1.2f));
        entity.getTranslation().addX(+3f);
        entity.getTranslation().addZ(-5f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + (BOX_COUNT + i), true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // sphere
    Sphere sphere = new Sphere(new Vector3(0f, 0f, 0f), 0.4f);
    Model sphereModel = PrimitiveModel.createModel(sphere, "sphere_model");
    sphereModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.5f, 0.8f, 0.8f, 1f);
    sphereModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(0f, 1f, 1f, 1f);
    // spheres
    for (int i = 0; i < SPHERE_COUNT; i++) {
        entity = new Object3D("sphere" + i, sphereModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(12f + (i * 1f));
        entity.getTranslation().addX(0.45f * i - 3f);
        entity.getTranslation().addZ(0.1f * i - 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("sphere" + i, true, RIGID_TYPEID_STANDARD, entity, sphere, 0.75f, 0.4f, 10f, RigidBody.computeInertiaMatrix(sphere, 10f, 1f, 1f, 1f));
    }
    // sphere
    Capsule capsule = new Capsule(new Vector3(0f, 0.5f, 0f), new Vector3(0f, -0.5f, 0f), 0.25f);
    Model capsuleModel = PrimitiveModel.createModel(capsule, "capsule_model");
    capsuleModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.0f, 0.8f, 1f);
    capsuleModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 1f, 1f);
    //
    for (int i = 0; i < CAPSULE_COUNT; i++) {
        entity = new Object3D("capsule" + i, capsuleModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(14f + (i * 2f));
        entity.getTranslation().addX((i * 0.5f));
        // entity.getPivot().set(capsule.getCenter());
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("capsule" + i, true, RIGID_TYPEID_STANDARD, entity, capsule, 0.0f, 0.4f, 3f, RigidBody.computeInertiaMatrix(capsule, 3f, 1f, 1f, 1f));
    }
    // sphere
    //Capsule capsuleBig = new Capsule(
    //	new Vector3(0f,+1f,0f),
    //	new Vector3(0f,-1f,0f),
    //	0.5f
    //);
    OrientedBoundingBox capsuleBig = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.5f, 1f, 0.5f));
    Model capsuleBigModel = PrimitiveModel.createModel(capsuleBig, "capsulebig_model");
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(1f, 0.8f, 0.8f, 1f);
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    System.out.println(capsuleBig.getCenter());
    //
    entity = new Object3D("capsulebig1", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(-2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig1", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 80f, RigidBody.getNoRotationInertiaMatrix());
    //
    entity = new Object3D("capsulebig2", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(+2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig2", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 100f, RigidBody.getNoRotationInertiaMatrix());
    //
    try {
        Model _terrainModel = DAEReader.read("resources/tests/environment/terrain_test", "terrain_test.dae");
        _terrainModel.getImportTransformationsMatrix().scale(1.5f);
        entity = new Object3D("terrain", _terrainModel);
        entity.getTranslation().setY(-4f);
        entity.update();
        engine.addEntity(entity);
        ArrayList<ConvexMesh> groundConvexMeshes = new ArrayList<ConvexMesh>();
        ConvexMesh.createTerrainConvexMeshes(new Object3DModel(_terrainModel), groundConvexMeshes);
        for (int i = 0; i < groundConvexMeshes.size(); i++) {
            world.addStaticRigidBody("ground" + i, true, RIGID_TYPEID_STANDARD, entity, groundConvexMeshes.get(i), 0.5f);
        }
        // load barrel, set up bounding volume
        Model _barrel = DAEReader.read("resources/tests/models/barrel", "barrel.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh barrelBoundingVolume = new ConvexMesh(new Object3DModel(_barrel));
        // set up barrel 1 in 3d engine
        entity = new Object3D("barrel1", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+4f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel1", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // set up barrel 2 in 3d engine
        entity = new Object3D("barrel2", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+6f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel2", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _cone = DAEReader.read("resources/tests/models/cone", "cone.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh coneBoundingVolume = new ConvexMesh(new Object3DModel(_cone));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone1", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone1", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone2", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-5f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone2", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _tire = DAEReader.read("resources/tests/models/tire", "tire.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh tireBoundingVolume = new ConvexMesh(new Object3DModel(_tire));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire1", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire1", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire2", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-6f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire2", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) Vector3(net.drewke.tdme.math.Vector3) Capsule(net.drewke.tdme.engine.primitives.Capsule) ConvexMesh(net.drewke.tdme.engine.primitives.ConvexMesh) Object3DModel(net.drewke.tdme.engine.Object3DModel) Rotation(net.drewke.tdme.engine.Rotation) Object3D(net.drewke.tdme.engine.Object3D) Sphere(net.drewke.tdme.engine.primitives.Sphere) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) Light(net.drewke.tdme.engine.Light) Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) Object3DModel(net.drewke.tdme.engine.Object3DModel) Camera(net.drewke.tdme.engine.Camera)

Example 14 with Rotation

use of net.drewke.tdme.engine.Rotation in project tdme by andreasdr.

the class LevelEditorView method placeObject.

/**
	 * Places selected model on given object
	 */
public void placeObject(Entity selectedObject) {
    if (selectedEntity != null && selectedObject != null) {
        // get selected level entity if it is one
        LevelEditorObject selectedLevelEditorObject = level.getObjectById(selectedObject.getId());
        // create level entity
        Transformations levelEditorObjectTransformations = new Transformations();
        // take translation of selected object as base
        levelEditorObjectTransformations.getTranslation().set(selectedObject.getTranslation());
        Vector3 centerSelectedObject = selectedObject.getBoundingBox().getMin().clone().add(selectedObject.getBoundingBox().getMax()).scale(0.5f);
        // compute center of selected model
        Vector3 centerNewObject = selectedEntity.getModel() != null ? selectedEntity.getModel().getBoundingBox().getCenter().clone() : new Vector3(0f, 0f, 0f);
        // put new object on middle of selected object
        levelEditorObjectTransformations.getTranslation().add(centerNewObject.clone().add(centerSelectedObject));
        // set on selected object / y
        if (selectedLevelEditorObject == null || selectedLevelEditorObject.getEntity().getType() == EntityType.PARTICLESYSTEM || selectedEntity.getType() == EntityType.PARTICLESYSTEM) {
            levelEditorObjectTransformations.getTranslation().setY(gridY + (selectedEntity.getModel() != null ? -selectedEntity.getModel().getBoundingBox().getMin().getY() : 0f));
        } else {
            // create transformed level editor object bounding box
            BoundingVolume bv = selectedLevelEditorObject.getEntity().getModel().getBoundingBox().clone();
            bv.fromBoundingVolumeWithTransformations(selectedLevelEditorObject.getEntity().getModel().getBoundingBox(), selectedLevelEditorObject.getTransformations());
            //
            levelEditorObjectTransformations.getTranslation().setY(bv.computeDimensionOnAxis(new Vector3(0f, 1f, 0f)) / 2 + bv.getCenter().getY() + -selectedEntity.getModel().getBoundingBox().getMin().getY());
        }
        // standard scale
        levelEditorObjectTransformations.getScale().set(new Vector3(1f, 1f, 1f));
        // standard rotations
        levelEditorObjectTransformations.getPivot().set(selectedEntity.getPivot());
        levelEditorObjectTransformations.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis0()));
        levelEditorObjectTransformations.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis1()));
        levelEditorObjectTransformations.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis2()));
        levelEditorObjectTransformations.update();
        // check if entity already exists
        for (int i = 0; i < level.getObjectCount(); i++) {
            LevelEditorObject levelEditorObject = level.getObjectAt(i);
            if (levelEditorObject.getEntity() == selectedEntity && levelEditorObject.getTransformations().getTranslation().equals(levelEditorObjectTransformations.getTranslation())) {
                // we already have a object with selected model on this translation
                return;
            }
        }
        // create new level editor object
        LevelEditorObject levelEditorObject = new LevelEditorObject(selectedEntity.getName() + "_" + level.allocateObjectId(), "", levelEditorObjectTransformations, selectedEntity);
        //	add to level
        level.addObject(levelEditorObject);
        // add model to 3d engine
        if (levelEditorObject.getEntity().getModel() != null) {
            Object3D object = new Object3D(levelEditorObject.getId(), levelEditorObject.getEntity().getModel());
            object.fromTransformations(levelEditorObjectTransformations);
            object.setPickable(true);
            engine.addEntity(object);
        }
        // add particle system to 3d engine
        if (levelEditorObject.getEntity().getType() == EntityType.PARTICLESYSTEM) {
            Entity object = Level.createParticleSystem(levelEditorObject.getEntity().getParticleSystem(), levelEditorObject.getId(), false);
            object.fromTransformations(levelEditorObjectTransformations);
            object.setPickable(true);
            engine.addEntity(object);
        }
        // add to objects listbox
        levelEditorScreenController.setObjectListbox(level.getObjectIdsIterator());
    }
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) Transformations(net.drewke.tdme.engine.Transformations) Vector3(net.drewke.tdme.math.Vector3) LevelEditorObject(net.drewke.tdme.tools.shared.model.LevelEditorObject) Rotation(net.drewke.tdme.engine.Rotation) Object3D(net.drewke.tdme.engine.Object3D)

Aggregations

Rotation (net.drewke.tdme.engine.Rotation)14 Vector3 (net.drewke.tdme.math.Vector3)12 Transformations (net.drewke.tdme.engine.Transformations)8 Object3D (net.drewke.tdme.engine.Object3D)6 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)5 Camera (net.drewke.tdme.engine.Camera)4 Model (net.drewke.tdme.engine.model.Model)4 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)4 File (java.io.File)3 Light (net.drewke.tdme.engine.Light)3 Object3DModel (net.drewke.tdme.engine.Object3DModel)3 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)3 ConvexMesh (net.drewke.tdme.engine.primitives.ConvexMesh)3 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)3 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)3 Sphere (net.drewke.tdme.engine.primitives.Sphere)3 IOException (java.io.IOException)2 Entity (net.drewke.tdme.engine.Entity)2 Rotations (net.drewke.tdme.engine.Rotations)2 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)2