Skip to content

Commit

Permalink
feat: fixed camera mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ErdongChen-Andrew committed Jun 25, 2024
1 parent 31af3cd commit 3541ef5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
Binary file added example/EcctrlFixedCamera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions featurelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## New Features

### (2024-6-24) FixedCamera Mode:

- 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:

`<Ecctrl mode="FixedCamera">`

[![screenshot](example/EcctrlFixedCamera.png)]

### (2024-1-1) EcctrlMode:

- Now you can seamlessly switch between different modes by adding "mode" inside Ecctrl.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecctrl",
"version": "1.0.80",
"version": "1.0.81",
"author": "Erdong Chen",
"license": "MIT",
"description": "A floating rigibody character controller for R3F",
Expand Down
22 changes: 5 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,13 @@

## New Features

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

- Now you can seamlessly switch between different modes by adding "mode" inside Ecctrl.
- 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:

`<Ecctrl mode="PointToMove">`
`<Ecctrl mode="FixedCamera">`

- "PointToMove" mode is designed for click-to-move or path following features. (no needs for keyboard controls)

```js
import { useGame } from "ecctrl";
// ...
const setMoveToPoint = useGame((state) => state.setMoveToPoint);
// ...
// call function setMoveToPoint(), whenever character needs to move
setMoveToPoint(point); // "point" is a vec3 value
```

- Here is a simple click-to-move example: [Ecctrl CodeSandbox](https://codesandbox.io/p/sandbox/ecctrl-pointtomove-m9z6xh)

[![screenshot](example/ecctrlClickToMove.png)](https://codesandbox.io/p/sandbox/ecctrl-pointtomove-m9z6xh)
![screenshot](example/EcctrlFixedCamera.png)

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

Expand Down Expand Up @@ -130,6 +117,7 @@ EcctrlProps: {
camZoomSpeed: 1, // Camera zooming speed multiplier
camCollision: true, // Camera collision active/deactive
camCollisionOffset: 0.7, // Camera collision offset
fixedCamRotMult: 1, // Camera rotate speed multiplier (FixedCamera mode)
// Follow light setups
followLightPos: { x: 20, y: 30, z: 10 }, // Follow light position
// Base control setups
Expand Down
22 changes: 19 additions & 3 deletions src/Ecctrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
camZoomSpeed = 1,
camCollision = true,
camCollisionOffset = 0.7,
fixedCamRotMult = 1,
// Follow light setups
followLightPos = { x: 20, y: 30, z: 10 },
// Base control setups
Expand Down Expand Up @@ -127,10 +128,12 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
* Mode setup
*/
let isModePointToMove: boolean = false
let isModeFixedCamera: boolean = false
const setCameraBased = useGame((state) => state.setCameraBased);
const getCameraBased = useGame((state) => state.getCameraBased);
if (mode) {
if (mode === "PointToMove") isModePointToMove = true
if (mode === "FixedCamera") isModeFixedCamera = true
if (mode === "CameraBasedMovement") setCameraBased(true)
}

Expand Down Expand Up @@ -486,6 +489,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
releaseAllButtons()
}
}

const handleSticks = (axes: readonly number[]) => {
// Gamepad first joystick trigger the EcctrlJoystick event to move the character
if (Math.abs(axes[0]) > 0 || Math.abs(axes[1]) > 0) {
Expand All @@ -499,7 +503,6 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
}
// Gamepad second joystick trigger the useFollowCam event to move the camera
if (Math.abs(axes[2]) > 0 || Math.abs(axes[3]) > 0) {
// console.log(axes[2], axes[3]);
joystickCamMove(axes[2], axes[3])
}
}
Expand Down Expand Up @@ -536,8 +539,8 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
camMaxDis,
camMinDis,
camInitDir,
camMoveSpeed,
camZoomSpeed,
camMoveSpeed: isModeFixedCamera ? 0 : camMoveSpeed, // Disable camera move in fixed camera mode
camZoomSpeed: isModeFixedCamera ? 0 : camMoveSpeed, // Disable camera zoom in fixed camera mode
camCollisionOffset
};

Expand Down Expand Up @@ -1114,6 +1117,7 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
collider.parent().userData && !(collider.parent().userData as userDataType).excludeEcctrlRay
))
);

/**Test shape ray */
// rayHit = world.castShape(
// currentPos,
Expand Down Expand Up @@ -1366,6 +1370,17 @@ const Ecctrl: ForwardRefRenderFunction<RapierRigidBody, EcctrlProps> = ({
*/
isModePointToMove && pointToMove(delta, slopeAngle, movingObjectVelocity)

/**
* Fixed camera feature
*/
if (isModeFixedCamera) {
if (leftward) {
pivot.rotation.y += (run ? delta * sprintMult * fixedCamRotMult : delta * fixedCamRotMult)
} else if (rightward) {
pivot.rotation.y -= (run ? delta * sprintMult * fixedCamRotMult : delta * fixedCamRotMult)
}
}

/**
* Apply all the animations
*/
Expand Down Expand Up @@ -1466,6 +1481,7 @@ export interface EcctrlProps extends RigidBodyProps {
camZoomSpeed?: number;
camCollision?: boolean;
camCollisionOffset?: number;
fixedCamRotMult?: number;
// Follow light setups
followLightPos?: { x: number, y: number, z: number };
// Base control setups
Expand Down

0 comments on commit 3541ef5

Please sign in to comment.