com.badlogic.gdx.math.Quaternion Java Examples

The following examples show how to use com.badlogic.gdx.math.Quaternion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnimationControllerHack.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
/** https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#appendix-c-spline-interpolation 
 * 
 * https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/6a862d2607fb47ac48f54786b04e40be2ad866a4/src/interpolator.js
 * */
private static void cubic(Quaternion out, float t, float delta, Quaternion p0, Quaternion m0, Quaternion p1, Quaternion m1){
	
	// XXX not good, see https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/master/src/interpolator.js#L42
	delta =- delta;
	
	// p(t) = (2t3 - 3t2 + 1)p0 + (t3 - 2t2 + t)m0 + (-2t3 + 3t2)p1 + (t3 - t2)m1
	float t2 = t*t;
	float t3 = t2*t;
	q1.set(p0).mul(2*t3 - 3*t2 + 1);
	q2.set(m0).mul(delta).mul(t3 - 2*t2 + t);
	q3.set(p1).mul(-2*t3 + 3*t2);
	q4.set(m1).mul(delta).mul(t3-t2);
	
	out.set(q1).add(q2).add(q3).add(q4).nor();
}
 
Example #2
Source File: GLTFAnimationExporter.java    From gdx-gltf with Apache License 2.0 6 votes vote down vote up
@Override
protected void getOutput(FloatBuffer outputs, Quaternion value) {
	if(value instanceof CubicQuaternion){
		CubicQuaternion cubic = (CubicQuaternion)value;
		outputs.put(cubic.tangentIn.x);
		outputs.put(cubic.tangentIn.y);
		outputs.put(cubic.tangentIn.z);
		outputs.put(cubic.tangentIn.w);
		outputs.put(value.x);
		outputs.put(value.y);
		outputs.put(value.z);
		outputs.put(value.w);
		outputs.put(cubic.tangentOut.x);
		outputs.put(cubic.tangentOut.y);
		outputs.put(cubic.tangentOut.z);
		outputs.put(cubic.tangentOut.w);
	}else{
		outputs.put(value.x);
		outputs.put(value.y);
		outputs.put(value.z);
		outputs.put(value.w);
	}
}
 
Example #3
Source File: SimpleRoom.java    From gdx-vr with Apache License 2.0 6 votes vote down vote up
@Override
public void render() {
	float deltaTime = Gdx.graphics.getDeltaTime();

	if (Gdx.input.isKeyPressed(Input.Keys.W)) {
		VirtualReality.body.position.add(new Vector3(0, 0, -2).mul(VirtualReality.body.orientation).scl(deltaTime));
	}
	if (Gdx.input.isKeyPressed(Input.Keys.S)) {
		VirtualReality.body.position.add(new Vector3(0, 0, 2).mul(VirtualReality.body.orientation).scl(deltaTime));
	}
	if (Gdx.input.isKeyPressed(Input.Keys.A)) {
		VirtualReality.body.orientation.mulLeft(new Quaternion(Vector3.Y, 90f * deltaTime));
	}
	if (Gdx.input.isKeyPressed(Input.Keys.D)) {
		VirtualReality.body.orientation.mulLeft(new Quaternion(Vector3.Y, -90f * deltaTime));
	}

	VirtualReality.update(Gdx.graphics.getDeltaTime());
	VirtualReality.renderer.render();
}
 
