Skip to content

Commit c52d03a

Browse files
feat: point to move mode
1 parent 02b8fd4 commit c52d03a

File tree

6 files changed

+221
-88
lines changed

6 files changed

+221
-88
lines changed

example/CharacterModel.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function CharacterModel(props: CharacterModelProps) {
3636
const rightHandPos = useMemo(() => new THREE.Vector3(), []);
3737
const leftHandPos = useMemo(() => new THREE.Vector3(), []);
3838
const bodyPos = useMemo(() => new THREE.Vector3(), []);
39+
const bodyRot = useMemo(() => new THREE.Quaternion(), []);
3940
let rightHand: THREE.Object3D = null;
4041
let leftHand: THREE.Object3D = null;
4142
let mugModel: THREE.Object3D = null;
@@ -130,20 +131,25 @@ export default function CharacterModel(props: CharacterModelProps) {
130131

131132
useFrame(() => {
132133
if (curAnimation === animationSet.action4) {
133-
if (rightHand && leftHand) {
134+
if (rightHand) {
134135
rightHand.getWorldPosition(rightHandPos);
135-
leftHand.getWorldPosition(leftHandPos);
136-
rightHandRef.current.parent.getWorldPosition(bodyPos);
136+
group.current.getWorldPosition(bodyPos);
137+
group.current.getWorldQuaternion(bodyRot);
137138
}
138139

139-
// Apply both hands position to hand colliders
140-
if (rightHandColliderRef.current && leftHandColliderRef.current) {
141-
rightHandRef.current.position.copy(rightHandPos).sub(bodyPos);
140+
// Apply hands position to hand colliders
141+
if (rightHandColliderRef.current) {
142+
// check if parent group autobalance is on or off
143+
if (group.current.parent.quaternion.y === 0 && group.current.parent.quaternion.w === 1) {
144+
rightHandRef.current.position.copy(rightHandPos).sub(bodyPos).applyQuaternion(bodyRot.conjugate());
145+
} else {
146+
rightHandRef.current.position.copy(rightHandPos).sub(bodyPos);
147+
}
142148
rightHandColliderRef.current.setTranslationWrtParent(
143149
rightHandRef.current.position
144150
);
145151
}
146-
}
152+
}
147153
});
148154

149155
useEffect(() => {
@@ -190,7 +196,7 @@ export default function CharacterModel(props: CharacterModelProps) {
190196
(action as any)._mixer._listeners = [];
191197

192198
// Move hand collider back to initial position after action
193-
if (curAnimation===animationSet.action4) {
199+
if (curAnimation === animationSet.action4) {
194200
if (rightHandColliderRef.current) {
195201
rightHandColliderRef.current.setTranslationWrtParent(vec3({ x: 0, y: 0, z: 0 }))
196202
}

example/Experience.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export default function Experience() {
6464
dampingC={0.2}
6565
autoBalanceSpringK={1.2}
6666
autoBalanceDampingC={0.04}
67+
autoBalanceSpringOnY={0.7}
68+
autoBalanceDampingOnY={0.05}
6769
>
6870
{/* Replace your model here */}
6971
<CharacterModel />

example/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Canvas } from "@react-three/fiber";
44
import Experience from "../example/Experience";
55
import { Leva } from "leva";
66
import { EcctrlJoystick } from "../src/EcctrlJoystick";
7-
import { useEffect, useState } from "react";
7+
import { Suspense, useEffect, useState } from "react";
88

99
const root = ReactDOM.createRoot(document.querySelector("#root"));
1010

@@ -43,7 +43,9 @@ root.render(
4343
}
4444
}}
4545
>
46-
<Experience />
46+
<Suspense fallback={null}>
47+
<Experience />
48+
</Suspense>
4749
</Canvas>
4850
</>
4951
);

package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ecctrl",
3-
"version": "1.0.50",
3+
"version": "1.0.51",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",
@@ -33,23 +33,21 @@
3333
"@types/react": "^18.2.28",
3434
"@types/react-dom": "^18.2.8",
3535
"@vitejs/plugin-react": "^4.1.0",
36-
"r3f-perf": "^7.1.2",
37-
"react": "^18.2.0",
38-
"react-dom": "^18.2.0",
39-
"three": "^0.153.0",
4036
"typescript": "^5.2.2",
4137
"vite": "^4.3.9"
4238
},
4339
"dependencies": {
44-
"@react-three/drei": "^9",
45-
"@react-three/rapier": "^1",
46-
"leva": "^0.9.34",
47-
"zustand": "^4.4.1"
40+
"leva": "^0.9.34",
41+
"r3f-perf": "^7.1.2"
4842
},
4943
"peerDependencies": {
5044
"@react-three/fiber": "^8.13.3",
45+
"@react-three/drei": "^9",
46+
"@react-three/rapier": "^1",
5147
"react": ">=18",
52-
"three": ">=0.133"
48+
"react-dom": ">=18.0",
49+
"three": ">=0.153",
50+
"zustand": "^4.4.1"
5351
},
5452
"scripts": {
5553
"dev": "vite",

0 commit comments

Comments
 (0)