Skip to content

Commit

Permalink
feat(cameras): add max lead to fixed axis
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Sep 4, 2024
1 parent 3f3f55a commit 48cc046
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-rockets-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Add max lead option to fixed axis controller
2 changes: 2 additions & 0 deletions apps/docs/src/examples/cameras/fixed-axis-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ColliderState extends TGameState {
this.addActor(plane);

const camera = new TPerspectiveCamera(engine);
camera.lerp = 0.1;
this.addActor(camera);
// this.activeCamera = camera;
camera.cameraComponent.showDebugCamera(engine);
Expand All @@ -90,6 +91,7 @@ class ColliderState extends TGameState {
distance: 5,
axis: 'z',
leadFactor: 0.5,
maxLead: 0.9,
});
controller.attachTo(box.rootComponent);
camera.controller = controller;
Expand Down
21 changes: 19 additions & 2 deletions packages/ted/src/cameras/fixed-axis-camera-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export default class TFollowAxisCameraController implements TCameraController {

private lastPosition?: vec3;
public leadFactor = 0; // Adjustable lead factor
public maxLead = 0; // Maximum lead distance

constructor(config?: {
distance?: number;
axis?: string;
deadzone?: number;
bounds?: { min: vec3; max: vec3 };
leadFactor?: number;
maxLead?: number;
}) {
if (config?.distance !== undefined) {
this.distance = config.distance;
Expand Down Expand Up @@ -65,6 +67,10 @@ export default class TFollowAxisCameraController implements TCameraController {
if (config?.leadFactor !== undefined) {
this.leadFactor = config.leadFactor;
}

if (config?.maxLead !== undefined) {
this.maxLead = config.maxLead;
}
}

attachTo(component: TSceneComponent) {
Expand All @@ -88,9 +94,20 @@ export default class TFollowAxisCameraController implements TCameraController {
}
this.lastPosition = vec3.clone(currentPosition);

// Calculate lead position
// Calculate lead position with maximum lead
const leadPosition = vec3.create();
vec3.scaleAndAdd(leadPosition, currentPosition, velocity, this.leadFactor);
const leadVector = vec3.create();
vec3.scale(leadVector, velocity, this.leadFactor);

// Limit the lead vector to the maximum lead distance
if (this.maxLead > 0) {
const leadDistance = vec3.length(leadVector);
if (leadDistance > this.maxLead) {
vec3.scale(leadVector, leadVector, this.maxLead / leadDistance);
}
}

vec3.add(leadPosition, currentPosition, leadVector);

const distance = vec3.multiply(
vec3.create(),
Expand Down

0 comments on commit 48cc046

Please sign in to comment.