Example #4
Source File: TreeGenerator.java    From Skyland with MIT License 6 votes vote down vote up
public static void initWorld(BulletWorld world) {
    //TreeShape
    Model model = Assets.get(Models.MODEL_TREE_PROTOTYPE, Model.class);
    model.nodes.first().translation.set(0, -1.15f, 0);
    btCompoundShape treeShape = new btCompoundShape();
    treeShape.addChildShape(new Matrix4(new Vector3(0, 0, 0), new Quaternion(), new Vector3(1, 1, 1)), new btBoxShape(new Vector3(.2f, .9f, .2f)));
    treeShape.addChildShape(new Matrix4(new Vector3(0, 1, 0), new Quaternion(), new Vector3(1, 1, 1)), new btSphereShape(1));
    //LogShape
    model = Assets.get(Models.MODEL_LOG_PROTOTYPE, Model.class);
    model.nodes.first().translation.set(0, -1.15f, 0);

    world.addConstructor("log", new BulletConstructor(Assets.get(Models.MODEL_LOG_PROTOTYPE, Model.class), 75, new btBoxShape(new Vector3(.2f, .9f, .2f))));
    world.addConstructor("stump", new BulletConstructor(Assets.get(Models.MODEL_STUMP_PROTOTYPE, Model.class), 0, new btCylinderShape(new Vector3(.2f, .22f, .2f))));
    world.addConstructor("staticTree", new BulletConstructor(Assets.get(Models.MODEL_TREE_PROTOTYPE, Model.class), 0, treeShape));
    world.addConstructor("dynamicTree", new BulletConstructor(Assets.get(Models.MODEL_TREE_PROTOTYPE, Model.class), 100, treeShape));
}
 
Example #5
Source File: ArmatureDebugDrawer.java    From GdxDemo3D with Apache License 2.0 6 votes vote down vote up
private void drawArmatureNodes(Node currentNode, Vector3 modelPos,
							   Quaternion modelRot,
							   Vector3 parentNodePos, Vector3 currentNodePos) {
	currentNode.globalTransform.getTranslation(currentNodePos);
	modelRot.transform(currentNodePos);
	currentNodePos.add(modelPos);
	drawVertex(currentNodePos, 0.02f, Color.GREEN);
	shapeRenderer.setColor(Color.YELLOW);
	if (currentNode.hasParent()) {
		shapeRenderer.line(parentNodePos, currentNodePos);
	}
	if (currentNode.hasChildren()) {
		float x = currentNodePos.x;
		float y = currentNodePos.y;
		float z = currentNodePos.z;
		for (Node child : currentNode.getChildren()) {
			drawArmatureNodes(child, modelPos, modelRot, currentNodePos, parentNodePos);
			currentNodePos.set(x, y, z);
		}
	}
}
 
Example #6
Source File: HeadlessG3dModelLoader.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
private void parseAnimations (ModelData model, JsonValue json) {
	JsonValue animations = json.get("animations");
	if (animations == null) return;

	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) {
			JsonValue keyframes = node.get("keyframes");

			ModelNodeAnimation nodeAnim = new ModelNodeAnimation();
			animation.nodeAnimations.add(nodeAnim);
			nodeAnim.nodeId = node.getString("boneId");
			nodeAnim.keyframes.ensureCapacity(keyframes.size);

			for (JsonValue keyframe = keyframes.child; keyframe != null; keyframe = keyframe.next) {
				ModelNodeKeyframe kf = new ModelNodeKeyframe();
				nodeAnim.keyframes.add(kf);
				kf.keytime = keyframe.getFloat("keytime") / 1000.f;
				JsonValue translation = keyframe.get("translation");
				if (translation != null && translation.size == 3)
					kf.translation = new Vector3(translation.getFloat(0), translation.getFloat(1), translation.getFloat(2));
				JsonValue rotation = keyframe.get("rotation");
				if (rotation != null && rotation.size == 4)
					kf.rotation = new Quaternion(rotation.getFloat(0), rotation.getFloat(1), rotation.getFloat(2),
							rotation.getFloat(3));
				JsonValue scale = keyframe.get("scale");
				if (scale != null && scale.size == 3)
					kf.scale = new Vector3(scale.getFloat(0), scale.getFloat(1), scale.getFloat(2));
			}
		}
	}
}
 
Example #7
Source File: AnimationControllerHack.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public Transform set (final Vector3 t, final Quaternion r, final Vector3 s, final WeightVector w) {
	translation.set(t);
	rotation.set(r);
	scale.set(s);
	weights.set(w);
	return this;
}
 
