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(float x, float y, float z, float w) 

Source Link

Document

Constructor, sets the four components of the quaternion.

Usage

From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java

License:Apache License

private ModelNode parseNodesRecursively(JsonValue json) {
    ModelNode jsonNode = new ModelNode();

    String id = json.getString("id", null);
    if (id == null)
        throw new GdxRuntimeException("Node id missing.");
    jsonNode.id = id;//  w  w w.j  av a  2 s .co m

    JsonValue translation = json.get("translation");
    if (translation != null && translation.size != 3)
        throw new GdxRuntimeException("Node translation incomplete");
    jsonNode.translation = translation == null ? null
            : new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));

    JsonValue rotation = json.get("rotation");
    if (rotation != null && rotation.size != 4)
        throw new GdxRuntimeException("Node rotation incomplete");
    jsonNode.rotation = rotation == null ? null
            : new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2),
                    rotation.getFloat(3));

    JsonValue scale = json.get("scale");
    if (scale != null && scale.size != 3)
        throw new GdxRuntimeException("Node scale incomplete");
    jsonNode.scale = scale == null ? null
            : new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));

    String meshId = json.getString("mesh", null);
    if (meshId != null)
        jsonNode.meshId = meshId;

    JsonValue materials = json.get("parts");
    if (materials != null) {
        jsonNode.parts = new ModelNodePart[materials.size];
        int i = 0;
        for (JsonValue material = materials.child; material != null; material = material.next, i++) {
            ModelNodePart nodePart = new ModelNodePart();

            String meshPartId = material.getString("meshpartid", null);
            String materialId = material.getString("materialid", null);
            if (meshPartId == null || materialId == null) {
                throw new GdxRuntimeException("Node " + id + " part is missing meshPartId or materialId");
            }
            nodePart.materialId = materialId;
            nodePart.meshPartId = meshPartId;

            JsonValue bones = material.get("bones");
            if (bones != null) {
                nodePart.bones = new ArrayMap<String, Matrix4>(true, bones.size, String.class, Matrix4.class);
                int j = 0;
                for (JsonValue bone = bones.child; bone != null; bone = bone.next, j++) {
                    String nodeId = bone.getString("node", null);
                    if (nodeId == null)
                        throw new GdxRuntimeException("Bone node ID missing");

                    Matrix4 transform = new Matrix4();

                    JsonValue val = bone.get("translation");
                    if (val != null && val.size >= 3)
                        transform.translate(val.getFloat(0), val.getFloat(1), val.getFloat(2));

                    val = bone.get("rotation");
                    if (val != null && val.size >= 4)
                        transform.rotate(
                                tempQ.set(val.getFloat(0), val.getFloat(1), val.getFloat(2), val.getFloat(3)));

                    val = bone.get("scale");
                    if (val != null && val.size >= 3)
                        transform.scale(val.getFloat(0), val.getFloat(1), val.getFloat(2));

                    nodePart.bones.put(nodeId, transform);
                }
            }

            jsonNode.parts[i] = nodePart;
        }
    }

    JsonValue children = json.get("children");
    if (children != null) {
        jsonNode.children = new ModelNode[children.size];

        int i = 0;
        for (JsonValue child = children.child; child != null; child = child.next, i++) {
            jsonNode.children[i] = parseNodesRecursively(child);
        }
    }

    return jsonNode;
}

From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java

License:Apache License

