Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7213

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T01:44:07+00:00 2024-11-28T01:44:07+00:00

Using XState to coordinate Three.js character animations

  • 60k

Disclaimer: This is not a definitive guide, improvements and feedback are more than welcome!

When creating a character with a complex animation flow, it is a common practice to use a finite-state machine. Popular game engines, such as Unity and Unreal, have built-in solutions to handle state machines in order to queue animation clips. When using JS, however, developers can pick from a multitude of libraries, including XState.

XState is a JS/TS library for state machine modelling and orchestration. It's easy to get started with, but its learning curve gets steeper quickly. Therefore, getting this done was a journey searching through forums, documentations and trial and error.

Goal

Create a character that walks, runs and dances, sits down and does push-ups.

Resources used

  • Three.js
  • XState
  • A Humanoid model
  • Ten individual animations:
    • Regular animations: Dancing, Idle, Running, Pushing-up, Sitting and Walking
    • State-Transition animations: Idle to Push-up, Push-up to Idle, Sit to Stand and Stand to Sit

Relevant XState Concepts

  • State: The machine's status or mode.
  • Events and Transitions: Event is a signal that causes a transition. Transition is a change from one state to another.
  • Actions: It's an effect fired when a transition happens, be it on entry or exit.
  • Context: Data stored in a state machine

Assumptions

  • The character should start from Idle state
  • The character should be able to walk, run, do push-ups and sit down from Idle
  • Regular animations should run in loop, until another action is fired
  • All State-Transition animations should be uninterruptable
  • “Idle to Push-up” and “Stand to Sit” should run once before looping “Pushing-up” and “Sitting”, respectively, effectively starts
  • When transitioning from “Pushing-up” or “Sitting”, “Push-up to Idle” and “Stand to Sit” should run once

Solution

Character model and animations were loaded using Three.js.

State Machine

Mermaid diagram of the state machine. A more detailed version can be seen at Stately.ai.

State Machine Context

  • currentAction: The Animation to be played
  • velocity: How fast should the model move

Setting up State-Transition animations

(     [       'Idle to Push-up',       'Push-up to Idle',       'Sit to Stand',       'Stand to Sit',       'Open Door',     ] as HumanoidActions[]   ).forEach((actionName) => {     actions[actionName].loop = THREE.LoopOnce; // important for triggering finished event     actions[actionName].clampWhenFinished = true;   }); 
Enter fullscreen mode Exit fullscreen mode

Playing an uninterruptable action

In order to play an uninterruptable action, a promise actor will be created. It reads the action to be played from input and adds an event listener to mixer that will be triggered once the action is finished.

playUninterruptableAction: fromPromise(         async ({ input }: { input: { action: HumanoidActions } }) =>           new Promise((resolve) => {             playAction(input?.action);             const callback = () => {               mixer.removeEventListener('finished', callback);               resolve(undefined);             };             mixer.addEventListener('finished', callback);           }),       ), 
Enter fullscreen mode Exit fullscreen mode

Instancing and Listening to State Machine changes

const actor = createActor(setupHumanoidMachine(crossfadeMixer)).start();  actor.subscribe((state) => {    if (state.context.currentAction === previousSnapshot?.context.currentAction) return;   crossfadeMixer.playAction(state.context.currentAction);   previousSnapshot = state; }); 
Enter fullscreen mode Exit fullscreen mode

Controls

In this case, PressControls were implemented. Every time the user presses anywhere in the screen, an event of type Walk or Run, depending on how far away from the character it clicked, will be sent to the actor.

const controls = new HumanoidControls(   mesh,   intersectionObject,   (type: Events) => actor.send({ type }),   mouse, ); 
Enter fullscreen mode Exit fullscreen mode

Result

  • Live demo
  • Code

beginnersgamedevjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.