Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions VM/include/llsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ uint8_t lua_lsl_type(const TValue *tv);
int lsl_cast(lua_State *L);
int lsl_cast_list_elem(lua_State *L);
int lsl_cast_list_elem_poszero(lua_State *L);
int lua_quaternion_tofwd(lua_State *L);
int lua_quaternion_toleft(lua_State *L);
int lua_quaternion_toup(lua_State *L);

void conj_quat(float *quat);
void rot_vec(const float *vec, const float *rot, float *res);
Expand Down
3 changes: 3 additions & 0 deletions VM/src/lll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@ static const luaL_Reg lllib[] = {
{"ToLower", ll_to_lower},
{"ToUpper", ll_to_upper},
{"GetTime", ll_get_time},
{"Rot2Fwd", lua_quaternion_tofwd},
{"Rot2Left", lua_quaternion_toleft},
{"Rot2Up", lua_quaternion_toup},
{NULL, NULL},
};

Expand Down
9 changes: 3 additions & 6 deletions VM/src/llsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1616,21 +1616,21 @@ static inline void push_rotated_vector(lua_State *L, const float* vec) {
lua_pushvector(L, res[0], res[1], res[2]);
}

static int lua_quaternion_tofwd(lua_State *L)
int lua_quaternion_tofwd(lua_State *L)
{
const float vec[3] = {1.0f, 0.0f, 0.0f};
push_rotated_vector(L, vec);
return 1;
}

static int lua_quaternion_toleft(lua_State *L)
int lua_quaternion_toleft(lua_State *L)
{
const float vec[3] = {0.0f, 1.0f, 0.0f};
push_rotated_vector(L, vec);
return 1;
}

static int lua_quaternion_toup(lua_State *L)
int lua_quaternion_toup(lua_State *L)
{
const float vec[3] = {0.0f, 0.0f, 1.0f};
push_rotated_vector(L, vec);
Expand All @@ -1644,9 +1644,6 @@ static const luaL_Reg quaternionlib[] = {
{"dot", lua_quaternion_dot},
{"slerp", lua_quaternion_slerp},
{"conjugate", lua_quaternion_conjugate},
{"tofwd", lua_quaternion_tofwd},
{"toleft", lua_quaternion_toleft},
{"toup", lua_quaternion_toup},
{NULL, NULL},
};

Expand Down
12 changes: 6 additions & 6 deletions tests/conformance/quaternion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ assert(quaternion.conjugate(quat) == quaternion(-quat.x, -quat.y, -quat.z, quat.
assert(`{quaternion.slerp(yaw_ninety, quaternion(0, 0, 0, 1), 0.5)}` == "<0, 0, 0.382683, 0.92388>")

-- string cast to deal with precision
assert(`{quaternion.tofwd(yaw_ninety)}` == "<0, 1, 0>")

@tapple tapple Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a test script to see if they are actually equal. they aren't:

local function printVector(v: vector)
    -- higher precision than default
    return "<" .. v.x .. ", " .. v.y .. ", " .. v.z .. ">"
end

local function assertQuaternion(q: quaternion)
    if ll.Rot2Fwd(q) ~= quaternion.tofwd(q) then
        print(`ll.Rot2Fwd({q} ~= quaternion.tofwd({q}): {printVector(ll.Rot2Fwd(q))} ~= {printVector(quaternion.tofwd(q))}`)
    end
    if ll.Rot2Left(q) ~= quaternion.toleft(q) then
        print(`ll.Rot2Left({q} ~= quaternion.toleft({q}): {printVector(ll.Rot2Left(q))} ~= {printVector(quaternion.toleft(q))}`)
    end
    if ll.Rot2Up(q) ~= quaternion.toup(q) then
        print(`ll.Rot2Up({q} ~= quaternion.toup({q}): {printVector(ll.Rot2Up(q))} ~= {printVector(quaternion.toup(q))}`)
    end
end

--[[
assertQuaternion(quaternion(0, 0, 0, 0))
for _ = 1, 1000 do
    local q = quaternion(math.random() * 4 - 2, math.random() * 4 - 2, math.random() * 4 - 2, math.random() * 4 - 2)
    assertQuaternion(q)
end
print(done)
--]]
-- print(quaternion(1, 2, 3, 4) + quaternion(4, 3, 2, 1))
-- print(quaternion(1, 2, 3, 4) * 2)
local q = quaternion(-1.426371, 0.787597, -1.999946, -0.307976)
assertQuaternion(q)
print(`      ll.Rot2Left: {printVector(ll.Rot2Left(q))}`)
print(`quaternion.toleft: {printVector(quaternion.toleft(q))}`)
print(`      ll.Rot2Up: {printVector(ll.Rot2Up(q))}`)
print(`quaternion.toup: {printVector(quaternion.toup(q))}`)

Output:

[1:14 AM] Object: ll.Rot2Left(<-1.426371, 0.787597, -1.999946, -0.307976> ~= quaternion.toleft(<-1.426371, 0.787597, -1.999946, -0.307976>): <-0.515400230884552, -0.7880847454071045, -0.33657824993133545> ~= <-0.515400230884552, -0.7880848050117493, -0.33657824993133545>
[1:14 AM] Object: ll.Rot2Up(<-1.426371, 0.787597, -1.999946, -0.307976> ~= quaternion.toup(<-1.426371, 0.787597, -1.999946, -0.307976>): <0.7734240889549255, -0.5969172716140747, 0.21331876516342163> ~= <0.7734240889549255, -0.5969172716140747, 0.21331875026226044>

      ll.Rot2Left: <-0.515400230884552, -0.7880847454071045, -0.33657824993133545>
quaternion.toleft: <-0.515400230884552, -0.7880848050117493, -0.33657824993133545>
      ll.Rot2Up: <0.7734240889549255, -0.5969172716140747, 0.21331876516342163>
quaternion.toup: <0.7734240889549255, -0.5969172716140747, 0.21331875026226044>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert(`{quaternion.toleft(yaw_ninety)}` == "<-1, 0, 0>")
assert(`{quaternion.toup(yaw_ninety)}` == "<0, 0, 1>")
assert(`{ll.Rot2Fwd(yaw_ninety)}` == "<0, 1, 0>")
assert(`{ll.Rot2Left(yaw_ninety)}` == "<-1, 0, 0>")
assert(`{ll.Rot2Up(yaw_ninety)}` == "<0, 0, 1>")

-- Zero quaternion normalizes to identity before rotation (matches server)
assert(`{quaternion.tofwd(quaternion(0, 0, 0, 0))}` == "<1, 0, 0>")
assert(`{quaternion.toleft(quaternion(0, 0, 0, 0))}` == "<0, 1, 0>")
assert(`{quaternion.toup(quaternion(0, 0, 0, 0))}` == "<0, 0, 1>")
assert(`{ll.Rot2Fwd(quaternion(0, 0, 0, 0))}` == "<1, 0, 0>")
assert(`{ll.Rot2Left(quaternion(0, 0, 0, 0))}` == "<0, 1, 0>")
assert(`{ll.Rot2Up(quaternion(0, 0, 0, 0))}` == "<0, 0, 1>")

-- Test that tostring doesn't lose precision for large component values
assert(`{vector(123456.5, 0, 0)}` == "<123456.5, 0, 0>")
Expand Down