private void parseAnimations(ModelData model, JsonValue json) {
    JsonValue animations = json.get("animations");
    if (animations == null)
        return;/*from ww w. java 2  s  .co m*/

    model.animations.ensureCapacity(animations.size);

    for (JsonValue anim = animations.child; anim != null; anim = anim.next) {
        JsonValue nodes = anim.get("bones");
        if (nodes == null)
            continue;
        ModelAnimation animation = new ModelAnimation();
        model.animations.add(animation);
        animation.nodeAnimations.ensureCapacity(nodes.size);
        animation.id = anim.getString("id");
        for (JsonValue node = nodes.child; node != null; node = node.next) {
            ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
            animation.nodeAnimations.add(nodeAnim);
            nodeAnim.nodeId = node.getString("boneId");

            // For backwards compatibility (version 0.1):
            JsonValue keyframes = node.get("keyframes");
            if (keyframes != null && keyframes.isArray()) {
                for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
                    final float keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                    JsonValue translation = keyframe.get("translation");
                    if (translation != null && translation.size == 3) {
                        if (nodeAnim.translation == null)
                            nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> tkf = new ModelNodeKeyframe<Vector3>();
                        tkf.keytime = keytime;
                        tkf.value = new Vector3(translation.getFloat(0), translation.getFloat(1),
                                translation.getFloat(2));
                        nodeAnim.translation.add(tkf);
                    }
                    JsonValue rotation = keyframe.get("rotation");
                    if (rotation != null && rotation.size == 4) {
                        if (nodeAnim.rotation == null)
                            nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                        ModelNodeKeyframe<Quaternion> rkf = new ModelNodeKeyframe<Quaternion>();
                        rkf.keytime = keytime;
                        rkf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1),
                                rotation.getFloat(2), rotation.getFloat(3));
                        nodeAnim.rotation.add(rkf);
                    }
                    JsonValue scale = keyframe.get("scale");
                    if (scale != null && scale.size == 3) {
                        if (nodeAnim.scaling == null)
                            nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                        ModelNodeKeyframe<Vector3> skf = new ModelNodeKeyframe();
                        skf.keytime = keytime;
                        skf.value = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
                        nodeAnim.scaling.add(skf);
                    }
                }
            } else { // Version 0.2:
                JsonValue translationKF = node.get("translation");
                if (translationKF != null && translationKF.isArray()) {
                    nodeAnim.translation = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.translation.ensureCapacity(translationKF.size);
                    for (JsonValue keyframe = translationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.translation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue translation = keyframe.get("value");
                        if (translation != null && translation.size >= 3)
                            kf.value = new Vector3(translation.getFloat(0), translation.getFloat(1),
                                    translation.getFloat(2));
                    }
                }

                JsonValue rotationKF = node.get("rotation");
                if (rotationKF != null && rotationKF.isArray()) {
                    nodeAnim.rotation = new Array<ModelNodeKeyframe<Quaternion>>();
                    nodeAnim.rotation.ensureCapacity(rotationKF.size);
                    for (JsonValue keyframe = rotationKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Quaternion> kf = new ModelNodeKeyframe<Quaternion>();
                        nodeAnim.rotation.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue rotation = keyframe.get("value");
                        if (rotation != null && rotation.size >= 4)
                            kf.value = new Quaternion(rotation.getFloat(0), rotation.getFloat(1),
                                    rotation.getFloat(2), rotation.getFloat(3));
                    }
                }

                JsonValue scalingKF = node.get("scaling");
                if (scalingKF != null && scalingKF.isArray()) {
                    nodeAnim.scaling = new Array<ModelNodeKeyframe<Vector3>>();
                    nodeAnim.scaling.ensureCapacity(scalingKF.size);
                    for (JsonValue keyframe = scalingKF.child; keyframe != null; keyframe = keyframe.next) {
                        ModelNodeKeyframe<Vector3> kf = new ModelNodeKeyframe<Vector3>();
                        nodeAnim.scaling.add(kf);
                        kf.keytime = keyframe.getFloat("keytime", 0f) / 1000.f;
                        JsonValue scaling = keyframe.get("value");
                        if (scaling != null && scaling.size >= 3)
                            kf.value = new Vector3(scaling.getFloat(0), scaling.getFloat(1),
                                    scaling.getFloat(2));
                    }
                }
            }
        }
    }
}

From source file:com.mygdx.game.simulation.Simulation.java

License:Apache License

public void moveShipLeft(float delta, float scale, int shipNumber) {
    if (ships[shipNumber].isExploding)
        return;/*from w w w.  j a  v  a2s .co  m*/

    float q0 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ0();
    float q1 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ1();
    float q2 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ2();
    float q3 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ3();

    ships[shipNumber].transform.trn(-delta * Ship.SHIP_VELOCITY * scale, 0, 0);
    ships[shipNumber].transform.getTranslation(tmpV1);
    if (tmpV1.x < PLAYFIELD_MIN_X)
        ships[shipNumber].transform.trn(PLAYFIELD_MIN_X - tmpV1.x, 0, 0);

    Vector3 oldTranslation = ships[shipNumber].transform.getTranslation(tmpV1);
    Quaternion rotateQ = new Quaternion(q0, -1 * q1, q3, -1 * q2); //Used if you want all 3-axis rotation

    ships[shipNumber].transform.setToRotation(0, 0, 0, 0);
    ships[shipNumber].transform.set(oldTranslation, rotateQ);
}

From source file:com.mygdx.game.simulation.Simulation.java

License:Apache License

public void moveShipRight(float delta, float scale, int shipNumber) {
    if (ships[shipNumber].isExploding)
        return;//  w w  w.j ava 2s  . c om

    float q0 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ0();
    float q1 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ1();
    float q2 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ2();
    float q3 = (float) Invaders.mInvaderInterfaceArray[shipNumber].getQ3();

    ships[shipNumber].transform.trn(+delta * Ship.SHIP_VELOCITY * scale, 0, 0);
    if (tmpV1.x > PLAYFIELD_MAX_X)
        ships[shipNumber].transform.trn(PLAYFIELD_MAX_X - tmpV1.x, 0, 0);
    Vector3 oldTranslation = ships[shipNumber].transform.getTranslation(tmpV1);
    Vector3 flip = new Vector3(0, 0, 0);
    Quaternion rotateQ = new Quaternion(q0, -1 * q1, q3, -1 * q2); //For 3-axis
    ships[shipNumber].transform.setToRotation(0, 0, 0, 0);
    ships[shipNumber].transform.set(oldTranslation.mulAdd(flip, -1), rotateQ);
}

From source file:net.smert.frameworkgl.examples.common.BulletGameObjectFactory.java

License:Apache License

public BulletGameObject create(String name, Transform4f worldTransform) {
    Integer existingIndex = nameToIndex.get(name);
    if (existingIndex == null) {
        return null;
    }//from   ww w  . ja va 2s.com
    BulletGameObject.Constructor constructor = constructors.get(existingIndex);
    Quaternion4f rotation = new Quaternion4f(worldTransform.getRotation());
    Vector3f position = worldTransform.getPosition();
    Quaternion newRotation = new Quaternion(rotation.getX(), rotation.getY(), rotation.getZ(), rotation.getW());
    Vector3 newPosition = new Vector3(position.getX(), position.getY(), position.getZ());
    Vector3 newScale = new Vector3(1f, 1f, 1f);
    return constructor.create(new Matrix4(newPosition, newRotation, newScale));
}