Skip to content

Commit

Permalink
[libgdx] Added skeleton.update(delta) to examples, even when physics …
Browse files Browse the repository at this point in the history
…is not used.
  • Loading branch information
NathanSweet committed Jan 18, 2024
1 parent d03f535 commit 55550c3
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public PointAttachment newPointAttachment (Skin skin, String name) {
float time = 0;
while (time < animation.getDuration()) {
animation.apply(skeleton, time, time, false, null, 1, MixBlend.first, MixDirection.in);
skeleton.update(fps);
skeleton.updateWorldTransform(Physics.update);

System.out.println(animation.getName() + "," //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public void render () {

animation.apply(skeleton, time, time, true, events, 1, MixBlend.first, MixDirection.in);
skeleton.x += 8 * delta;
skeleton.update(delta);
skeleton.updateWorldTransform(Physics.update);
skeletonRenderer.draw(batch, skeleton);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,39 +79,29 @@ public void create () {
// Queue the "walk" animation on the first track.
state.setAnimation(0, "walk", true);

// Queue the "aim" animation on a higher track.
// It consists of a single frame that positions
// the back arm and gun such that they point at
// the "crosshair" bone. By setting this
// animation on a higher track, it overrides
// any changes to the back arm and gun made
// by the walk animation, allowing us to
// mix the two. The mouse position following
// is performed in the render() method below.
// Queue the "aim" animation on a higher track. It consists of a single frame that positions the back arm and gun such that
// they point at the "crosshair" bone. By setting this animation on a higher track, it overrides any changes to the back arm
// and gun made by the walk animation, allowing us to mix the two. The mouse position following is performed in the render()
// method below.
state.setAnimation(1, "aim", true);
}

public void render () {
// Update and apply the animations to the skeleton,
// then calculate the world transforms of every bone.
// This is needed so we can call Bone#worldToLocal()
// later.
state.update(Gdx.graphics.getDeltaTime());
// Update and apply the animations to the skeleton, then calculate the world transforms of every bone. This is needed so we
// can call Bone#worldToLocal() later.
float delta = Gdx.graphics.getDeltaTime();
state.update(delta);
state.apply(skeleton);
skeleton.updateWorldTransform(Physics.update);
skeleton.update(delta);
// This example has no physics, but if it did we first pose the skeleton without physics.
skeleton.updateWorldTransform(Physics.pose);

// Position the "crosshair" bone at the mouse
// location. We do this before calling
// skeleton.updateWorldTransform() below, so
// our change is incorporated before the IK
// constraint is applied.
// Position the "crosshair" bone at the mouse location. We do this before calling skeleton.updateWorldTransform() below, so
// our change is incorporated before the IK constraint is applied.
//
// When setting the crosshair bone position
// to the mouse position, we need to translate
// from "mouse space" to "camera space"
// and then to "local bone space". Note that the local
// bone space is calculated using the bone's parent
// worldToLocal() function!
// When setting the crosshair bone position to the mouse position, we need to translate from "mouse space" to "camera space"
// and then to "local bone space". Note that the local bone space is calculated using the bone's parent worldToLocal()
// function!
cameraCoords.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(cameraCoords); // mouse space to camera space

Expand All @@ -120,13 +110,10 @@ public void render () {
crosshair.getParent().worldToLocal(boneCoords); // camera space to local bone space
crosshair.setPosition(boneCoords.x, boneCoords.y); // override the crosshair position

// Calculate final world transform with the
// crosshair bone set to the mouse cursor
// position.
// Calculate final world transform with the crosshair bone set to the mouse cursor position. Update physics this time.
skeleton.updateWorldTransform(Physics.update);

// Clear the screen, update the camera and
// render the skeleton.
// Clear the screen, update the camera and render the skeleton.
ScreenUtils.clear(0, 0, 0, 0);
camera.update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ public void create () {
}

public void render () {
state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
float delta = Gdx.graphics.getDeltaTime();
state.update(delta); // Update the animation time.

ScreenUtils.clear(0, 0, 0, 0);

state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.update(delta);
skeleton.updateWorldTransform(Physics.update); // Uses the bones' local SRT to compute their world SRT.

// Configure the camera, and PolygonSpriteBatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ public boolean touchUp (int screenX, int screenY, int pointer, int button) {

public void render () {
float lastTime = time;
time += Gdx.graphics.getDeltaTime();
float delta = Gdx.graphics.getDeltaTime();
time += delta;
if (animation != null) animation.apply(skeleton, lastTime, time, true, null, 1, MixBlend.first, MixDirection.in);
skeleton.update(delta);
skeleton.updateWorldTransform(Physics.update);

lightPosition.x = Gdx.input.getX();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void create () {
int frame = 1;
while (time < animation.getDuration()) {
animation.apply(skeleton, time, time, false, null, 1, MixBlend.first, MixDirection.in);
skeleton.update(fps);
skeleton.updateWorldTransform(Physics.update);

// Render the skeleton to the FBO.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ public void create () {
}

public void render () {
state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
float delta = Gdx.graphics.getDeltaTime();
state.update(delta); // Update the animation time.

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.update(delta); // Advance the skeleton time. This is needed when the skeleton has physics.
skeleton.updateWorldTransform(Physics.update); // Uses the bones' local SRT to compute their world SRT.

// Configure the camera, SpriteBatch, and SkeletonRendererDebug.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public void create () {
}

public void render () {
state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
float delta = Gdx.graphics.getDeltaTime();
state.update(delta); // Update the animation time.

ScreenUtils.clear(0, 0, 0, 0);

skeleton.update(delta); // Advance the skeleton time. This is needed when the skeleton has physics.
if (state.apply(skeleton)) // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.updateWorldTransform(Physics.update); // Uses the bones' local SRT to compute their world SRT.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ public void render () {
state.addAnimation(0, "run", true, 0); // Run after the jump.
}

state.update(Gdx.graphics.getDeltaTime()); // Update the animation time.
float delta = Gdx.graphics.getDeltaTime();
state.update(delta); // Update the animation time.

state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.update(delta); // Advance the skeleton time. This is needed when the skeleton has physics.
skeleton.updateWorldTransform(Physics.update); // Uses the bones' local SRT to compute their world SRT.

// Configure the camera, SpriteBatch, and SkeletonRendererDebug.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ public void create () {
}

public void render () {
spineboyState.update(Gdx.graphics.getDeltaTime());
float delta = Gdx.graphics.getDeltaTime();
spineboyState.update(delta);
spineboyState.apply(spineboy);
spineboy.update(delta);
spineboy.updateWorldTransform(Physics.update);

goblinState.update(Gdx.graphics.getDeltaTime());
goblinState.apply(goblin);
goblin.update(delta);
goblin.updateWorldTransform(Physics.update, attachmentBone);

ScreenUtils.clear(0, 0, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@

/** Boilerplate for basic skeleton rendering, used for various testing. */
public class TestHarness extends ApplicationAdapter {
// static String JSON = "coin/coin-pro.json";
// static String ATLAS = "coin/coin-pma.atlas";

static String JSON = "raptor/raptor-pro.json";
static String ATLAS = "raptor/raptor-pma.atlas";

Expand Down Expand Up @@ -85,10 +82,11 @@ public void create () {
}

public void render () {
if (Gdx.input.justTouched()) {
state.update(0.25f); // Update the animation time.
}
float delta = 0;
if (Gdx.input.justTouched()) delta = 0.25f;
state.update(delta); // Update the animation time.
state.apply(skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
skeleton.update(delta); // Advance the skeleton time. This is needed when the skeleton has physics.
skeleton.updateWorldTransform(Physics.update); // Uses the bones' local SRT to compute their world SRT.

ScreenUtils.clear(0, 0, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void render () {
walkAnimation.apply(skeleton, time, time, true, events, 1, MixBlend.first, MixDirection.in);
}

skeleton.update(delta);
skeleton.updateWorldTransform(Physics.update);

batch.begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public void setToSetupPose () {
mix = data.mix;
}

public void translate () {
}

/** Applies the constraint to the constrained bones. */
public void update (Physics physics) {
float mix = this.mix;
Expand Down

0 comments on commit 55550c3

Please sign in to comment.