Skip to content

Commit

Permalink
feat(physics): add linear and angular velocity to components
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Oct 1, 2024
1 parent 5c782f1 commit b338423
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-coats-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Add linear and angular velocity to scene components
4 changes: 2 additions & 2 deletions apps/docs/src/examples/cameras/fixed-axis-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ 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,8 +89,9 @@ class ColliderState extends TGameState {
const controller = new TFixedAxisCameraController({
distance: 5,
axis: 'z',
leadFactor: 0.5,
leadFactor: 50,
maxLead: 0.9,
lerpFactor: 0.9,
});
controller.attachTo(box.rootComponent);
camera.controller = controller;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

18 changes: 18 additions & 0 deletions packages/ted/src/actor-components/scene-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from '../renderer/frame-params';
import TActorComponent from './actor-component';
import type {
TPhysicsBody,
TPhysicsBodyOptions,
TPhysicsBodyType,
} from '../physics/physics-world';
Expand Down Expand Up @@ -147,4 +148,21 @@ export default class TSceneComponent extends TActorComponent {
public applyTransform() {
this.actor?.world?.updateTransform(this);
}

private _physicsBody: TPhysicsBody | undefined;

/**
* Set physics body for this component, this is automatically called by the physics world.
* Changing this will not update the physics world, use the various `set` methods instead.
*/
public updatePhysicsBody(body: TPhysicsBody | undefined) {
this._physicsBody = body;
}
public get angularVelocity(): vec3 | undefined {
return this._physicsBody?.angularVelocity;
}

public get linearVelocity(): vec3 | undefined {
return this._physicsBody?.linearVelocity;
}
}
21 changes: 7 additions & 14 deletions packages/ted/src/cameras/fixed-axis-camera-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type TBaseCamera from './base-camera';

export default class TFixedAxisCameraController
extends TBaseCameraController
implements ICameraController
{
implements ICameraController {
private component?: TSceneComponent;

// Distance from the attached component on the z axis.
Expand All @@ -23,14 +22,13 @@ export default class TFixedAxisCameraController
rotation: [number, number, number];
};
} = {
x: { distance: [1, 0, 0], rotation: [0, 90, 0] },
y: { distance: [0, 1, 0], rotation: [-90, 0, 0] },
z: { distance: [0, 0, 1], rotation: [0, 0, 0] },
};
x: { distance: [1, 0, 0], rotation: [0, 90, 0] },
y: { distance: [0, 1, 0], rotation: [-90, 0, 0] },
z: { distance: [0, 0, 1], rotation: [0, 0, 0] },
};

public bounds?: { min: vec3; max: vec3 };

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

Expand Down Expand Up @@ -107,13 +105,8 @@ export default class TFixedAxisCameraController

const currentPosition = this.component.getWorldTransform().translation;

// Calculate velocity locally
const velocity = vec3.create();
if (this.lastPosition && this.leadFactor > 0) {
vec3.subtract(velocity, currentPosition, this.lastPosition);
vec3.scale(velocity, velocity, 1 / delta);
}
this.lastPosition = vec3.clone(currentPosition);
const velocity = this.component.linearVelocity ?? [0, 0, 0];
vec3.scale(velocity, velocity, delta);

// Calculate lead position with maximum lead
const leadPosition = vec3.create();
Expand Down
3 changes: 3 additions & 0 deletions packages/ted/src/core/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ export default class TWorld {
...obj.rotation,
);

// @todo should we just update the linear and angular velocity here?
actor.rootComponent.updatePhysicsBody(obj);

break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/ted/src/physics/physics-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface TPhysicsBody {
uuid: string;
translation: [number, number, number];
rotation: [number, number, number, number];
angularVelocity: [number, number, number];
linearVelocity: [number, number, number];
}

export interface TPhysicsCollision {
Expand Down
10 changes: 10 additions & 0 deletions packages/ted/src/physics/rapier3d-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ export default class TRapier3DWorld implements TPhysicsWorld {
obj.body.rotation().z,
obj.body.rotation().w,
],
angularVelocity: [
obj.body.angvel().x,
obj.body.angvel().y,
obj.body.angvel().z,
],
linearVelocity: [
obj.body.linvel().x,
obj.body.linvel().y,
obj.body.linvel().z,
],
});
}

Expand Down

0 comments on commit b338423

Please sign in to comment.