oxeon 0.0.6

Rust-like scripting language for modding Bevy games, compiled to sandboxed WebAssembly.
Documentation
//! Bevy's math types in mods (`Vec2`, `Vec3`, `Quat`), against glam's own.

use bevy_math::{Quat, Vec2, Vec3};
use oxeon_runtime::{Runtime, RuntimeConfig, Value};

/// Each function returns one `f32` computed with the math types.
const MOD: &str = r#"
pub fn v2_ops() -> f32 { let a = Vec2::new(3.0, 4.0); let b = Vec2::new(-1.0, 2.0); let c = (a + b) * 2.0 - b / 2.0 + 0.5 * a; c.x * 100.0 + c.y }
pub fn v2_length() -> f32 { Vec2::new(3.0, 4.0).length() }
pub fn v2_normalize() -> f32 { Vec2::new(3.0, 4.0).normalize().y }
pub fn v2_angle() -> f32 { Vec2::from_angle(0.7).to_angle() }
pub fn v2_angle_to() -> f32 { Vec2::new(1.0, 0.0).angle_to(Vec2::new(-1.0, 1.0)) }
pub fn v2_perp_dot() -> f32 { Vec2::new(2.0, 1.0).perp_dot(Vec2::new(-1.0, 3.0)) }
pub fn v2_rotate() -> f32 { Vec2::new(2.0, 1.0).rotate(Vec2::from_angle(1.2)).x }
pub fn v2_lerp() -> f32 { Vec2::new(0.0, 10.0).lerp(Vec2::new(10.0, 0.0), 0.25).y }
pub fn v2_clamp_length() -> f32 { Vec2::new(30.0, 40.0).clamp_length_max(5.0).x }
pub fn v2_move_towards() -> f32 { Vec2::ZERO.move_towards(Vec2::new(3.0, 4.0), 2.0).y }
pub fn v3_cross() -> f32 { let c = Vec3::new(1.0, 2.0, 3.0).cross(Vec3::new(-2.0, 0.5, 4.0)); c.x * 10000.0 + c.y * 100.0 + c.z }
pub fn v3_distance() -> f32 { Vec3::new(1.0, 2.0, 3.0).distance(Vec3::new(4.0, 6.0, 3.0)) }
pub fn v3_project() -> f32 { Vec3::new(2.0, 3.0, 4.0).project_onto(Vec3::new(1.0, 1.0, 0.0)).x }
pub fn v3_reflect() -> f32 { Vec3::new(1.0, -1.0, 0.0).reflect(Vec3::Y).y }
pub fn v3_angle_between() -> f32 { Vec3::X.angle_between(Vec3::new(1.0, 1.0, 0.0)) }
pub fn v3_assign() -> f32 { let mut v = Vec3::ONE; v += Vec3::X; v *= 3.0; v -= 1.0; v.x * 10.0 + v.y }
pub fn q_rotate() -> f32 { (Quat::from_rotation_z(0.9) * Vec3::new(1.0, 2.0, 3.0)).x }
pub fn q_axis() -> f32 { (Quat::from_axis_angle(Vec3::new(0.0, 0.6, 0.8), 1.3) * Vec3::X).z }
pub fn q_compose() -> f32 { (Quat::from_rotation_x(0.4) * Quat::from_rotation_y(-1.1)).w }
pub fn q_inverse() -> f32 { (Quat::from_rotation_y(0.8).inverse() * Vec3::Z).x }
pub fn q_slerp() -> f32 { Quat::from_rotation_z(0.2).slerp(Quat::from_rotation_z(1.4), 0.3).z }
pub fn q_angle_between() -> f32 { Quat::from_rotation_x(0.3).angle_between(Quat::from_rotation_x(1.0)) }
pub fn q_to_axis_angle() -> f32 { let (axis, angle) = Quat::from_axis_angle(Vec3::new(0.6, 0.0, 0.8), 0.9).to_axis_angle(); axis.x * 10.0 + angle }
"#;

