Example usage for com.badlogic.gdx.math Quaternion Quaternion

List of usage examples for com.badlogic.gdx.math Quaternion Quaternion

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Quaternion Quaternion.

Prototype

public Quaternion() 

Source Link

Usage

From source file:br.com.raphaelbruno.game.zombieinvaders.vr.model.GameObject.java

License:Apache License

public void lookAt(Vector3 point) {
    Vector3 from = transform.getTranslation(new Vector3()).cpy();
    Vector3 to = point.cpy();//from w  w  w. j a v  a 2 s. c  o m
    Vector3 direction = to.sub(from).nor();
    direction.set(-direction.x, -direction.y, -direction.z);

    Quaternion quaternion = new Quaternion();
    Matrix4 instanceRotation = transform.cpy().mul(transform);

    instanceRotation.setToLookAt(direction, new Vector3(0, -1, 0));
    instanceRotation.rotate(0, 0, 1, 180);
    instanceRotation.getRotation(quaternion);

    transform.set(from, quaternion);
}

From source file:br.com.raphaelbruno.game.zombieinvaders.vr.tween.GameObjectAccessor.java

License:Apache License

@Override
public int getValues(GameObject target, int tweenType, float[] returnValues) {
    Vector3 position = target.transform.getTranslation(new Vector3());
    Vector3 rotation = new Vector3();

    switch (tweenType) {
    case XYZ:/*from  ww w  .  j a  v a  2s  .c  o m*/
        returnValues[0] = position.x;
        returnValues[1] = position.y;
        returnValues[2] = position.z;
        return 3;

    case ROTATION:
        returnValues[0] = target.transform.getRotation(new Quaternion()).getAxisAngle(rotation)
                * rotation.nor().y;
        return 1;

    case ALPHA:
        returnValues[0] = target.blending.opacity;
        return 1;

    default:
        assert false;
        return -1;
    }
}

From source file:com.kevlanche.threedeetest.ScalableObjLoader.java

License:Apache License

