Skip to content

Commit

Permalink
(sys_mimic) Exponential easing
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Mar 8, 2024
1 parent a5d49c0 commit 1424753
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/blueprints/blu_camera_follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Game} from "../game.js";
export function blueprint_camera_follow(game: Game) {
return [
transform(),
mimic(first_named(game.World, "camera anchor")),
mimic(first_named(game.World, "camera anchor"), 0.2),
children([
transform(
[50, 50, 50],
Expand Down
6 changes: 3 additions & 3 deletions src/components/com_mimic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import {Has} from "../world.js";
export interface Mimic {
/** Entity whose transform to mimic. */
Target: Entity;
/** How laggy vs. precise is the mimicking [0-1]. */
/** The stiffness of the follower, eased exponentially. */
Stiffness: number;
}

/**
* Add `Mimic` to an entity.
*
* @param target Entity whose transform to mimic.
* @param stiffness How laggy vs. precise is the mimicking [0-1].
* @param stiffness The time in seconds that it takes the followers to get closer to the target by a factor of e.
*/
export function mimic(target: Entity, stiffness: number = 0.1) {
export function mimic(target: Entity, stiffness: number = 1) {
return (game: Game, entity: Entity) => {
game.World.Signature[entity] |= Has.Mimic;
game.World.Mimic[entity] = {
Expand Down
10 changes: 6 additions & 4 deletions src/systems/sys_mimic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const QUERY = Has.Transform | Has.Mimic;
export function sys_mimic(game: Game, delta: number) {
for (let ent = 0; ent < game.World.Signature.length; ent++) {
if ((game.World.Signature[ent] & QUERY) === QUERY) {
update(game, ent);
update(game, ent, delta);
}
}
}

let target_position: Vec3 = [0, 0, 0];
let target_rotation: Quat = [0, 0, 0, 1];

function update(game: Game, entity: Entity) {
function update(game: Game, entity: Entity, delta: number) {
// Follower must be a top-level transform for this to work.
let transform = game.World.Transform[entity];
let mimic = game.World.Mimic[entity];
Expand All @@ -34,8 +34,10 @@ function update(game: Game, entity: Entity) {
mat4_get_translation(target_position, target_transform.World);
mat4_get_rotation(target_rotation, target_transform.World);

vec3_lerp(transform.Translation, transform.Translation, target_position, mimic.Stiffness);
quat_slerp(transform.Rotation, transform.Rotation, target_rotation, mimic.Stiffness);
// https://lisyarus.github.io/blog/programming/2023/02/21/exponential-smoothing.html
let t = 1 - Math.exp(-delta / mimic.Stiffness);
vec3_lerp(transform.Translation, transform.Translation, target_position, t);
quat_slerp(transform.Rotation, transform.Rotation, target_rotation, t);

game.World.Signature[entity] |= Has.Dirty;
}

0 comments on commit 1424753

Please sign in to comment.