Skip to content

Instantly share code, notes, and snippets.

@xoppa
Created July 22, 2014 22:17
Show Gist options
  • Save xoppa/ded7980ac2b3c796b6b0 to your computer and use it in GitHub Desktop.
Save xoppa/ded7980ac2b3c796b6b0 to your computer and use it in GitHub Desktop.
PointingTest
package com.badlogic.gdx.tests.g3d;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.model.Animation;
import com.badlogic.gdx.graphics.g3d.model.Node;
import com.badlogic.gdx.graphics.g3d.utils.AnimationController;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.Ray;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.StringBuilder;
public class PointingTest extends BaseG3dHudTest {
ObjectMap<ModelInstance, AnimationController> animationControllers = new ObjectMap<ModelInstance, AnimationController>();
String currentAnimation = "";
ModelInstance libgdxman;
Node pointNode;
@Override
public void create () {
super.create();
showAxes = true;
modelsWindow.setVisible(false);
String name = "g3d/test/LibGDXMan.g3db";
currentlyLoading = "data/" + name;
assets.load(currentlyLoading, Model.class);
loading = true;
}
Vector3 vx = new Vector3(), vy = new Vector3(), vz = new Vector3();
Matrix4 m = new Matrix4();
@Override
protected void render (ModelBatch batch, Array<ModelInstance> instances) {
for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries())
e.value.update(Gdx.graphics.getDeltaTime());
if (pointNode != null) {
Ray r = cam.getPickRay(Gdx.input.getX(), Gdx.input.getY());
vx.set(r.direction).scl(.95f*r.origin.len()).add(r.origin);
libgdxman.transform.getTranslation(vy);
vx.mul(m.set(libgdxman.transform).mul(pointNode.parent.globalTransform).inv()).nor();
vz.set(vx).crs(Vector3.Y);
vy.set(vx).crs(vz);
vz.set(vx).crs(vy);
Matrix4 m = pointNode.localTransform.idt().trn(pointNode.translation);
Vector3 v;
m.val[Matrix4.M00] = vx.x; m.val[Matrix4.M01] = vx.y; m.val[Matrix4.M02] = vx.z;
m.val[Matrix4.M10] = vy.x; m.val[Matrix4.M11] = vy.y; m.val[Matrix4.M12] = vy.z;
m.val[Matrix4.M20] = vz.x; m.val[Matrix4.M21] = vz.y; m.val[Matrix4.M22] = vz.z;
pointNode.isAnimated = true;
libgdxman.calculateTransforms();
}
batch.render(instances);
}
@Override
protected void getStatus (StringBuilder stringBuilder) {
super.getStatus(stringBuilder);
for (final ModelInstance instance : instances) {
if (instance.animations.size > 0) {
stringBuilder.append(" press space or menu to switch animation");
if (currentAnimation.length() > 0)
stringBuilder.append(" (").append(currentAnimation).append(")");
break;
}
}
}
protected String currentlyLoading;
@Override
protected void onModelClicked (final String name) {}
@Override
protected void onLoaded () {
if (currentlyLoading == null || currentlyLoading.isEmpty()) return;
instances.clear();
animationControllers.clear();
libgdxman = new ModelInstance(assets.get(currentlyLoading, Model.class));
instances.add(libgdxman);
if (libgdxman.animations.size > 0) animationControllers.put(libgdxman, new AnimationController(libgdxman));
currentlyLoading = null;
pointNode = libgdxman.getNode("ORG-upper_arm_L");
if (pointNode == null)
Gdx.app.log("Test", "No such node");
}
protected void switchAnimation () {
for (ObjectMap.Entry<ModelInstance, AnimationController> e : animationControllers.entries()) {
int animIndex = -1;
if (e.value.current != null) {
for (int i = 0; i < e.key.animations.size; i++) {
final Animation animation = e.key.animations.get(i);
if (e.value.current.animation == animation) {
animIndex = i;
break;
}
}
}
if (animIndex == e.key.animations.size - 1) {
e.value.setAnimation(null);
currentAnimation = "";
}
else {
animIndex = (animIndex + 1) % e.key.animations.size;
e.value.animate(currentAnimation = e.key.animations.get(animIndex).id, -1, 1f, null, 0.2f);
}
}
}
@Override
public boolean keyUp (int keycode) {
if (keycode == Keys.SPACE || keycode == Keys.MENU) switchAnimation();
return super.keyUp(keycode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment