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 c7d7ec0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 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
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;
}
}
10 changes: 2 additions & 8 deletions packages/ted/src/cameras/fixed-axis-camera-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default class TFixedAxisCameraController

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 +106,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, 1 / 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 c7d7ec0

Please sign in to comment.