#[test]
fn math_types_match_glam() {
    let compiled = oxeon_compiler::compile_with_prelude(MOD, oxeon_compiler::bevy_prelude())
        .unwrap_or_else(|e| panic!("{:?}", e.iter().map(|d| &d.message).collect::<Vec<_>>()));
    let runtime = Runtime::new(RuntimeConfig::default()).unwrap();
    let module = runtime.load(&compiled.wasm).unwrap();
    let mut instance = runtime.instantiate(&module).unwrap();
    let mut call = |f: &str| match instance.call(f, &[]).unwrap()[..] {
        [Value::F32(v)] => v,
        ref other => panic!("{f}: {other:?}"),
    };
    let (a, b) = (Vec2::new(3.0, 4.0), Vec2::new(-1.0, 2.0));
    let c = (a + b) * 2.0 - b / 2.0 + 0.5 * a;
    let cross = Vec3::new(1.0, 2.0, 3.0).cross(Vec3::new(-2.0, 0.5, 4.0));
    let mut v = Vec3::ONE;
    v += Vec3::X;
    v *= 3.0;
    v -= 1.0;
    let (axis, angle) = Quat::from_axis_angle(Vec3::new(0.6, 0.0, 0.8), 0.9).to_axis_angle();
    let expected: &[(&str, f32)] = &[
        ("v2_ops", c.x * 100.0 + c.y),
        ("v2_length", a.length()),
        ("v2_normalize", a.normalize().y),
        ("v2_angle", Vec2::from_angle(0.7).to_angle()),
        (
            "v2_angle_to",
            Vec2::new(1.0, 0.0).angle_to(Vec2::new(-1.0, 1.0)),
        ),
        (
            "v2_perp_dot",
            Vec2::new(2.0, 1.0).perp_dot(Vec2::new(-1.0, 3.0)),
        ),
        (
            "v2_rotate",
            Vec2::new(2.0, 1.0).rotate(Vec2::from_angle(1.2)).x,
        ),
        (
            "v2_lerp",
            Vec2::new(0.0, 10.0).lerp(Vec2::new(10.0, 0.0), 0.25).y,
        ),
        (
            "v2_clamp_length",
            Vec2::new(30.0, 40.0).clamp_length_max(5.0).x,
        ),
        (
            "v2_move_towards",
            Vec2::ZERO.move_towards(Vec2::new(3.0, 4.0), 2.0).y,
        ),
        ("v3_cross", cross.x * 10000.0 + cross.y * 100.0 + cross.z),
        (
            "v3_distance",
            Vec3::new(1.0, 2.0, 3.0).distance(Vec3::new(4.0, 6.0, 3.0)),
        ),
        (
            "v3_project",
            Vec3::new(2.0, 3.0, 4.0)
                .project_onto(Vec3::new(1.0, 1.0, 0.0))
                .x,
        ),
        ("v3_reflect", Vec3::new(1.0, -1.0, 0.0).reflect(Vec3::Y).y),
        (
            "v3_angle_between",
            Vec3::X.angle_between(Vec3::new(1.0, 1.0, 0.0)),
        ),
        ("v3_assign", v.x * 10.0 + v.y),
        (
            "q_rotate",
            (Quat::from_rotation_z(0.9) * Vec3::new(1.0, 2.0, 3.0)).x,
        ),
        (
            "q_axis",
            (Quat::from_axis_angle(Vec3::new(0.0, 0.6, 0.8), 1.3) * Vec3::X).z,
        ),
        (
            "q_compose",
            (Quat::from_rotation_x(0.4) * Quat::from_rotation_y(-1.1)).w,
        ),
        (
            "q_inverse",
            (Quat::from_rotation_y(0.8).inverse() * Vec3::Z).x,
        ),
        (
            "q_slerp",
            Quat::from_rotation_z(0.2)
                .slerp(Quat::from_rotation_z(1.4), 0.3)
                .z,
        ),
        (
            "q_angle_between",
            Quat::from_rotation_x(0.3).angle_between(Quat::from_rotation_x(1.0)),
        ),
        ("q_to_axis_angle", axis.x * 10.0 + angle),
    ];
    for (f, want) in expected {
        let got = call(f);
        // glam's `acos` and `atan2` are approximations in places; the rest
        // is the same arithmetic.
        assert!(
            (got - want).abs() <= 1e-5 * want.abs().max(1.0),
            "{f}: {got} vs glam's {want}"
        );
    }
}