new math functions

lua has added math.sign and math.clamp functions, and maybe others. would be nice to have.

more tricky might be to add the ability to use a quart to rotate a vector, though there is probably a roundabout way to o it.

thanks!

@RonJeffries Those functions are easy to duplicate.

function sign(val)
    if val<0 then
        return -1
    elseif val==0 then
        return 0
    else
        return 1
    end
end

function clamp(val,min,max)
    if val<= min then
        return min
    elseif val>= max then
        return max
    else
        return val
    end
end

clamp is quicker as math.min(max,math.max(min,val))

Applying a quat to a vec3 is available via my quat extension library: https://github.com/loopspace/Codea-Library-Maths/blob/master/VecExt.lua

@LoopSpace Thanks for that. I knew there was a way to do it like that, but I didn’t want to spend the time trying to get it right.

oh yes, can dup … builtins be nice tho … thanks!
will check out the lib, @LoopSpace thanks!

The plan is to update to Lua 5.4 as soon as it’s ready, so I’ll definitely integrate these new math functions

@Simeon while on the subject of missing functions…as i mentioned in a previous post, i was surprised there is no angleBetween() for vec3.

@piinthesky Mathematically, angleBetween for vec3s should return a quat. This is implemented in my library as `u:rotateTo(v)’

@LoopSpace surely i can calculate a space angle between two 3d vectors, no? A dot product would give it?

axis-angle between two vec3 is what one generally wants, or “rotation between” which is the quat from one to the other.

@LoopSpace your library is … daunting. i suppose one can just use it. folding it inside Codea-lua would make that more palatable for the likes of me, though i’m enjoying figuring out its inner workings. really good stuff!

@piinthesky Yes, you can calculate the angle via the dot product. But as @RonJeffries says, most of the time you want to know how to rotate one vector onto the other and for that you need a quaternion.

@RonJeffries It’s a library that has grown, and grown! I should probably document it a bit better than it is. Feel free to ask about anything that isn’t clear.

thanks! from my short look, using it will be straightforward. understanding how to set up all the metatables will be … interesting.

i suspect it might be better organized to put usage “at the top” somehow, but it’s not clear how. ideally, it’d be folded into Codea, but since most folks don’t need it, it might be bloaty if bloaty is a word.