Skip to content

Commit 51937ab

Browse files
fix: reuse vectors in useFrame
1 parent 464cb75 commit 51937ab

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.76",
3+
"version": "1.0.77",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",

src/Ecctrl.tsx

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
120120
}: EcctrlProps, ref) => {
121121
const characterRef = ref as RefObject<RapierRigidBody> || useRef<RapierRigidBody>()
122122
const characterModelRef = useRef<THREE.Group>();
123-
const characterModelIndicator = useMemo(() => new THREE.Object3D(), [])
123+
const characterModelIndicator: THREE.Object3D = useMemo(() => new THREE.Object3D(), [])
124124
const defaultControllerKeys = { forward: 12, backward: 13, leftward: 14, rightward: 15, jump: 2, action1: 11, action2: 3, action3: 1, action4: 0 }
125125

126126
/**
127127
* Mode setup
128128
*/
129-
let isModePointToMove = false
129+
let isModePointToMove: boolean = false
130130
const setCameraBased = useGame((state) => state.setCameraBased);
131131
const getCameraBased = useGame((state) => state.getCameraBased);
132132
if (mode) {
@@ -137,15 +137,20 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
137137
/**
138138
* Body collider setup
139139
*/
140-
const modelFacingVec = useMemo(() => new THREE.Vector3(), []);
141-
const bodyFacingVec = useMemo(() => new THREE.Vector3(), []);
142-
const bodyBalanceVec = useMemo(() => new THREE.Vector3(), []);
143-
const bodyBalanceVecOnX = useMemo(() => new THREE.Vector3(), []);
144-
const bodyFacingVecOnY = useMemo(() => new THREE.Vector3(), []);
145-
const bodyBalanceVecOnZ = useMemo(() => new THREE.Vector3(), []);
146-
const vectorY = useMemo(() => new THREE.Vector3(0, 1, 0), []);
147-
const vectorZ = useMemo(() => new THREE.Vector3(0, 0, 1), []);
148-
const bodyContactForce = useMemo(() => new THREE.Vector3(), []);
140+
const modelFacingVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
141+
const bodyFacingVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
142+
const bodyBalanceVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
143+
const bodyBalanceVecOnX: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
144+
const bodyFacingVecOnY: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
145+
const bodyBalanceVecOnZ: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
146+
const vectorY: THREE.Vector3 = useMemo(() => new THREE.Vector3(0, 1, 0), []);
147+
const vectorZ: THREE.Vector3 = useMemo(() => new THREE.Vector3(0, 0, 1), []);
148+
const crossVecOnX: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
149+
const crossVecOnY: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
150+
const crossVecOnZ: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
151+
const bodyContactForce: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
152+
const slopeRayOriginUpdatePosition: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
153+
const camBasedMoveCrossVecOnY: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
149154

150155
// Animation change functions
151156
const idleAnimation = !animated ? null : useGame((state) => state.idle);
@@ -454,7 +459,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
454459
*/
455460
let controllerIndex: number = null
456461
const gamepadKeys = { forward: false, backward: false, leftward: false, rightward: false };
457-
const gamepadJoystickVec2 = useMemo(() => new THREE.Vector2(), [])
462+
const gamepadJoystickVec2: THREE.Vector2 = useMemo(() => new THREE.Vector2(), [])
458463
let gamepadJoystickDis: number = 0
459464
let gamepadJoystickAng: number = 0
460465
const gamepadConnect = (e: any) => { controllerIndex = e.gamepad.index }
@@ -500,20 +505,20 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
500505
}
501506

502507
// can jump setup
503-
let canJump = false;
504-
let isFalling = false;
508+
let canJump: boolean = false;
509+
let isFalling: boolean = false;
505510
const initialGravityScale: number = useMemo(() => props.gravityScale || 1, [])
506511

507512
// on moving object state
508-
let massRatio = 1;
509-
let isOnMovingObject = false;
510-
const standingForcePoint = useMemo(() => new THREE.Vector3(), []);
511-
const movingObjectDragForce = useMemo(() => new THREE.Vector3(), []);
512-
const movingObjectVelocity = useMemo(() => new THREE.Vector3(), []);
513-
const movingObjectVelocityInCharacterDir = useMemo(() => new THREE.Vector3(), []);
514-
const distanceFromCharacterToObject = useMemo(() => new THREE.Vector3(), []);
515-
const objectAngvelToLinvel = useMemo(() => new THREE.Vector3(), []);
516-
const velocityDiff = useMemo(() => new THREE.Vector3(), []);
513+
let massRatio: number = 1;
514+
let isOnMovingObject: boolean = false;
515+
const standingForcePoint: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
516+
const movingObjectDragForce: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
517+
const movingObjectVelocity: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
518+
const movingObjectVelocityInCharacterDir: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
519+
const distanceFromCharacterToObject: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
520+
const objectAngvelToLinvel: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
521+
const velocityDiff: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
517522

518523
/**
519524
* Initial light setup
@@ -541,28 +546,28 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
541546
*/
542547
const { pivot, cameraCollisionDetect, joystickCamMove } =
543548
useFollowCam(cameraSetups);
544-
const pivotPosition = useMemo(() => new THREE.Vector3(), []);
545-
const modelEuler = useMemo(() => new THREE.Euler(), []);
546-
const modelQuat = useMemo(() => new THREE.Quaternion(), []);
547-
const moveImpulse = useMemo(() => new THREE.Vector3(), []);
548-
const movingDirection = useMemo(() => new THREE.Vector3(), []);
549-
const moveAccNeeded = useMemo(() => new THREE.Vector3(), []);
550-
const jumpVelocityVec = useMemo(() => new THREE.Vector3(), []);
551-
const jumpDirection = useMemo(() => new THREE.Vector3(), []);
552-
const currentVel = useMemo(() => new THREE.Vector3(), []);
553-
const currentPos = useMemo(() => new THREE.Vector3(), []);
554-
const dragForce = useMemo(() => new THREE.Vector3(), []);
555-
const dragAngForce = useMemo(() => new THREE.Vector3(), []);
556-
const wantToMoveVel = useMemo(() => new THREE.Vector3(), []);
557-
const rejectVel = useMemo(() => new THREE.Vector3(), []);
549+
const pivotPosition: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
550+
const modelEuler: THREE.Euler = useMemo(() => new THREE.Euler(), []);
551+
const modelQuat: THREE.Quaternion = useMemo(() => new THREE.Quaternion(), []);
552+
const moveImpulse: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
553+
const movingDirection: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
554+
const moveAccNeeded: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
555+
const jumpVelocityVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
556+
const jumpDirection: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
557+
const currentVel: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
558+
const currentPos: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
559+
const dragForce: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
560+
const dragAngForce: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
561+
const wantToMoveVel: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
562+
const rejectVel: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
558563

559564
/**
560565
* Floating Ray setup
561566
*/
562567
let floatingForce = null;
563-
const springDirVec = useMemo(() => new THREE.Vector3(), []);
564-
const characterMassForce = useMemo(() => new THREE.Vector3(), []);
565-
const rayOrigin = useMemo(() => new THREE.Vector3(), []);
568+
const springDirVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
569+
const characterMassForce: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
570+
const rayOrigin: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
566571
const rayCast = new rapier.Ray(rayOrigin, rayDir);
567572
let rayHit: RayColliderToi = null;
568573

@@ -575,10 +580,10 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
575580
let slopeAngle: number = null;
576581
let actualSlopeNormal: Vector = null;
577582
let actualSlopeAngle: number = null;
578-
const actualSlopeNormalVec = useMemo(() => new THREE.Vector3(), []);
579-
const floorNormal = useMemo(() => new THREE.Vector3(0, 1, 0), []);
583+
const actualSlopeNormalVec: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
584+
const floorNormal: THREE.Vector3 = useMemo(() => new THREE.Vector3(0, 1, 0), []);
580585
const slopeRayOriginRef = useRef<THREE.Mesh>();
581-
const slopeRayorigin = useMemo(() => new THREE.Vector3(), []);
586+
const slopeRayorigin: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
582587
const slopeRayCast = new rapier.Ray(slopeRayorigin, slopeRayDir);
583588
let slopeRayHit: RayColliderToi = null;
584589

@@ -587,8 +592,8 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
587592
*/
588593
let isBodyHitWall = false;
589594
let isPointMoving = false;
590-
const crossVector = useMemo(() => new THREE.Vector3(), []);
591-
const pointToPoint = useMemo(() => new THREE.Vector3(), []);
595+
const crossVector: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
596+
const pointToPoint: THREE.Vector3 = useMemo(() => new THREE.Vector3(), []);
592597
const getMoveToPoint = useGame((state) => state.getMoveToPoint);
593598
const bodySensorRef = useRef<Collider>();
594599
const handleOnIntersectionEnter = () => {
@@ -601,7 +606,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
601606
/**
602607
* Character moving function
603608
*/
604-
let characterRotated = true;
609+
let characterRotated: boolean = true;
605610
const moveCharacter = (
606611
_: number,
607612
run: boolean,
@@ -758,16 +763,16 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
758763
modelEuler.y = pivot.rotation.y
759764
pivot.getWorldDirection(modelFacingVec)
760765
// Update slopeRayOrigin to new positon
761-
const slopeRayOriginNewPosition = new THREE.Vector3(movingDirection.x, 0, movingDirection.z)
762-
const crossVecOnY = slopeRayOriginNewPosition.clone().cross(modelFacingVec)
763-
slopeRayOriginRef.current.position.x = slopeRayOriginOffest * Math.sin(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
764-
slopeRayOriginRef.current.position.z = slopeRayOriginOffest * Math.cos(slopeRayOriginNewPosition.angleTo(modelFacingVec) * (crossVecOnY.y < 0 ? 1 : -1))
766+
slopeRayOriginUpdatePosition.set(movingDirection.x, 0, movingDirection.z)
767+
camBasedMoveCrossVecOnY.copy(slopeRayOriginUpdatePosition).cross(modelFacingVec)
768+
slopeRayOriginRef.current.position.x = slopeRayOriginOffest * Math.sin(slopeRayOriginUpdatePosition.angleTo(modelFacingVec) * (camBasedMoveCrossVecOnY.y < 0 ? 1 : -1))
769+
slopeRayOriginRef.current.position.z = slopeRayOriginOffest * Math.cos(slopeRayOriginUpdatePosition.angleTo(modelFacingVec) * (camBasedMoveCrossVecOnY.y < 0 ? 1 : -1))
765770
} else {
766771
characterModelIndicator.getWorldDirection(modelFacingVec)
767772
}
768-
const crossVecOnX = vectorY.clone().cross(bodyBalanceVecOnX);
769-
const crossVecOnY = modelFacingVec.clone().cross(bodyFacingVecOnY);
770-
const crossVecOnZ = vectorY.clone().cross(bodyBalanceVecOnZ);
773+
crossVecOnX.copy(vectorY).cross(bodyBalanceVecOnX);
774+
crossVecOnY.copy(modelFacingVec).cross(bodyFacingVecOnY);
775+
crossVecOnZ.copy(vectorY).cross(bodyBalanceVecOnZ);
771776

772777
dragAngForce.set(
773778
(crossVecOnX.x < 0 ? 1 : -1) *

0 commit comments

Comments
 (0)