Skip to content

Commit 3541ef5

Browse files
feat: fixed camera mode
1 parent 31af3cd commit 3541ef5

File tree

6 files changed

+35
-23
lines changed

6 files changed

+35
-23
lines changed

example/EcctrlFixedCamera.png

1.43 MB
Loading

featurelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## New Features
22

3+
### (2024-6-24) FixedCamera Mode:
4+
5+
- The “FixedCamera” mode automatically rotates the camera as the character turns (similar to the controls in Coastal World). You can activate it with the following code:
6+
7+
`<Ecctrl mode="FixedCamera">`
8+
9+
[![screenshot](example/EcctrlFixedCamera.png)]
10+
311
### (2024-1-1) EcctrlMode:
412

513
- Now you can seamlessly switch between different modes by adding "mode" inside Ecctrl.

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

readme.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,13 @@
1313

1414
## New Features
1515

16-
### (2024-1-1) EcctrlMode:
16+
### (2024-6-24) FixedCamera Mode:
1717

18-
- Now you can seamlessly switch between different modes by adding "mode" inside Ecctrl.
18+
- The “FixedCamera” mode automatically rotates the camera as the character turns (similar to the controls in Coastal World). You can activate it with the following code:
1919

20-
`<Ecctrl mode="PointToMove">`
20+
`<Ecctrl mode="FixedCamera">`
2121

22-
- "PointToMove" mode is designed for click-to-move or path following features. (no needs for keyboard controls)
23-
24-
```js
25-
import { useGame } from "ecctrl";
26-
// ...
27-
const setMoveToPoint = useGame((state) => state.setMoveToPoint);
28-
// ...
29-
// call function setMoveToPoint(), whenever character needs to move
30-
setMoveToPoint(point); // "point" is a vec3 value
31-
```
32-
33-
- Here is a simple click-to-move example: [Ecctrl CodeSandbox](https://codesandbox.io/p/sandbox/ecctrl-pointtomove-m9z6xh)
34-
35-
[![screenshot](example/ecctrlClickToMove.png)](https://codesandbox.io/p/sandbox/ecctrl-pointtomove-m9z6xh)
22+
![screenshot](example/EcctrlFixedCamera.png)
3623

3724
Check out the [featurelog.md](/featurelog.md) for details on previous updates and features.
3825

@@ -130,6 +117,7 @@ EcctrlProps: {
130117
camZoomSpeed: 1, // Camera zooming speed multiplier
131118
camCollision: true, // Camera collision active/deactive
132119
camCollisionOffset: 0.7, // Camera collision offset
120+
fixedCamRotMult: 1, // Camera rotate speed multiplier (FixedCamera mode)
133121
// Follow light setups
134122
followLightPos: { x: 20, y: 30, z: 10 }, // Follow light position
135123
// Base control setups

src/Ecctrl.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
6767
camZoomSpeed = 1,
6868
camCollision = true,
6969
camCollisionOffset = 0.7,
70+
fixedCamRotMult = 1,
7071
// Follow light setups
7172
followLightPos = { x: 20, y: 30, z: 10 },
7273
// Base control setups
@@ -127,10 +128,12 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
127128
* Mode setup
128129
*/
129130
let isModePointToMove: boolean = false
131+
let isModeFixedCamera: boolean = false
130132
const setCameraBased = useGame((state) => state.setCameraBased);
131133
const getCameraBased = useGame((state) => state.getCameraBased);
132134
if (mode) {
133135
if (mode === "PointToMove") isModePointToMove = true
136+
if (mode === "FixedCamera") isModeFixedCamera = true
134137
if (mode === "CameraBasedMovement") setCameraBased(true)
135138
}
136139

@@ -486,6 +489,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
486489
releaseAllButtons()
487490
}
488491
}
492+
489493
const handleSticks = (axes: readonly number[]) => {
490494
// Gamepad first joystick trigger the EcctrlJoystick event to move the character
491495
if (Math.abs(axes[0]) > 0 || Math.abs(axes[1]) > 0) {
@@ -499,7 +503,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
499503
}
500504
// Gamepad second joystick trigger the useFollowCam event to move the camera
501505
if (Math.abs(axes[2]) > 0 || Math.abs(axes[3]) > 0) {
502-
// console.log(axes[2], axes[3]);
503506
joystickCamMove(axes[2], axes[3])
504507
}
505508
}
@@ -536,8 +539,8 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
536539
camMaxDis,
537540
camMinDis,
538541
camInitDir,
539-
camMoveSpeed,
540-
camZoomSpeed,
542+
camMoveSpeed: isModeFixedCamera ? 0 : camMoveSpeed, // Disable camera move in fixed camera mode
543+
camZoomSpeed: isModeFixedCamera ? 0 : camMoveSpeed, // Disable camera zoom in fixed camera mode
541544
camCollisionOffset
542545
};
543546

@@ -1114,6 +1117,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
11141117
collider.parent().userData && !(collider.parent().userData as userDataType).excludeEcctrlRay
11151118
))
11161119
);
1120+
11171121
/**Test shape ray */
11181122
// rayHit = world.castShape(
11191123
// currentPos,
@@ -1366,6 +1370,17 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
13661370
*/
13671371
isModePointToMove && pointToMove(delta, slopeAngle, movingObjectVelocity)
13681372

1373+
/**
1374+
* Fixed camera feature
1375+
*/
1376+
if (isModeFixedCamera) {
1377+
if (leftward) {
1378+
pivot.rotation.y += (run ? delta * sprintMult * fixedCamRotMult : delta * fixedCamRotMult)
1379+
} else if (rightward) {
1380+
pivot.rotation.y -= (run ? delta * sprintMult * fixedCamRotMult : delta * fixedCamRotMult)
1381+
}
1382+
}
1383+
13691384
/**
13701385
* Apply all the animations
13711386
*/
@@ -1466,6 +1481,7 @@ export interface EcctrlProps extends RigidBodyProps {
14661481
camZoomSpeed?: number;
14671482
camCollision?: boolean;
14681483
camCollisionOffset?: number;
1484+
fixedCamRotMult?: number;
14691485
// Follow light setups
14701486
followLightPos?: { x: number, y: number, z: number };
14711487
// Base control setups

0 commit comments

Comments
 (0)