Skip to content

Commit a1a00ab

Browse files
feat: Ecctrl now can take rapier rigidbody props
1 parent 97bb12a commit a1a00ab

File tree

3 files changed

+38
-20
lines changed

3 files changed

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

readme.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ Download [Node.js](https://nodejs.org/en/download). Run this followed commands:
5454
# Install dependencies (only the first time)
5555
npm install
5656

57-
# Run the local server at localhost:8080
57+
# Run the local server at localhost:5173
5858
npm run dev
5959

60-
# Build for production in the dist/ directory
61-
npm run build
60+
# Build for production in the example/exampleDist/ directory
61+
vite build -c vercelVite.config.js
6262
```
6363

6464
## How To Use
@@ -115,8 +115,8 @@ Here are all the default properties you can play with for `<Ecctrl>`:
115115

116116
```js
117117
// Default properties for Ecctrl
118-
props: {
119-
children,
118+
EcctrlProps: {
119+
children, // ReactNode
120120
debug: false, // Enable debug mode (require leva package)
121121
capsuleHalfHeight: 0.35, // Half-height of the character capsule
122122
capsuleRadius: 0.3, // Radius of the character capsule
@@ -151,7 +151,8 @@ props: {
151151
dampingC: 0.08, // Damping coefficient
152152
// Slope Ray setups
153153
showSlopeRayOrigin: false, // Show slope ray origin
154-
slopeRayOriginOffest: capsuleRadius, // Slope ray origin offset
154+
slopeMaxAngle = 1, // in rad, the max walkable slope angle
155+
slopeRayOriginOffest: capsuleRadius - 0.02, // Slope ray origin offset
155156
slopeRayLength: capsuleRadius + 3, // Slope ray length
156157
slopeRayDir: { x: 0, y: -1, z: 0 }, // Slope ray direction
157158
slopeUpExtraForce: 0.1, // Slope up extra force
@@ -163,11 +164,14 @@ props: {
163164
autoBalanceDampingOnY: 0.02, // Auto-balance damping on Y-axis
164165
// Animation temporary setups
165166
animated: false, // Enable animation
166-
...props,
167+
// Other rigibody props from parent
168+
// Rigidbody props can be used here,
169+
// such as position, friction, gravityScale, etc.
170+
...props
167171
}
168172

169173
// Simply change the value by doing this
170-
<Ecctrl maxVelLimit={5} jumpVel={4}>
174+
<Ecctrl maxVelLimit={5} jumpVel={4} position={[0,10,0]}>
171175
<CharacterModel />
172176
</Ecctrl>
173177
```

src/Ecctrl.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CapsuleCollider,
66
useRapier,
77
RapierRigidBody,
8+
type RigidBodyProps,
89
} from "@react-three/rapier";
910
import { useEffect, useRef, useMemo, type ReactNode } from "react";
1011
import * as THREE from "three";
@@ -55,7 +56,8 @@ export default function Ecctrl({
5556
dampingC = 0.08,
5657
// Slope Ray setups
5758
showSlopeRayOrigin = false,
58-
slopeRayOriginOffest = capsuleRadius,
59+
slopeMaxAngle = 1, // in rad
60+
slopeRayOriginOffest = capsuleRadius - 0.02,
5961
slopeRayLength = capsuleRadius + 3,
6062
slopeRayDir = { x: 0, y: -1, z: 0 },
6163
slopeUpExtraForce = 0.1,
@@ -67,6 +69,8 @@ export default function Ecctrl({
6769
autoBalanceDampingOnY = 0.02,
6870
// Animation temporary setups
6971
animated = false,
72+
// Other rigibody props from parent
73+
...props
7074
}: EcctrlProps) {
7175
const characterRef = useRef<RapierRigidBody>();
7276
const characterModelRef = useRef<THREE.Group>();
@@ -260,6 +264,12 @@ export default function Ecctrl({
260264
"Slope Ray",
261265
{
262266
showSlopeRayOrigin: false,
267+
slopeMaxAngle: {
268+
value: slopeMaxAngle,
269+
min: 0,
270+
max: 1.57,
271+
step: 0.01
272+
},
263273
slopeRayOriginOffest: {
264274
value: capsuleRadius,
265275
min: 0,
@@ -290,6 +300,7 @@ export default function Ecctrl({
290300
);
291301
// Apply debug values
292302
showSlopeRayOrigin = slopeRayDebug.showSlopeRayOrigin;
303+
slopeMaxAngle = slopeRayDebug.slopeMaxAngle;
293304
slopeRayLength = slopeRayDebug.slopeRayLength;
294305
slopeRayDir = slopeRayDebug.slopeRayDir;
295306
slopeUpExtraForce = slopeRayDebug.slopeUpExtraForce;
@@ -423,14 +434,14 @@ export default function Ecctrl({
423434
/**
424435
* Setup moving direction
425436
*/
426-
// Only apply slope extra force when slope angle is between 0.2-1, actualSlopeAngle < 1
437+
// Only apply slope extra force when slope angle is between 0.2 and slopeMaxAngle, actualSlopeAngle < slopeMaxAngle
427438
if (
428-
actualSlopeAngle < 1 &&
439+
actualSlopeAngle < slopeMaxAngle &&
429440
Math.abs(slopeAngle) > 0.2 &&
430-
Math.abs(slopeAngle) < 1
441+
Math.abs(slopeAngle) < slopeMaxAngle
431442
) {
432443
movingDirection.set(0, Math.sin(slopeAngle), Math.cos(slopeAngle));
433-
} else if (actualSlopeAngle >= 1) {
444+
} else if (actualSlopeAngle >= slopeMaxAngle) {
434445
movingDirection.set(
435446
0,
436447
Math.sin(slopeAngle) > 0 ? 0 : Math.sin(slopeAngle),
@@ -757,7 +768,7 @@ export default function Ecctrl({
757768
// );
758769

759770
if (rayHit && rayHit.toi < floatingDis + rayHitForgiveness) {
760-
if (slopeRayHit && actualSlopeAngle < 1) {
771+
if (slopeRayHit && actualSlopeAngle < slopeMaxAngle) {
761772
canJump = true;
762773
}
763774
} else {
@@ -778,7 +789,7 @@ export default function Ecctrl({
778789
const rayHitObjectBodyType = rayHit.collider.parent().bodyType();
779790
const rayHitObjectBodyMass = rayHit.collider.parent().mass();
780791
// Body type 0 is rigid body, body type 1 is fixed body, body type 2 is kinematic body
781-
// And iff it stands on big mass object (>0.5)
792+
// And if it stands on big mass object (>0.5)
782793
if (
783794
(rayHitObjectBodyType === 0 || rayHitObjectBodyType === 2) &&
784795
rayHitObjectBodyMass > 0.5
@@ -975,11 +986,11 @@ export default function Ecctrl({
975986
return (
976987
<RigidBody
977988
colliders={false}
978-
position={[0, 5, 0]}
979-
friction={-0.5}
980-
gravityScale={1.2}
981989
canSleep={false}
982990
ref={characterRef}
991+
position={props.position || [0, 5, 0]}
992+
friction={props.friction || -0.5}
993+
{...props}
983994
>
984995
<CapsuleCollider args={[capsuleHalfHeight, capsuleRadius]} />
985996
<group ref={characterModelRef} userData={{ camExcludeCollision: true }}>
@@ -1003,7 +1014,7 @@ export default function Ecctrl({
10031014
);
10041015
}
10051016

1006-
export type EcctrlProps = {
1017+
export interface EcctrlProps extends RigidBodyProps {
10071018
children?: ReactNode;
10081019
debug?: boolean;
10091020
capsuleHalfHeight?: number;
@@ -1039,6 +1050,7 @@ export type EcctrlProps = {
10391050
dampingC?: number;
10401051
// Slope Ray setups
10411052
showSlopeRayOrigin?: boolean;
1053+
slopeMaxAngle?: number;
10421054
slopeRayOriginOffest?: number;
10431055
slopeRayLength?: number;
10441056
slopeRayDir?: { x: number; y: number; z: number };
@@ -1051,4 +1063,6 @@ export type EcctrlProps = {
10511063
autoBalanceDampingOnY?: number;
10521064
// Animation temporary setups
10531065
animated?: boolean;
1066+
// Other rigibody props from parent
1067+
props?: RigidBodyProps;
10541068
};

0 commit comments

Comments
 (0)