Skip to content

Commit

Permalink
fix physics when motion ended
Browse files Browse the repository at this point in the history
  • Loading branch information
culdo committed Dec 25, 2024
1 parent 86bd81a commit 4b0c1b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
26 changes: 4 additions & 22 deletions app/components/three-world/renderLoop/useRenderLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { useRef } from "react";
function useRenderLoop() {

const helper = useGlobalStore(state => state.helper)
const physics = usePresetStore(state => state.physics)
const player = useGlobalStore(state => state.player)
const runtimeCharacter = useGlobalStore(state => state.runtimeCharacter)
const controls = useGlobalStore(state => state.controls)
const isMotionUpdating = useGlobalStore(state => state.isMotionUpdating)

const prevTime = 0.0
const prevTimeRef = useRef(prevTime)
const prevTimeRef = useRef(0.0)

useFrame(() => {
if (!runtimeCharacter || !player) {
Expand All @@ -24,23 +22,11 @@ function useRenderLoop() {
const delta = currTime - prevTimeRef.current;

if (Math.abs(delta) > 0) {
// check if time seeking using player control
if (Math.abs(delta) > 1.0) {
helper.enable('physics', false);
}

// camera motion updating
isMotionUpdating.current = true
// character motion updating
helper.update(delta, currTime);

// check if time seeking using player control
if (Math.abs(delta) > 1.0) {
runtimeCharacter.physics.reset();
helper.enable('physics', physics);
}
prevTimeRef.current = currTime

} else {
isMotionUpdating.current = false
if (controls.autoRotate) {
Expand All @@ -51,15 +37,11 @@ function useRenderLoop() {
}
}

// stop when motion is finished and then fix physics
// stop when motion is finished
if (runtimeCharacter.looped) {
player.pause();
player.currentTime = 0.0

runtimeCharacter.physics.reset();
runtimeCharacter.physics.update(0.1)

runtimeCharacter.looped = false;
player.pause()
runtimeCharacter.looped = false
}
}, 1)
}
Expand Down
26 changes: 23 additions & 3 deletions app/modules/MMDAnimationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'three';
import { CCDIKSolver } from 'three/examples/jsm/animation/CCDIKSolver.js';
import { MMDPhysics, MMDPhysicsParameter } from 'three/examples/jsm/animation/MMDPhysics.js';
import AmmoCls from "ammojs-typed";

/**
* MMDAnimationHelper handles animation of MMD assets loaded by MMDLoader
Expand All @@ -33,6 +34,7 @@ class MMDAnimationHelper {
masterPhysics: null;
looped: boolean;
animations: any;
zeroVector: AmmoCls.btVector3;

/**
* @param {Object} params - (optional)
Expand All @@ -56,6 +58,8 @@ class MMDAnimationHelper {
this.cameraTarget.name = 'target';
this.cameraTarget.userData.frameNum = 0;

this.zeroVector = new Ammo.btVector3();

this.objects = new WeakMap();

this.configuration = {
Expand Down Expand Up @@ -509,7 +513,7 @@ class MMDAnimationHelper {
// alternate solution to save/restore bones but less performant?
//mesh.pose();
//this._updatePropertyMixersBuffer( mesh );

this._restoreBones(mesh);

mixer.setTime(time);
Expand Down Expand Up @@ -547,8 +551,11 @@ class MMDAnimationHelper {

}

if (delta > 0 && physics && this.enabled.physics && !this.sharedPhysics) {

if (Math.abs(delta) > 0 && physics && this.enabled.physics && !this.sharedPhysics) {
// reset physic when time seeking
if (Math.abs(delta) > 1.0) {
this.reset(time);
}
physics.update(delta);

}
Expand Down Expand Up @@ -892,6 +899,19 @@ class MMDAnimationHelper {
}
}

reset(currTime: number) {
for (const mesh of this.meshes) {
const physics = this.objects.get(mesh).physics;
this.update(0.0, currTime);
physics.reset();
for(const rigidBody of physics.bodies) {
rigidBody.body.clearForces()
rigidBody.body.setLinearVelocity(this.zeroVector)
rigidBody.body.setAngularVelocity(this.zeroVector)
}
}
}


}

Expand Down

0 comments on commit 4b0c1b1

Please sign in to comment.