protected ModelData loadModelData(FileHandle file, boolean flipV) {
    String line;//from w w w. j  a va2 s .co m
    String[] tokens;
    char firstChar;
    MtlLoader mtl = new MtlLoader();

    // Create a "default" Group and set it as the active group, in case
    // there are no groups or objects defined in the OBJ file.
    Group activeGroup = new Group("default");
    groups.add(activeGroup);

    BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096);
    int id = 0;
    try {
        while ((line = reader.readLine()) != null) {

            tokens = line.split("\\s+");
            if (tokens.length < 1)
                break;

            if (tokens[0].length() == 0) {
                continue;
            } else if ((firstChar = tokens[0].toLowerCase().charAt(0)) == '#') {
                continue;
            } else if (firstChar == 'v') {
                if (tokens[0].length() == 1) {
                    verts.add(Float.parseFloat(tokens[1]) * this.objScale);
                    verts.add(Float.parseFloat(tokens[2]) * this.objScale);
                    verts.add(Float.parseFloat(tokens[3]) * this.objScale);
                } else if (tokens[0].charAt(1) == 'n') {
                    norms.add(Float.parseFloat(tokens[1]));
                    norms.add(Float.parseFloat(tokens[2]));
                    norms.add(Float.parseFloat(tokens[3]));
                } else if (tokens[0].charAt(1) == 't') {
                    uvs.add(Float.parseFloat(tokens[1]));
                    uvs.add((flipV ? 1 - Float.parseFloat(tokens[2]) : Float.parseFloat(tokens[2])));
                }
            } else if (firstChar == 'f') {
                String[] parts;
                Array<Integer> faces = activeGroup.faces;
                for (int i = 1; i < tokens.length - 2; i--) {
                    parts = tokens[1].split("/");
                    faces.add(getIndex(parts[0], verts.size));
                    if (parts.length > 2) {
                        if (i == 1)
                            activeGroup.hasNorms = true;
                        faces.add(getIndex(parts[2], norms.size));
                    }
                    if (parts.length > 1 && parts[1].length() > 0) {
                        if (i == 1)
                            activeGroup.hasUVs = true;
                        faces.add(getIndex(parts[1], uvs.size));
                    }
                    parts = tokens[++i].split("/");
                    faces.add(getIndex(parts[0], verts.size));
                    if (parts.length > 2)
                        faces.add(getIndex(parts[2], norms.size));
                    if (parts.length > 1 && parts[1].length() > 0)
                        faces.add(getIndex(parts[1], uvs.size));
                    parts = tokens[++i].split("/");
                    faces.add(getIndex(parts[0], verts.size));
                    if (parts.length > 2)
                        faces.add(getIndex(parts[2], norms.size));
                    if (parts.length > 1 && parts[1].length() > 0)
                        faces.add(getIndex(parts[1], uvs.size));
                    activeGroup.numFaces++;
                }
            } else if (firstChar == 'o' || firstChar == 'g') {
                // This implementation only supports single object or group
                // definitions. i.e. "o group_a group_b" will set group_a
                // as the active group, while group_b will simply be
                // ignored.
                if (tokens.length > 1)
                    activeGroup = setActiveGroup(tokens[1]);
                else
                    activeGroup = setActiveGroup("default");
            } else if (tokens[0].equals("mtllib")) {
                mtl.load(file.parent().child(tokens[1]));
            } else if (tokens[0].equals("usemtl")) {
                if (tokens.length == 1)
                    activeGroup.materialName = "default";
                else
                    activeGroup.materialName = tokens[1];
            }
        }
        reader.close();
    } catch (IOException e) {
        return null;
    }

    // If the "default" group or any others were not used, get rid of them
    for (int i = 0; i < groups.size; i++) {
        if (groups.get(i).numFaces < 1) {
            groups.removeIndex(i);
            i--;
        }
    }

    // If there are no groups left, there is no valid Model to return
    if (groups.size < 1)
        return null;

    // Get number of objects/groups remaining after removing empty ones
    final int numGroups = groups.size;

    final ModelData data = new ModelData();

    for (int g = 0; g < numGroups; g++) {
        Group group = groups.get(g);
        Array<Integer> faces = group.faces;
        final int numElements = faces.size;
        final int numFaces = group.numFaces;
        final boolean hasNorms = group.hasNorms;
        final boolean hasUVs = group.hasUVs;

        final float[] finalVerts = new float[(numFaces * 3) * (3 + (hasNorms ? 3 : 0) + (hasUVs ? 2 : 0))];

        for (int i = 0, vi = 0; i < numElements;) {
            int vertIndex = faces.get(i++) * 3;
            finalVerts[vi++] = verts.get(vertIndex++);
            finalVerts[vi++] = verts.get(vertIndex++);
            finalVerts[vi++] = verts.get(vertIndex);
            if (hasNorms) {
                int normIndex = faces.get(i++) * 3;
                finalVerts[vi++] = norms.get(normIndex++);
                finalVerts[vi++] = norms.get(normIndex++);
                finalVerts[vi++] = norms.get(normIndex);
            }
            if (hasUVs) {
                int uvIndex = faces.get(i++) * 2;
                finalVerts[vi++] = uvs.get(uvIndex++);
                finalVerts[vi++] = uvs.get(uvIndex);
            }
        }

        final int numIndices = numFaces * 3 >= Short.MAX_VALUE ? 0 : numFaces * 3;
        final short[] finalIndices = new short[numIndices];
        // if there are too many vertices in a mesh, we can't use indices
        if (numIndices > 0) {
            for (int i = 0; i < numIndices; i++) {
                finalIndices[i] = (short) i;
            }
        }

        Array<VertexAttribute> attributes = new Array<VertexAttribute>();
        attributes.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE));
        if (hasNorms)
            attributes.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));
        if (hasUVs)
            attributes.add(
                    new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));

        String nodeId = "node" + (++id);
        String meshId = "mesh" + id;
        String partId = "part" + id;
        ModelNode node = new ModelNode();
        node.id = nodeId;
        node.meshId = meshId;
        node.scale = new Vector3(1, 1, 1);
        node.translation = new Vector3();
        node.rotation = new Quaternion();
        ModelNodePart pm = new ModelNodePart();
        pm.meshPartId = partId;
        pm.materialId = group.materialName;
        node.parts = new ModelNodePart[] { pm };
        ModelMeshPart part = new ModelMeshPart();
        part.id = partId;
        part.indices = finalIndices;
        part.primitiveType = GL10.GL_TRIANGLES;
        ModelMesh mesh = new ModelMesh();
        mesh.id = meshId;
        mesh.attributes = attributes.toArray(VertexAttribute.class);
        mesh.vertices = finalVerts;
        mesh.parts = new ModelMeshPart[] { part };
        data.nodes.add(node);
        data.meshes.add(mesh);
        ModelMaterial mm = mtl.getMaterial(group.materialName);
        data.materials.add(mm);
    }

    //for (ModelMaterial m : mtl.materials)
    //data.materials.add(m);

    // An instance of ObjLoader can be used to load more than one OBJ.
    // Clearing the Array cache instead of instantiating new
    // Arrays should result in slightly faster load times for
    // subsequent calls to loadObj
    if (verts.size > 0)
        verts.clear();
    if (norms.size > 0)
        norms.clear();
    if (uvs.size > 0)
        uvs.clear();
    if (groups.size > 0)
        groups.clear();

    return data;
}

From source file:com.mbrlabs.mundus.commons.scene3d.SimpleNode.java

License:Apache License

public SimpleNode(int id) {
    super(id);//w ww  . j  ava 2s .co  m
    localPosition = new Vector3();
    localRotation = new Quaternion();
    localScale = new Vector3(1, 1, 1);
    combined = new Matrix4();
}

From source file:com.mbrlabs.mundus.history.commands.RotateCommand.java

License:Apache License

public RotateCommand(GameObject go) {
    this.before = new Quaternion();
    this.after = new Quaternion();
    this.go = go;
}

From source file:com.mbrlabs.mundus.tools.ModelPlacementTool.java

