Files
physics/physics3d/pose.sakura
T
2026-10-30 23:21:01 -05:00

66 lines
2.0 KiB
Plaintext

#! Contains `pose`, where a body is in space and which way it's turned, which
#! places its shapes.
import std::serial;
import malunal::linear;
module malunal::physics3d;
#! Where a body is in space, and which way it's turned: what a place of its own,
#! as its shapes' corners are, is in space.
@serial
pub struct pose {
@serial pub var at: vec3;
@serial pub var turn := quat::identity;
}
#! At the origin, unturned.
pub val pose::identity := pose{};
#! A pose at a place, turned by a rotation.
pub def pose::of(at: vec3, turn: quat = quat::identity): pose {
return pose{ at = at, turn = turn };
}
pub def pose::equals(&this, other: &pose): bool {
return this.at == other.at && this.turn == other.turn;
}
#! Where a place of the body's own is in space.
pub def pose::apply(&this, local: vec3): vec3 {
return this.at + this.turn * local;
}
#! Where a place of space is in the body's own.
pub def pose::unapply(&this, place: vec3): vec3 {
return this.turn.inverse() * (place - this.at);
}
#! A direction of the body's own, turned as it's turned.
pub def pose::rotate(&this, direction: vec3): vec3 {
return this.turn * direction;
}
#! A direction of space, as the body's own.
pub def pose::unrotate(&this, direction: vec3): vec3 {
return this.turn.inverse() * direction;
}
#! It after another: the pose of what's placed by `other` within what this
#! places.
pub def pose::mul(&this, other: &pose): pose {
return pose{ at = this.apply(other.at), turn = (this.turn * other.turn).normalized() };
}
#! The pose undoing it: a place it puts somewhere, put back.
pub def pose::inverse(&this): pose {
val back := this.turn.inverse();
return pose{ at = back * -this.at, turn = back };
}
#! Another pose of space, as one within what this places, of its own places:
#! what `this * found` is `other` of.
pub def pose::unapply_pose(&this, other: pose): pose {
val back := this.turn.inverse();
return pose{ at = back * (other.at - this.at), turn = (back * other.turn).normalized() };
}