|
@@ -32,18 +32,16 @@ package com.esotericsoftware.spine;
|
|
import com.badlogic.gdx.ApplicationAdapter;
|
|
import com.badlogic.gdx.ApplicationAdapter;
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
|
|
-import com.badlogic.gdx.graphics.GL20;
|
|
|
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
|
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
|
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
|
import com.badlogic.gdx.math.Vector2;
|
|
import com.badlogic.gdx.math.Vector2;
|
|
import com.badlogic.gdx.math.Vector3;
|
|
import com.badlogic.gdx.math.Vector3;
|
|
|
|
+import com.badlogic.gdx.utils.ScreenUtils;
|
|
|
|
+
|
|
import com.esotericsoftware.spine.utils.TwoColorPolygonBatch;
|
|
import com.esotericsoftware.spine.utils.TwoColorPolygonBatch;
|
|
|
|
|
|
-/** Demonstrates how to let the target bone of an IK constraint
|
|
|
|
- * follow the mouse or touch position, which in turn repositions
|
|
|
|
- * part of the skeleton, in this case Spineboy's back arm including
|
|
|
|
- * his gun.
|
|
|
|
- */
|
|
|
|
|
|
+/** Demonstrates how to let the target bone of an IK constraint follow the mouse or touch position, which in turn repositions part
|
|
|
|
+ * of the skeleton, in this case Spineboy's back arm including his gun. */
|
|
public class IKTest extends ApplicationAdapter {
|
|
public class IKTest extends ApplicationAdapter {
|
|
OrthographicCamera camera;
|
|
OrthographicCamera camera;
|
|
TwoColorPolygonBatch batch;
|
|
TwoColorPolygonBatch batch;
|
|
@@ -52,7 +50,7 @@ public class IKTest extends ApplicationAdapter {
|
|
TextureAtlas atlas;
|
|
TextureAtlas atlas;
|
|
Skeleton skeleton;
|
|
Skeleton skeleton;
|
|
AnimationState state;
|
|
AnimationState state;
|
|
-
|
|
|
|
|
|
+
|
|
Vector3 cameraCoords = new Vector3();
|
|
Vector3 cameraCoords = new Vector3();
|
|
Vector2 boneCoords = new Vector2();
|
|
Vector2 boneCoords = new Vector2();
|
|
|
|
|
|
@@ -66,21 +64,21 @@ public class IKTest extends ApplicationAdapter {
|
|
// Load the texture atlas and skeleton data
|
|
// Load the texture atlas and skeleton data
|
|
atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy-pma.atlas"));
|
|
atlas = new TextureAtlas(Gdx.files.internal("spineboy/spineboy-pma.atlas"));
|
|
SkeletonBinary json = new SkeletonBinary(atlas);
|
|
SkeletonBinary json = new SkeletonBinary(atlas);
|
|
- json.setScale(0.6f);
|
|
|
|
|
|
+ json.setScale(0.6f);
|
|
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/spineboy-pro.skel"));
|
|
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("spineboy/spineboy-pro.skel"));
|
|
|
|
|
|
// Create a skeleton from the skeleton data
|
|
// Create a skeleton from the skeleton data
|
|
skeleton = new Skeleton(skeletonData);
|
|
skeleton = new Skeleton(skeletonData);
|
|
skeleton.setPosition(250, 20);
|
|
skeleton.setPosition(250, 20);
|
|
-
|
|
|
|
|
|
+
|
|
// Create an animation satte
|
|
// Create an animation satte
|
|
AnimationStateData stateData = new AnimationStateData(skeletonData);
|
|
AnimationStateData stateData = new AnimationStateData(skeletonData);
|
|
state = new AnimationState(stateData);
|
|
state = new AnimationState(stateData);
|
|
-
|
|
|
|
|
|
+
|
|
// Queue the "walk" animation on the first track.
|
|
// Queue the "walk" animation on the first track.
|
|
state.setAnimation(0, "walk", true);
|
|
state.setAnimation(0, "walk", true);
|
|
-
|
|
|
|
- // Queue the "aim" animation on a higher track.
|
|
|
|
|
|
+
|
|
|
|
+ // Queue the "aim" animation on a higher track.
|
|
// It consists of a single frame that positions
|
|
// It consists of a single frame that positions
|
|
// the back arm and gun such that they point at
|
|
// the back arm and gun such that they point at
|
|
// the "crosshair" bone. By setting this
|
|
// the "crosshair" bone. By setting this
|
|
@@ -97,10 +95,10 @@ public class IKTest extends ApplicationAdapter {
|
|
// then calculate the world transforms of every bone.
|
|
// then calculate the world transforms of every bone.
|
|
// This is needed so we can call Bone#worldToLocal()
|
|
// This is needed so we can call Bone#worldToLocal()
|
|
// later.
|
|
// later.
|
|
- state.update(Gdx.graphics.getDeltaTime());
|
|
|
|
|
|
+ state.update(Gdx.graphics.getDeltaTime());
|
|
state.apply(skeleton);
|
|
state.apply(skeleton);
|
|
skeleton.updateWorldTransform();
|
|
skeleton.updateWorldTransform();
|
|
-
|
|
|
|
|
|
+
|
|
// Position the "crosshair" bone at the mouse
|
|
// Position the "crosshair" bone at the mouse
|
|
// location. We do this before calling
|
|
// location. We do this before calling
|
|
// skeleton.updateWorldTransform() below, so
|
|
// skeleton.updateWorldTransform() below, so
|
|
@@ -110,18 +108,18 @@ public class IKTest extends ApplicationAdapter {
|
|
// When setting the crosshair bone position
|
|
// When setting the crosshair bone position
|
|
// to the mouse position, we need to translate
|
|
// to the mouse position, we need to translate
|
|
// from "mouse space" to "camera space"
|
|
// from "mouse space" to "camera space"
|
|
- // and then to "local bone space". Note that the local
|
|
|
|
|
|
+ // and then to "local bone space". Note that the local
|
|
// bone space is calculated using the bone's parent
|
|
// bone space is calculated using the bone's parent
|
|
// worldToLocal() function!
|
|
// worldToLocal() function!
|
|
cameraCoords.set(Gdx.input.getX(), Gdx.input.getY(), 0);
|
|
cameraCoords.set(Gdx.input.getX(), Gdx.input.getY(), 0);
|
|
- camera.unproject(cameraCoords); // mouse space to camera space
|
|
|
|
-
|
|
|
|
|
|
+ camera.unproject(cameraCoords); // mouse space to camera space
|
|
|
|
+
|
|
Bone crosshair = skeleton.findBone("crosshair"); // Should be cached.
|
|
Bone crosshair = skeleton.findBone("crosshair"); // Should be cached.
|
|
boneCoords.set(cameraCoords.x, cameraCoords.y);
|
|
boneCoords.set(cameraCoords.x, cameraCoords.y);
|
|
crosshair.getParent().worldToLocal(boneCoords); // camera space to local bone space
|
|
crosshair.getParent().worldToLocal(boneCoords); // camera space to local bone space
|
|
crosshair.setPosition(boneCoords.x, boneCoords.y); // override the crosshair position
|
|
crosshair.setPosition(boneCoords.x, boneCoords.y); // override the crosshair position
|
|
crosshair.setAppliedValid(false);
|
|
crosshair.setAppliedValid(false);
|
|
-
|
|
|
|
|
|
+
|
|
// Calculate final world transform with the
|
|
// Calculate final world transform with the
|
|
// crosshair bone set to the mouse cursor
|
|
// crosshair bone set to the mouse cursor
|
|
// position.
|
|
// position.
|
|
@@ -129,9 +127,9 @@ public class IKTest extends ApplicationAdapter {
|
|
|
|
|
|
// Clear the screen, update the camera and
|
|
// Clear the screen, update the camera and
|
|
// render the skeleton.
|
|
// render the skeleton.
|
|
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
+ ScreenUtils.clear(0, 0, 0, 0);
|
|
camera.update();
|
|
camera.update();
|
|
-
|
|
|
|
|
|
+
|
|
batch.getProjectionMatrix().set(camera.combined);
|
|
batch.getProjectionMatrix().set(camera.combined);
|
|
batch.begin();
|
|
batch.begin();
|
|
renderer.draw(batch, skeleton);
|
|
renderer.draw(batch, skeleton);
|