Example #8
Source File: Recorder.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public void update(Vector3 pos, Vector3 velocity, Quaternion rotation) {
	if (Main.frame % recordIntervalFrames != 0) return;
	Snapshot snap = new Snapshot();
	System.out.println("Snapshot: " + ObjectSize.getObjectSize(snap));
	snap.position.set(pos);
	//snap.velocity.set(velocity);
	snap.rotation.set(rotation);
	history.add(snap);
}
 
Example #9
Source File: Tools.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public static void faceDirectionY(Quaternion q, Vector3 direction) {
	throw new GdxRuntimeException("THIS IS NOT WORKING! DONT USE IT!");
	/*Vector3 axisZ = tmp.set(direction).nor();
	Vector3	axisY = tmp2.set(tmp).crs(Vector3.X).nor().crs(tmp).nor();
	Vector3	axisX = tmp3.set(axisY).crs(axisZ).nor();
	q.setFromAxes(false, axisX.x, axisY.x, axisZ.x,
			axisX.y, axisY.y, axisZ.y,
			axisX.z, axisY.z, axisZ.z);*/
}
 
Example #10
Source File: Tools.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
/** thanks to lordjone from #libgdx! */
public static void faceDirectionZ(Quaternion q, Vector3 direction) {
	Vector3 axisZ = tmp.set(direction).nor();
	Vector3	axisY = tmp2.set(tmp).crs(Vector3.Y).nor().crs(tmp).nor();
	Vector3	axisX = tmp3.set(axisY).crs(axisZ).nor();
	q.setFromAxes(false, axisX.x, axisY.x, axisZ.x,
			axisX.y, axisY.y, axisZ.y,
			axisX.z, axisY.z, axisZ.z);
}
 
Example #11
Source File: VirtualRealityRenderer.java    From gdx-vr with Apache License 2.0 5 votes vote down vote up
private void renderEye(Viewport eye, Vector3 eyeOffset) {
	Camera camera = eye.getCamera();

	Vector3 eyePosition = camera.position;
	eyePosition.set(VirtualReality.body.position);

	Vector3 headOffset = new Vector3(0, VirtualReality.head.getEyeHeight() / 2f, 0);
	headOffset.mul(VirtualReality.body.orientation);
	eyePosition.add(headOffset);

	Quaternion eyeOrientation = new Quaternion();
	eyeOrientation.set(VirtualReality.head.getOrientation());
	eyeOrientation.mul(VirtualReality.body.orientation);

	eyeOffset.mul(eyeOrientation);
	eyePosition.add(eyeOffset);

	Vector3 eyeDirection = new Vector3(0, 0, -1);
	eyeDirection.mul(eyeOrientation);
	Vector3 eyeUp = new Vector3(0, 1, 0);
	eyeUp.mul(eyeOrientation);

	camera.position.set(eyePosition);
	camera.direction.set(eyeDirection);
	camera.up.set(eyeUp);

	camera.update(true);

	for (VirtualRealityRenderListener listener : listeners) {
		listener.render(camera);
	}
}
 
Example #12
Source File: TypeTransformer.java    From gdx-vr with Apache License 2.0 5 votes vote down vote up
public static void transform(OvrQuaternionf sourceQuaternion, Quaternion quaternion) {
	float x = sourceQuaternion.x;
	float y = sourceQuaternion.y;
	float z = sourceQuaternion.z;
	float w = sourceQuaternion.w;

	quaternion.set(x, y, z, w);
}
 
Example #13
Source File: TypeTransformer.java    From gdx-vr with Apache License 2.0 5 votes vote down vote up
public static Quaternion transform(OvrQuaternionf sourceQuaternion) {
	float x = sourceQuaternion.x;
	float y = sourceQuaternion.y;
	float z = sourceQuaternion.z;
	float w = sourceQuaternion.w;

	return new Quaternion(x, y, z, w);
}
 
Example #14
Source File: GhostCamera.java    From GdxDemo3D with Apache License 2.0 5 votes vote down vote up
public void rotateAround(Vector3 point, Quaternion quat) {
	tmp.set(point).sub(targetPosition);
	targetPosition.add(tmp);
	quat.transform(targetDirection);
	quat.transform(targetUp);
	quat.transform(tmp);
	targetPosition.add(-tmp.x, -tmp.y, -tmp.z);
}
 
Example #15
Source File: AnimationControllerHack.java    From gdx-gltf with Apache License 2.0 5 votes vote down vote up
public Transform lerp (final Vector3 targetT, final Quaternion targetR, final Vector3 targetS, final WeightVector targetW, final float alpha) {
	translation.lerp(targetT, alpha);
	rotation.slerp(targetR, alpha);
	scale.lerp(targetS, alpha);
	weights.lerp(targetW, alpha);
	return this;
}
 
Example #16
Source File: SimpleNode.java    From Mundus with Apache License 2.0 5 votes vote down vote up
/**
 * Copy construction
 * 
 * @param simpleNode
 * @param id
 */
public SimpleNode(SimpleNode simpleNode, int id) {
    super(id);
    this.localPosition = new Vector3(simpleNode.localPosition);
    this.localRotation = new Quaternion(simpleNode.localRotation);
    this.localScale = new Vector3(simpleNode.localScale);
    this.combined = new Matrix4(simpleNode.combined);
}
 
Example #17
Source File: SimpleNode.java    From Mundus with Apache License 2.0 5 votes vote down vote up
public SimpleNode(int id) {
    super(id);
    localPosition = new Vector3();
    localRotation = new Quaternion();
    localScale = new Vector3(1, 1, 1);
    combined = new Matrix4();
}
 
Example #18
Source File: Entity.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public Quaternion getRotation() {
	return rotation;
}
 
Example #19
Source File: GLTFTypes.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
public static Quaternion map(Quaternion q, float[] fv) {
	return q.set(fv[0], fv[1], fv[2], fv[3]);
}
 
Example #20
Source File: CommandPackage.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public void setRotation(Quaternion q) {
	yaw = q.getYaw();
	pitch = q.getPitch();
	roll = q.getRoll();
}
 
Example #21
Source File: GLTFTypes.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
public static Quaternion map(Quaternion q, float[] fv, int offset) {
	return q.set(fv[offset+0], fv[offset+1], fv[offset+2], fv[offset+3]);
}
 
Example #22
Source File: GLTFExportTypes.java    From gdx-gltf with Apache License 2.0 4 votes vote down vote up
static float[] toArray(Quaternion v){
	return new float[]{v.x,v.y,v.z,v.w};
}
 
Example #23
Source File: Tools.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public static void print(Quaternion q) {
	System.out.println(fmt(q, null));
}
 
Example #24
Source File: Tools.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public static void print(Quaternion q, String name) {
	System.out.println(fmt(q, name));
}
 
Example #25
Source File: Tools.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public static String fmt(Quaternion q) {
	return fmt(q, null);
}
 
Example #26
Source File: Tools.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public static String fmt(Quaternion q, String name) {
	if (name == null) name = "";
	return String.format("(Q %s) - yaw: %.0f, pitch: %.0f, roll: %.0f -- w: %.2f, x: %.2f, y: %.2f, z: %.2f",
			name, q.getYaw(), q.getPitch(), q.getRoll(), q.w, q.x, q.y, q.z);
}
 
Example #27
Source File: View.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public void setCameraRotation(Quaternion q) {
	mtx.set(q);
	camera.direction.set(Vector3.Z);
	camera.direction.prj(mtx);
}
 
Example #28
Source File: Entity.java    From gdx-proto with Apache License 2.0 4 votes vote down vote up
public void setRotation(Quaternion newRot) {
	rotation.set(newRot);
}
 
Example #29
Source File: Head.java    From gdx-vr with Apache License 2.0 4 votes vote down vote up
public Quaternion getOrientation() {
	return orientation;
}
 
Example #30
Source File: SimpleNode.java    From Mundus with Apache License 2.0 4 votes vote down vote up
@Override
public Quaternion getLocalRotation(Quaternion out) {
    return out.set(localRotation);
}