License:Apache License

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    if (curEntity != null && button == Input.Buttons.LEFT) {
        int id = projectManager.current().obtainID();
        GameObject modelGo = new GameObject(projectManager.current().currScene.sceneGraph, model.getName(), id);
        projectManager.current().currScene.sceneGraph.addGameObject(modelGo);

        curEntity.modelInstance.transform.getTranslation(tempV3);
        modelGo.translate(tempV3);/*from   w ww .j  ava 2 s  .  c  o m*/
        Quaternion rot = new Quaternion();
        rot = curEntity.modelInstance.transform.getRotation(rot);
        modelGo.setLocalRotation(rot.x, rot.y, rot.z, rot.w);
        ModelComponent modelComponent = new ModelComponent(modelGo);
        modelComponent.setShader(shader);
        modelComponent.setModelInstance(curEntity);
        modelComponent.encodeRaypickColorId();

        try {
            modelGo.addComponent(modelComponent);
        } catch (InvalidComponentException e) {
            Dialogs.showErrorDialog(Ui.getInstance(), e.getMessage());
            return false;
        }

        Mundus.postEvent(new SceneGraphChangedEvent());

        curEntity = new MModelInstance(model);
        mouseMoved(screenX, screenY);
    }
    return false;
}

From source file:com.mbrlabs.mundus.tools.ToolHandle.java

License:Apache License

public ToolHandle(int id) {
    this.id = id;
    position = new Vector3();
    rotationEuler = new Vector3();
    rotation = new Quaternion();
    scale = new Vector3(1, 1, 1);
    idAttribute = new PickerIDAttribute();
    PickerColorEncoder.encodeRaypickColorId(id, idAttribute);
}

From source file:MeshBoneUtil.dualQuat.java

License:Open Source License

public dualQuat() {
    real = new Quaternion();

    real.w = 0;//from  w w  w  . j a  v  a2s  .co m
    real.x = 0;
    real.y = 0;
    real.z = 0;

    imaginary = real.cpy();

    tmpQuat = new Quaternion();

    v0 = new Vector3(0, 0, 0);
    ve = new Vector3(0, 0, 0);
    tmpVec = new Vector3(0, 0, 0);
    tmpVec0 = new Vector3(0, 0, 0);
    tmpVec1 = new Vector3(0, 0, 0);
    tmpVec2 = new Vector3(0, 0, 0);
}

From source file:MeshBoneUtil.dualQuat.java

License:Open Source License

public dualQuat(Quaternion q0, Vector3 t) {
    real = q0;//w  w w .  j ava2 s.c om
    imaginary = new Quaternion();
    imaginary.w = -0.5f * (t.x * q0.x + t.y * q0.y + t.z * q0.z);
    imaginary.x = 0.5f * (t.x * q0.w + t.y * q0.z - t.z * q0.y);
    imaginary.y = 0.5f * (-t.x * q0.z + t.y * q0.w + t.z * q0.x);
    imaginary.z = 0.5f * (t.x * q0.y - t.y * q0.x + t.z * q0.w);
}

From source file:MeshBoneUtil.MeshBone.java

License:Open Source License

public void computeWorldDeltaTransforms() {
    Tuple<Vector3, Vector3> calc = computeDirs(world_start_pt, world_end_pt);
    Vector3 cur_tangent = new Vector3(calc.x.x, calc.x.y, 0);
    Vector3 cur_normal = new Vector3(calc.y.x, calc.y.y, 0);
    Vector3 cur_binormal = new Vector3(local_binormal_dir.x, local_binormal_dir.y, local_binormal_dir.z);

    Matrix4 cur_rotate = new Matrix4();
    /*//from   w ww  .j  ava2 s .c om
    cur_rotate.Right = cur_tangent;
    cur_rotate.Up = cur_normal;
    cur_rotate.Backward = cur_binormal;
    */
    cur_rotate.set(cur_tangent, cur_normal, cur_binormal, new Vector3(0, 0, 0));
    cur_rotate.tra();

    Matrix4 cur_translate = new Matrix4();
    cur_translate.setTranslation(world_start_pt.x, world_start_pt.y, 0);

    /*
    world_delta_mat = (cur_translate * cur_rotate)
    * bind_world_inv_mat;
    */

    world_delta_mat = (cur_translate.cpy().mul(cur_rotate)).mul(bind_world_inv_mat);
    //world_delta_mat = bind_world_inv_mat.cpy().mul(cur_rotate.cpy().mul(cur_translate));

    //        Quaternion cur_quat = XnaGeometry.Quaternion.CreateFromRotationMatrix(world_delta_mat);
    Quaternion cur_quat = new Quaternion().setFromMatrix(world_delta_mat);
    if (cur_quat.z < 0) {
        //cur_quat = -cur_quat;
    }

    Vector3 tmp_pos = new Vector3();
    world_delta_mat.getTranslation(tmp_pos);
    world_dq = new dualQuat(cur_quat, tmp_pos);

    for (int i = 0; i < children.size(); i++) {
        MeshBone cur_bone = children.get(i);
        cur_bone.computeWorldDeltaTransforms();
    }
}