Skip to content

Commit 5ebad15

Browse files
fix: falling gravity scale perf issue
1 parent 56e431b commit 5ebad15

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ecctrl",
3-
"version": "1.0.46",
3+
"version": "1.0.47",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",

src/Ecctrl.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ const getMovingDirection = (forward: boolean,
3232
leftward: boolean,
3333
rightward: boolean,
3434
pivot: THREE.Object3D)
35-
:number | null => {
36-
if (!forward && !backward && !leftward && !rightward) return null;
37-
if (forward && leftward) return pivot.rotation.y + Math.PI / 4;
38-
if (forward && rightward) return pivot.rotation.y - Math.PI / 4;
39-
if (backward && leftward) return pivot.rotation.y - Math.PI / 4 + Math.PI;
40-
if (backward && rightward) return pivot.rotation.y + Math.PI / 4 + Math.PI;
41-
if (backward) return pivot.rotation.y + Math.PI;
42-
if (leftward) return pivot.rotation.y + Math.PI / 2;
43-
if (rightward) return pivot.rotation.y - Math.PI / 2;
44-
if (forward) return pivot.rotation.y;
35+
: number | null => {
36+
if (!forward && !backward && !leftward && !rightward) return null;
37+
if (forward && leftward) return pivot.rotation.y + Math.PI / 4;
38+
if (forward && rightward) return pivot.rotation.y - Math.PI / 4;
39+
if (backward && leftward) return pivot.rotation.y - Math.PI / 4 + Math.PI;
40+
if (backward && rightward) return pivot.rotation.y + Math.PI / 4 + Math.PI;
41+
if (backward) return pivot.rotation.y + Math.PI;
42+
if (leftward) return pivot.rotation.y + Math.PI / 2;
43+
if (rightward) return pivot.rotation.y - Math.PI / 2;
44+
if (forward) return pivot.rotation.y;
4545
};
4646

4747
const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
@@ -407,6 +407,7 @@ const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
407407
// can jump setup
408408
let canJump = false;
409409
let isFalling = false;
410+
const initialGravityScale = useMemo(() => props.gravityScale || 1, [])
410411

411412
// on moving object state
412413
let isOnMovingObject = false;
@@ -826,7 +827,7 @@ const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
826827

827828
// Getting moving directions (IIFE)
828829
modelEuler.y = ((movingDirection) => movingDirection === null ? modelEuler.y : movingDirection)
829-
(getMovingDirection(forward, backward, leftward, rightward, pivot ))
830+
(getMovingDirection(forward, backward, leftward, rightward, pivot))
830831

831832
// Move character to the moving direction
832833
if (forward || backward || leftward || rightward)
@@ -1093,10 +1094,13 @@ const Ecctrl = forwardRef<RapierRigidBody, EcctrlProps>(({
10931094
* Apply larger gravity when falling
10941095
*/
10951096
if (characterRef.current) {
1096-
characterRef.current.setGravityScale(
1097-
currentVel.y > fallingMaxVel ? (isFalling ? fallingGravityScale : 1) : 0,
1098-
true
1099-
)
1097+
if (currentVel.y < fallingMaxVel && characterRef.current.gravityScale() !== 0) {
1098+
characterRef.current.setGravityScale(0, true)
1099+
} else if (isFalling && characterRef.current.gravityScale() !== fallingGravityScale) {
1100+
characterRef.current.setGravityScale(fallingGravityScale, true)
1101+
} else if (!isFalling && characterRef.current.gravityScale() !== initialGravityScale) {
1102+
characterRef.current.setGravityScale(initialGravityScale, true)
1103+
}
11001104
}
11011105

11021106
/**

0 commit comments

Comments
 (0)